webservice changes
This commit is contained in:
@ -2,10 +2,6 @@ from dbpool import getConnection
|
|||||||
from heroes_mock import HEROES
|
from heroes_mock import HEROES
|
||||||
|
|
||||||
def get_heroes():
|
def get_heroes():
|
||||||
# if HEROES:
|
|
||||||
# return HEROES
|
|
||||||
# else:
|
|
||||||
# return 'No heroes available', 404
|
|
||||||
try:
|
try:
|
||||||
dbh = getConnection()
|
dbh = getConnection()
|
||||||
heroes = []
|
heroes = []
|
||||||
@ -14,31 +10,49 @@ def get_heroes():
|
|||||||
for (id, name) in cur:
|
for (id, name) in cur:
|
||||||
heroes.append({"id": id, "name": name})
|
heroes.append({"id": id, "name": name})
|
||||||
return heroes
|
return heroes
|
||||||
except Exception:
|
except Exception as err:
|
||||||
return 500, str(Exception)
|
return str(err), 500
|
||||||
finally:
|
finally:
|
||||||
dbh.close()
|
dbh.close()
|
||||||
|
|
||||||
|
|
||||||
def get_hero(id=None):
|
def get_hero(id=None):
|
||||||
try:
|
try:
|
||||||
hero = next(x for x in HEROES if x["id"] == id)
|
dbh = getConnection()
|
||||||
return { "id": hero["id"], "name": hero["name"] }
|
cur = dbh.cursor()
|
||||||
except StopIteration:
|
cur.execute("SELECT id, name FROM hero WHERE id = ?", (id,))
|
||||||
return 'Hero not found', 404
|
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):
|
def put_hero(id=None, hero=None):
|
||||||
try:
|
try:
|
||||||
heroToUpdate = next(x for x in HEROES if x["id"] == id)
|
dbh = getConnection()
|
||||||
checkNewName = next((x for x in HEROES if x["name"] == hero["name"]), None)
|
cur = dbh.cursor()
|
||||||
if (checkNewName):
|
cur.execute("UPDATE hero SET name = ? WHERE id = ?", (hero["name"], id))
|
||||||
return 'Duplicate name', 403
|
dbh.commit()
|
||||||
heroToUpdate["name"] = hero["name"]
|
|
||||||
return 'Hero updated', 200
|
return 'Hero updated', 200
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
return 'Hero not found', 404
|
return 'Hero not found', 404
|
||||||
except Exception:
|
except Exception as err:
|
||||||
return 'Some error', 403
|
return str(err), 500
|
||||||
|
finally:
|
||||||
|
dbh.close()
|
||||||
|
|
||||||
def post_hero(hero=None):
|
def post_hero(hero=None):
|
||||||
try:
|
try:
|
||||||
@ -46,5 +60,5 @@ def post_hero(hero=None):
|
|||||||
hero["id"] = newHeroId
|
hero["id"] = newHeroId
|
||||||
HEROES.append(hero)
|
HEROES.append(hero)
|
||||||
return 'Hero inserted', 201
|
return 'Hero inserted', 201
|
||||||
except Exception:
|
except Exception as err:
|
||||||
return 'Some error', 403
|
return str(err), 403
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
import connexion
|
import connexion
|
||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
|
from dbpool import createConnectionPool
|
||||||
|
|
||||||
|
# prepare database connections
|
||||||
|
createConnectionPool()
|
||||||
|
|
||||||
# instantiate the webservice
|
# instantiate the webservice
|
||||||
app = connexion.App(__name__)
|
app = connexion.App(__name__)
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
swagger: '2.0'
|
swagger: '2.0'
|
||||||
info:
|
info:
|
||||||
title: Hello API
|
title: Heroes
|
||||||
version: "0.1"
|
version: "0.1"
|
||||||
|
|
||||||
paths:
|
paths:
|
||||||
/greeting:
|
/greeting/hello:
|
||||||
get:
|
get:
|
||||||
|
tags: [ "greeting" ]
|
||||||
operationId: api.say_hello
|
operationId: api.say_hello
|
||||||
summary: Returns a greeting
|
summary: Returns a greeting
|
||||||
parameters:
|
parameters:
|
||||||
@ -21,8 +22,10 @@ paths:
|
|||||||
message:
|
message:
|
||||||
type: string
|
type: string
|
||||||
description: Message greeting
|
description: Message greeting
|
||||||
/hero/{id}:
|
|
||||||
|
/heroes/hero/{id}:
|
||||||
get:
|
get:
|
||||||
|
tags: [ "heroes" ]
|
||||||
operationId: heroes.get_hero
|
operationId: heroes.get_hero
|
||||||
summary: Returns hero by id
|
summary: Returns hero by id
|
||||||
parameters:
|
parameters:
|
||||||
@ -40,6 +43,7 @@ paths:
|
|||||||
500:
|
500:
|
||||||
description: Some server error
|
description: Some server error
|
||||||
put:
|
put:
|
||||||
|
tags: [ "heroes" ]
|
||||||
operationId: heroes.put_hero
|
operationId: heroes.put_hero
|
||||||
summary: Update a hero
|
summary: Update a hero
|
||||||
parameters:
|
parameters:
|
||||||
@ -63,8 +67,9 @@ paths:
|
|||||||
description: Hero not found
|
description: Hero not found
|
||||||
500:
|
500:
|
||||||
description: Some server error
|
description: Some server error
|
||||||
/hero:
|
/heroes/hero:
|
||||||
post:
|
post:
|
||||||
|
tags: [ "heroes" ]
|
||||||
operationId: heroes.post_hero
|
operationId: heroes.post_hero
|
||||||
summary: Insert a hero
|
summary: Insert a hero
|
||||||
parameters:
|
parameters:
|
||||||
@ -82,8 +87,9 @@ paths:
|
|||||||
description: Duplicate name
|
description: Duplicate name
|
||||||
500:
|
500:
|
||||||
description: Some server error
|
description: Some server error
|
||||||
/heroes:
|
/heroes/heroes:
|
||||||
get:
|
get:
|
||||||
|
tags: [ "heroes" ]
|
||||||
operationId: heroes.get_heroes
|
operationId: heroes.get_heroes
|
||||||
summary: Returns all heroes
|
summary: Returns all heroes
|
||||||
responses:
|
responses:
|
||||||
|
Reference in New Issue
Block a user