From 24482da680e78f246b11afb07eac7d445110c287 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Fri, 15 Jan 2021 12:53:46 +0100 Subject: [PATCH] database in service --- tools/ws/dbpool.py | 19 +++++++++++++++++ tools/ws/heroes.py | 38 ++++++++++++++++----------------- tools/ws/heroes_mock.py | 47 +++++++++++++++++++++++++++++++++++++++++ tools/ws/swagger.yaml | 14 ++++++++++-- 4 files changed, 97 insertions(+), 21 deletions(-) create mode 100644 tools/ws/dbpool.py create mode 100644 tools/ws/heroes_mock.py diff --git a/tools/ws/dbpool.py b/tools/ws/dbpool.py new file mode 100644 index 0000000..79405fa --- /dev/null +++ b/tools/ws/dbpool.py @@ -0,0 +1,19 @@ +import mariadb + +pool = None + +def createConnectionPool(): + global pool + + pool = mariadb.ConnectionPool( + user = 'heroes', + password = 'test123', + host = '172.16.10.18', + database = 'heroes', + pool_name = 'wep-app', + pool_size = 5 + ) + +def getConnection(): + global pool + return pool.get_connection() diff --git a/tools/ws/heroes.py b/tools/ws/heroes.py index 1f81d7c..50bacf1 100644 --- a/tools/ws/heroes.py +++ b/tools/ws/heroes.py @@ -1,24 +1,24 @@ -HEROES = [ - { "id": 1, "name": "Wolfgang" }, - { "id": 2, "name": "Andreas" }, - { "id": 3, "name": "Frank" }, - { "id": 4, "name": "Thomas" }, - { "id": 5, "name": "Barbara" }, - { "id": 6, "name": "Robert" }, - { "id": 7, "name": "Raphael" }, - { "id": 8, "name": "Lucia" }, - { "id": 9, "name": "Gregor" }, -] - - - - +from dbpool import getConnection +from heroes_mock import HEROES def get_heroes(): - if HEROES: - return HEROES - else: - return 'No heroes available', 404 +# if HEROES: +# return HEROES +# else: +# return 'No heroes available', 404 + try: + dbh = getConnection() + heroes = [] + cur = dbh.cursor() + cur.execute("SELECT id, name FROM hero") + for (id, name) in cur: + heroes.append({"id": id, "name": name}) + return heroes + except Exception: + return 500, str(Exception) + finally: + dbh.close() + def get_hero(id=None): try: diff --git a/tools/ws/heroes_mock.py b/tools/ws/heroes_mock.py new file mode 100644 index 0000000..25d06a9 --- /dev/null +++ b/tools/ws/heroes_mock.py @@ -0,0 +1,47 @@ +HEROES = [ + { "id": 1, "name": "Wolfgang" }, + { "id": 2, "name": "Andreas" }, + { "id": 3, "name": "Frank" }, + { "id": 4, "name": "Thomas" }, + { "id": 5, "name": "Barbara" }, + { "id": 6, "name": "Robert" }, + { "id": 7, "name": "Raphael" }, + { "id": 8, "name": "Lucia" }, + { "id": 9, "name": "Gregor" }, +] + + +def get_heroes(): + if HEROES: + return HEROES + else: + return 'No heroes available', 404 + +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 + +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"] + return 'Hero updated', 200 + except StopIteration: + return 'Hero not found', 404 + except Exception: + return 'Some error', 403 + +def post_hero(hero=None): + try: + newHeroId = len(HEROES) + hero["id"] = newHeroId + HEROES.append(hero) + return 'Hero inserted', 201 + except Exception: + return 'Some error', 403 diff --git a/tools/ws/swagger.yaml b/tools/ws/swagger.yaml index 08bb67d..a8d9374 100644 --- a/tools/ws/swagger.yaml +++ b/tools/ws/swagger.yaml @@ -37,6 +37,8 @@ paths: $ref: '#/definitions/Hero' 404: description: Hero not found + 500: + description: Some server error put: operationId: heroes.put_hero summary: Update a hero @@ -53,12 +55,14 @@ paths: responses: 200: description: Hero updated - 402: - description: Duplicate name 403: description: Some error + 409: + description: Duplicate name 404: description: Hero not found + 500: + description: Some server error /hero: post: operationId: heroes.post_hero @@ -74,6 +78,10 @@ paths: description: Hero inserted 403: description: Some error + 409: + description: Duplicate name + 500: + description: Some server error /heroes: get: operationId: heroes.get_heroes @@ -87,6 +95,8 @@ paths: $ref: '#/definitions/Hero' 404: description: No heroes available + 500: + description: Some server error definitions: