Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
PyConWeb 2018
2018-07-01
HENNING JACOBS
@try_except_
API First with
Connexion
2
ZALANDO AT A GLANCE
~ 4.5billion EUR
revenue 2017
> 200
million
visits
per
month
> 15.000
employees in
Europe
> 70%
of visits via
mobile devices
> 23
million
active customers
> 300.000
product choices
~ 2.000
brands
15
countries
3
ZALANDO TECH
~ 2.000
Employees in Tech
> 200
Delivery teams
4
API FIRST
5
API FIRST
• Define REST API before implementation
• API definition is contract between teams
• Includes validation & documentation
6
ZALANDO TECH RADAR
7
ZALANDO TECH RADAR
8
RESTful API
Guidelines
https://github.com/zalando/restful-api-guidelines
API First with Connexion - PyConWeb 2018
API First with Connexion - PyConWeb 2018
11 https://github.com/zalando/intellij-swagger
12 https://github.com/zalando/intellij-swagger
13 https://github.com/zalando/zally
14
Python?
15
Connexion
OpenAPI-first with Python+Flask
https://github.com/zalando/connexion
16
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
17
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
18
CONNEXION: USAGE
PETS = {}
def get_pets(animal_type=None):
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(__name__)
app.add_api('swagger.yaml')
app.run(port=8080, server='gevent')
https://github.com/zalando/connexion
19
QUICKSTART WITH SWAGGER CODEGEN
wget
http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/2.3.1/swagger
-codegen-cli-2.3.1.jar -O swagger-codegen-cli.jar
java -jar swagger-codegen-cli.jar generate -i swagger.yaml -l python-flask
docker build -t myapp .
docker run -it -p 8080:8080 myapp
Swagger's
"python-flask" is
actually Connexion!
https://github.com/swagger-api/swagger-codegen
20
CONNEXION FEATURES
• Path, query and body params mapped to Python args
• Bundled Swagger UI (served on /ui/ path)
• Automatic JSON serialization
• Schema validation for HTTP request body and query params:
• required object properties
• primitive types (string, integers, etc)
• date/time values
• string lengths
• min/max values
• regular expressions
https://github.com/zalando/connexion
21
DEPLOYMENT
CONSIDERATIONS
22
SWAGGER CODEGEN..
docker run -it -p 8080:8080 swagger-codegen-server
... OAuth2 token info URL missing. **IGNORING SECURITY REQUIREMENTS**
* Serving Flask app "__main__" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
23
CONSIDERATIONS
• WSGI server?
• Runtime?
• Target environment?
• High availability?
• Performance?
24
CONSIDERATIONS
• WSGI server? gevent
• Runtime? Docker, image ~74MiB
• Target environment? Kubernetes
• High availability? multiple pods, readinessProbe
• Performance? load test
https://github.com/hjacobs/connexion-example-redis-kubernetes
25
EXAMPLE #1
https://github.com/hjacobs/connexion-example-redis-kubernetes
26
KILLING REDIS
kubectl get pod
NAME READY STATUS RESTARTS AGE
connexion-example-67c64766f5-rgmmz 1/1 Running 0 55s
connexion-example-67c64766f5-rwjt7 1/1 Running 1 1h
connexion-example-redis-0 0/1 Terminating 1 1h
https://github.com/hjacobs/connexion-example-redis-kubernetes
27
RESILIENCE
https://github.com/hjacobs/connexion-example-redis-kubernetes
Rolling deployment
Redis downtime
28
EXAMPLE #2: ZALANDO AWS CREDENTIALS
https://github.com/zalando-stups/aws-credentials-service
29
CONNEXION IN ZALANDO
30
REAL WORLD SERVICE
31
OPEN SOURCE
Zalando's RESTful API Guidelines
https://github.com/zalando/restful-api-guidelines
IntelliJ IDEA Swagger Editor Plugin
https://github.com/zalando/intellij-swagger
Zally API Linter
https://github.com/zalando/zally
Connexion
https://github.com/zalando/connexion
Connexion Examples
https://github.com/hjacobs/connexion-example
https://github.com/zalando-stups/aws-credentials-service
QUESTIONS?
HENNING JACOBS
HEAD OF
DEVELOPER PRODUCTIVITY
henning@zalando.de
@try_except_
Illustrations by @01k

More Related Content

API First with Connexion - PyConWeb 2018