Technical Blog and online Resume
Published on 11 May 2021 in Category [Dev]
I’ve been looking around for a fast way to build Rest API for a work project. Recently, in a similar situation, while developing a pet project, I decided to use Flask for the purpose. Flask is a lightweight web framework for Python, it’s simple, it does its job, but in my opinion it doesn’t provide, out of the box, every element that one would need in order to build a modern Rest API for the real world.
Does that mean that Flask is no good for Rest API? Not at all. It just means that probably, you’ll have to spend some extra time to do it.
To put it simply, I was looking for a quick way to build an unbloated and lightweight Rest API. Furthermore, since documentation and good standars are paramount when working with heterogeneous teams, I wanted it to be compliant with the most recent version of OpenApi and use Swagger out of the box.
I spent a whole afternoon looking around and I came across a multitude of alternatives for Python, it was a very discouraging puzzle of frameworks and extensions. To increase the overall confusion I found a huge number of outdated tutorials, which made difficult for me to quickly grasp the state of the art at the moment. At that point I almost gave up and started considering other options outside the Python world.
Lucky me, I found an article packed with useful informations, which brushed a broad picture of the more general topic Python & Rest, presenting several alternatives, one of them being FastAPI.
I decided to visit the project website, it claimed to be fast to execute (as fast as Go and NodeJS), fast to code, robust and standards-based, all things considered it sounded incredibly appealing.
The thing that made me decide almost instantly for fastAPI is that on the project website I found immediately a very straightforward, complete and clear Tutorial / User Guide, rich of useful links to go deeper on specific topics.
Good documentation means that the developers cared for other human beings to use their product and this is a thing I always appreciate, you know… being a human being myself. So again, I strongly recommend you to try it out.
Retrospectively it was a good call since it took me less than a working day to complete a fully working Rest API, with a simple CRUD, backed by an SQL server for persistence.
Few words about what I got by the end of the basic user guide, roughly:
Data validation is a big topic with API, so to take care of all the data validation stuff, FastAPI makes heavy use of Pydantic which also helps when you have to define your own data model and pass it around, or converting it from the JSON contained in the body of a request. Most of the things you will be able to with a request after it hits an endpoint is empowered by pydantic.
The tutorial uses SQLAlchemy, so be warned since this may introduce some confusion between Pydantic Models and SQLAlchemy Models. Keep in mind that the firsts define a valid data shape (a schema), the seconds define (more or less) the sql tables that will represent the data.
Deploying the service via Docker is pretty straightforward, you can check the guide for mode details. To be honest, my pick was to play it safe, using
python:3.8 the official python image (based on the latest stable Debian release) and to add the packages I needed as requirements, mainly: