database in service
This commit is contained in:
19
tools/ws/dbpool.py
Normal file
19
tools/ws/dbpool.py
Normal file
@ -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()
|
@ -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:
|
||||
|
47
tools/ws/heroes_mock.py
Normal file
47
tools/ws/heroes_mock.py
Normal file
@ -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
|
@ -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:
|
||||
|
Reference in New Issue
Block a user