python meetup berlin aug 2015: connexion swagger-first rest framework

8
Connexion Swagger-first with Python+Flask Python Meetup Berlin 2015-08-13 [email protected] @try_except_

Upload: henning-jacobs

Post on 17-Aug-2015

139 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Python Meetup Berlin Aug 2015: Connexion Swagger-first REST Framework

ConnexionSwagger-first with Python+FlaskPython Meetup Berlin [email protected] @try_except_

Page 2: Python Meetup Berlin Aug 2015: Connexion Swagger-first REST Framework

API First

● Define REST API before implementation

● API definition is contract between teams

● Includes validation & documentation

Page 3: Python Meetup Berlin Aug 2015: Connexion Swagger-first REST Framework

info: {title: "Pet Shop Example API", version: "0.1"}paths: /pets: get: operationId: app.get_pets parameters: - name: animal_type in: query type: string responses: 200: schema: type: array items: $ref: '#/definitions/Pet' /pets/{pet_id}: get: operationId: app.get_pet parameters: - name: pet_id in: path type: string required: true responses: 200: schema: $ref: '#/definitions/Pet' 404: description: Pet does not exist put: tags: [Pets] operationId: app.put_pet summary: Create or update a pet parameters: - name: pet_id in: path type: string required: true - name: body in: body schema: $ref: '#/definitions/Pet' responses: 200: description: Pet updated 201: description: New pet created delete: tags: [Pets] operationId: app.delete_pet summary: Remove a pet parameters: - name: pet_id in: path type: string required: true responses: 204: description: Pet was deleted 404: description: Pet does not exist

Page 4: Python Meetup Berlin Aug 2015: Connexion Swagger-first REST Framework

info: {title: "Pet Shop Example API", version: "0.1"}paths: /pets: get: operationId: app.get_pets parameters: - name: animal_type in: query type: string responses: 200: schema: type: array items: $ref: '#/definitions/Pet' /pets/{pet_id}: get: operationId: app.get_pet parameters: - name: pet_id in: path type: string required: true responses: 200: schema: $ref: '#/definitions/Pet' 404: description: Pet does not exist put: tags: [Pets] operationId: app.put_pet summary: Create or update a pet parameters: - name: pet_id in: path type: string required: true - name: body in: body schema: $ref: '#/definitions/Pet' responses: 200: description: Pet updated 201: description: New pet created delete: tags: [Pets] operationId: app.delete_pet summary: Remove a pet parameters: - name: pet_id in: path type: string required: true responses: 204: description: Pet was deleted 404: description: Pet does not exist

Page 5: Python Meetup Berlin Aug 2015: Connexion Swagger-first REST Framework

Connexion: UsagePETS = {}

def get_pets(): animal_type = flask.request.args.get('animal_type') return [pet for pet in PETS.values() if not animal_type or pet['animal_type'] == animal_type]

def get_pet(pet_id): pet = PETS.get(pet_id) return pet or ('Not found', 404)

# ...

app = connexion.App('myapp', port=8080, server='gevent')app.add_api('swagger.yaml')app.run()

Page 6: Python Meetup Berlin Aug 2015: Connexion Swagger-first REST Framework
Page 7: Python Meetup Berlin Aug 2015: Connexion Swagger-first REST Framework

HTTPie as REST client+ http PUT :8080/pets/1 name=foo animal_type=testHTTP/1.1 201 CREATEDContent-Length: 4Content-Type: application/jsonDate: Tue, 11 Aug 2015 16:34:06 GMT

+ http :8080/pets/1HTTP/1.1 200 OKContent-Length: 101Content-Type: application/jsonDate: Tue, 11 Aug 2015 16:34:06 GMT

{ "animal_type": "test", "created": "2015-08-11T16:34:06.410620Z", "id": "1", "name": "foo"}

Page 8: Python Meetup Berlin Aug 2015: Connexion Swagger-first REST Framework

Linksgithub.com/hjacobs/connexion-example

github.com/zalando/connexion

tech.zalando.com@try_except_