refactor
This commit is contained in:
@ -11,6 +11,12 @@ HEROES = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def get_heroes():
|
||||||
|
if HEROES:
|
||||||
|
return HEROES
|
||||||
|
else:
|
||||||
|
return 'No heroes available', 404
|
||||||
|
|
||||||
def get_hero(id=None):
|
def get_hero(id=None):
|
||||||
try:
|
try:
|
||||||
hero = next(x for x in HEROES if x["id"] == id)
|
hero = next(x for x in HEROES if x["id"] == id)
|
||||||
@ -18,25 +24,21 @@ def get_hero(id=None):
|
|||||||
except StopIteration:
|
except StopIteration:
|
||||||
return 'Hero not found', 404
|
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):
|
def put_hero(id=None, hero=None):
|
||||||
if id:
|
try:
|
||||||
# update
|
heroToUpdate = next(x for x in HEROES if x["id"] == id)
|
||||||
try:
|
heroToUpdate["name"] = hero["name"]
|
||||||
heroToUpdate = next(x for x in HEROES if x["id"] == id)
|
return 'Hero updated', 200
|
||||||
heroToUpdate["name"] = hero["name"]
|
except StopIteration:
|
||||||
return 'Hero updated', 200
|
return 'Hero not found', 404
|
||||||
except StopIteration:
|
except Exception:
|
||||||
return 'Hero not found', 404
|
return 'Some error', 403
|
||||||
else:
|
|
||||||
# insert
|
def post_hero(hero=None):
|
||||||
|
try:
|
||||||
newHeroId = len(HEROES)
|
newHeroId = len(HEROES)
|
||||||
hero["id"] = newHeroId
|
hero["id"] = newHeroId
|
||||||
HEROES.append(hero)
|
HEROES.append(hero)
|
||||||
return 'Hero inserted', 201
|
return 'Hero inserted', 201
|
||||||
|
except Exception:
|
||||||
|
return 'Some error', 403
|
||||||
|
@ -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
|
|
@ -3,7 +3,7 @@ from flask_cors import CORS
|
|||||||
|
|
||||||
# instantiate the webservice
|
# instantiate the webservice
|
||||||
app = connexion.App(__name__)
|
app = connexion.App(__name__)
|
||||||
app.add_api('my_api.yaml')
|
app.add_api('swagger.yaml')
|
||||||
|
|
||||||
# CORSify it - otherwise Angular won't accept it
|
# CORSify it - otherwise Angular won't accept it
|
||||||
CORS(app.app)
|
CORS(app.app)
|
||||||
|
Reference in New Issue
Block a user