You are on page 1of 11

Turbo-Charging

Your Python API


Featuring FastAPI, Starlette, Pydantic and OpenAPI
WHAT THIS PRESENTATION IS
AN OUTLINE A COMPARISON A STORY
Presenting new frameworks Contrasting with other How and why FastAPI was
Showcasing possibilities frameworks chosen for the Ads
Outlining differences in Automation Service
approach

WHAT THIS PRESENTATION ISN’T


A POSITION A CONCLUSION A REVOLUTION
Not claiming this approach is Not closing a debate, but Not advocating for the
superior to others adding new considerations universal adoption of
FastAPI
PYDANTIC

Runtime type validation


Can integrate with dataclasses
Can load values from environment for configs
Automatic serialisation and deserialisation
Can generate JSON schemas from models
PyCharm and mypy plugins for enhanced static analysis
Python 3.6+ web framework based on Starlette
Similar APIs to Flask
Heavily reliant on typings
Can be synchronous or asynchronous
Focus on web APIs, but useable for any website
Automatic OpenAPI schema and Swagger UI docs
Data validation through Pydantic
Support for Datadog, Sentry, GraphQL…
DEMO
FastAPI AS A WEB FRAMEWORK

FastAPI Flask

WEB SERVER uvicorn (ASGI) + gunicorn uWSGI (WSGI)

Still very underdeveloped, but Many, many plugins for all sorts
PLUGINS/MIDDLEWARE
easy to extend of different tasks

INPUT VALIDATION Yes (typings) No

OpenAPI GENERATION Yes Yes, with plugins (e.g. Flasger)

ASYNC? Yes No
FastAPI FOR INTER-SERVICE
COMMUNICATION
FastAPI gRPC

Designed for REST APIs Designed for RPC

TRANSPORT PROTOCOL HTTP/1.1 HTTP/2

Server code -> OpenAPI spec -> Protocol Buffers ->


CODE GENERATION
Client code Client/Server code

STRENGTHS More interoperable Better performance (in theory)


(REST+JSON) Can be converted to
Easily readable payloads REST+JSON (grpc-gateway)
Very similar to Flask First-class code generation
Pythonic (for the server, at
least – client code, less so)
FastAPI FOR ADS AUTOMATION

Many network calls, no async support


Replaced Flask with FastAPI
Rigorously typed code
FastAPI fit in seamlessly
Messy configuration files, lots of manual validation
Switched to Pydantic settings, clean and simple
Still using a hand-written client in tiqetsweb
Might switch to code generation if API grows
Also haven’t tried gunicorn, as it is currently overkill

MARCOS OTTONELLO
“Just make everybody’s life
easier and use FastAPI.”
FastAPI FOR ADS AUTOMATION

Other niceties:
▪ Auto-completion of config entries
▪ Static and run-time validation of API parameters
▪ Auto-generated and hosted docs for quick testing
▪ Zero-effort serialisation of models
▪ Can run Flask or Django on top of FastAPI using
WSGIMiddleware
Questions?

CREDITS: This presentation template was created by Slidesgo, including


icons by Flaticon, and infographics & images by Freepik.
Please keep this slide for attribution.
RESOURCES

The following sources were used to inform this presentation, check them out for more information:
■ FastAPI Documentation
■ Pydantic Documentation
■ Starlette
■ Comparing OpenAPI with gRPC – Red Hat Services Speak
■ OpenAPI and gRPC Side-by-Side – Medium

You might also like