This commit is contained in:
2021-01-14 21:52:56 +01:00
parent 521e916bbd
commit 9717d171bf
3 changed files with 20 additions and 103 deletions

View File

@ -11,6 +11,12 @@ HEROES = [
]
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)
@ -18,25 +24,21 @@ def get_hero(id=None):
except StopIteration:
return 'Hero not found', 404
def get_heroes():
if HEROES:
return HEROES
else:
return 'No heroes available', 404
def put_hero(id=None, hero=None):
if id:
# update
try:
heroToUpdate = next(x for x in HEROES if x["id"] == id)
heroToUpdate["name"] = hero["name"]
return 'Hero updated', 200
except StopIteration:
return 'Hero not found', 404
else:
# insert
try:
heroToUpdate = next(x for x in HEROES if x["id"] == id)
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

View File

@ -1,85 +0,0 @@
swagger: '2.0'
info:
title: Hello API
version: "0.1"
paths:
/greeting:
get:
operationId: api.say_hello
summary: Returns a greeting
parameters:
- name: name
in: query
type: string
responses:
200:
description: Successful response.
schema:
type: object
properties:
message:
type: string
description: Message greeting
/hero:
get:
operationId: heroes.get_hero
summary: Returns hero by id
parameters:
- name: id
in: query
type: integer
required: true
responses:
200:
description: Successful response.
schema:
$ref: '#/definitions/Hero'
404:
description: Hero not found
put:
operationId: heroes.put_hero
summary: Insert or update a hero
parameters:
- name: id
in: query
type: integer
required: false
- name: hero
in: body
required: true
schema:
$ref: '#/definitions/Hero'
responses:
200:
description: Hero updated
201:
description: New hero created
403:
description: Some error
404:
description: Hero not found
/heroes:
get:
operationId: heroes.get_heroes
summary: Returns all heroes
responses:
200:
description: Successful response.
schema:
type: array
items:
$ref: '#/definitions/Hero'
404:
description: No heroes available
definitions:
Hero:
description: Hero type
type: object
properties:
id:
type: integer
name:
type: string

View File

@ -3,7 +3,7 @@ from flask_cors import CORS
# instantiate the webservice
app = connexion.App(__name__)
app.add_api('my_api.yaml')
app.add_api('swagger.yaml')
# CORSify it - otherwise Angular won't accept it
CORS(app.app)