From 9717d171bfbf101a49906ab2ff14e4c8f7d34df5 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Thu, 14 Jan 2021 21:52:56 +0100 Subject: [PATCH] refactor --- tools/ws/heroes.py | 36 ++++++++++--------- tools/ws/my_api.yaml | 85 -------------------------------------------- tools/ws/server.py | 2 +- 3 files changed, 20 insertions(+), 103 deletions(-) delete mode 100644 tools/ws/my_api.yaml diff --git a/tools/ws/heroes.py b/tools/ws/heroes.py index 4e8c5e7..39bf205 100644 --- a/tools/ws/heroes.py +++ b/tools/ws/heroes.py @@ -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 diff --git a/tools/ws/my_api.yaml b/tools/ws/my_api.yaml deleted file mode 100644 index 213771d..0000000 --- a/tools/ws/my_api.yaml +++ /dev/null @@ -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 diff --git a/tools/ws/server.py b/tools/ws/server.py index 844c97c..301dfe0 100644 --- a/tools/ws/server.py +++ b/tools/ws/server.py @@ -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)