webservice changes

This commit is contained in:
2021-01-15 16:19:05 +01:00
parent 24482da680
commit e031660df9
3 changed files with 49 additions and 25 deletions

View File

@ -2,10 +2,6 @@ from dbpool import getConnection
from heroes_mock import HEROES
def get_heroes():
# if HEROES:
# return HEROES
# else:
# return 'No heroes available', 404
try:
dbh = getConnection()
heroes = []
@ -14,31 +10,49 @@ def get_heroes():
for (id, name) in cur:
heroes.append({"id": id, "name": name})
return heroes
except Exception:
return 500, str(Exception)
except Exception as err:
return str(err), 500
finally:
dbh.close()
def get_hero(id=None):
try:
hero = next(x for x in HEROES if x["id"] == id)
return { "id": hero["id"], "name": hero["name"] }
dbh = getConnection()
cur = dbh.cursor()
cur.execute("SELECT id, name FROM hero WHERE id = ?", (id,))
hero = None
try:
(id, name) = cur.next()
hero = { "id": id, "name": name }
print("x1: {}\n".format(hero))
except StopIteration:
return 'Hero not found', 404
return "Hero not found", 404
try:
(id, name) = cur.next()
return "More than one hero by that id ({}, {})".format(id, name), 500
except:
pass
return hero
except Exception as err:
return str(err), 500
finally:
dbh.close()
def put_hero(id=None, hero=None):
try:
heroToUpdate = next(x for x in HEROES if x["id"] == id)
checkNewName = next((x for x in HEROES if x["name"] == hero["name"]), None)
if (checkNewName):
return 'Duplicate name', 403
heroToUpdate["name"] = hero["name"]
dbh = getConnection()
cur = dbh.cursor()
cur.execute("UPDATE hero SET name = ? WHERE id = ?", (hero["name"], id))
dbh.commit()
return 'Hero updated', 200
except StopIteration:
return 'Hero not found', 404
except Exception:
return 'Some error', 403
except Exception as err:
return str(err), 500
finally:
dbh.close()
def post_hero(hero=None):
try:
@ -46,5 +60,5 @@ def post_hero(hero=None):
hero["id"] = newHeroId
HEROES.append(hero)
return 'Hero inserted', 201
except Exception:
return 'Some error', 403
except Exception as err:
return str(err), 403

View File

@ -1,5 +1,9 @@
import connexion
from flask_cors import CORS
from dbpool import createConnectionPool
# prepare database connections
createConnectionPool()
# instantiate the webservice
app = connexion.App(__name__)

View File

@ -1,11 +1,12 @@
swagger: '2.0'
info:
title: Hello API
title: Heroes
version: "0.1"
paths:
/greeting:
/greeting/hello:
get:
tags: [ "greeting" ]
operationId: api.say_hello
summary: Returns a greeting
parameters:
@ -21,8 +22,10 @@ paths:
message:
type: string
description: Message greeting
/hero/{id}:
/heroes/hero/{id}:
get:
tags: [ "heroes" ]
operationId: heroes.get_hero
summary: Returns hero by id
parameters:
@ -40,6 +43,7 @@ paths:
500:
description: Some server error
put:
tags: [ "heroes" ]
operationId: heroes.put_hero
summary: Update a hero
parameters:
@ -63,8 +67,9 @@ paths:
description: Hero not found
500:
description: Some server error
/hero:
/heroes/hero:
post:
tags: [ "heroes" ]
operationId: heroes.post_hero
summary: Insert a hero
parameters:
@ -82,8 +87,9 @@ paths:
description: Duplicate name
500:
description: Some server error
/heroes:
/heroes/heroes:
get:
tags: [ "heroes" ]
operationId: heroes.get_heroes
summary: Returns all heroes
responses: