From e031660df97686883d368c823017202c33c7c361 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Fri, 15 Jan 2021 16:19:05 +0100 Subject: [PATCH] webservice changes --- tools/ws/heroes.py | 54 +++++++++++++++++++++++++++---------------- tools/ws/server.py | 4 ++++ tools/ws/swagger.yaml | 16 +++++++++---- 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/tools/ws/heroes.py b/tools/ws/heroes.py index 50bacf1..a5a831b 100644 --- a/tools/ws/heroes.py +++ b/tools/ws/heroes.py @@ -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"] } - except StopIteration: - return 'Hero not found', 404 - + 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 + 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 diff --git a/tools/ws/server.py b/tools/ws/server.py index 301dfe0..c6aeb4c 100644 --- a/tools/ws/server.py +++ b/tools/ws/server.py @@ -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__) diff --git a/tools/ws/swagger.yaml b/tools/ws/swagger.yaml index a8d9374..6fea90e 100644 --- a/tools/ws/swagger.yaml +++ b/tools/ws/swagger.yaml @@ -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: