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):
|
||||
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
|
||||
|
@ -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
|
||||
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)
|
||||
|
Reference in New Issue
Block a user