I really liked FastAPI… Love at first sight kind of feeling…
So, I decided to read all its docs, and instead of having study notes, memos, or whatever, I am writing it all as a template.
Since I suffer from a lack of creativity, I needed to come up with a plot/story to build my template. I was upon that time of the month: Monthly groceries shop, which was the basis of the template: Shopcart API. Then, a side plot to spice things up: MY CAT IS A HACKER WHO’S ALWAYS TRYING TO SPEND ALL MY MONEY BUYING TUNA!
That’s more suited to me…
Have in mind that this template is not fully generic and abstract (with stuff like generic_post_endpoint
and/or whatever_print_function
). Its endpoints have names with meaning, functions, classes, and (pydantic) objects too.
Now let’s have a look at the FastAPI template… FasTemplate!
I wrote this as a follow-up after your 'Hello, wolrd' app.
I recommend that you use this repo as reading material to go along with the official User Guide (Tutorial and Advanced).
The code here already uses a lot of tools and functionalities provided by the framework, it's well documented, it has custom exceptions, test cases, and python code written using good practices while it ranges from simple for loops
to (custom) decorators
. I believe it is a good material to go along whether you are just starting or close enough to a mid-level fluency in Python.
The main dependencies are:
FastAPI
: Build the actual API endpointsSphinx
: Create our project documentationUvicorn
or GUnicorn
: Launches our APIPytest
: Run our tests
The workflow is really simple: I open the docs, read it, create the functionality inside the template, make sure it works, update the docs and commit it to the repo.
I’ve already added a sh*tton of features from the docs:
Template |
Topic |
---|---|
✅ |
Tutorial - User Guide - Intro |
✅ |
First Steps |
✅ |
Path Parameters |
✅ |
Query Parameters |
✅ |
Request Body |
✅ |
Query Parameters and String Validations |
✅ |
Path Parameters and Numeric Validations |
✅ |
Body - Multiple Parameters |
✅ |
Body - Fields |
✅ |
Body - Nested Models |
✅ |
Declare Request Example Data |
✅ |
Extra Data Types |
✅ |
Cookie Parameters |
✅ |
Header Parameters |
✅ |
Response Model |
✅ |
Extra Models |
✅ |
Response Status Code |
✅ |
Form Data |
✅ |
Request Files |
✅ |
Request Forms and Files |
✅ |
Handling Errors |
✅ |
Path Operation Configuration |
✅ |
JSON Compatible Encoder |
✅ |
Body - Updates |
✅ |
Dependencies |
✅ |
Security |
✅ |
Middleware |
✅ |
CORS (Cross-Origin Resource Sharing) |
❌ |
SQL (Relational) Databases |
✅ |
Bigger Applications - Multiple Files |
❌ |
Background Tasks |
✅ |
Metadata and Docs URLs |
✅ |
Static Files |
✅ |
Testing |
✅ |
Debugging |
Template |
Topic |
---|---|
✅ |
Advanced User Guide - Intro |
✅ |
Path Operation Advanced Configuration |
✅ |
Additional Status Codes |
✅ |
Return a Response Directly |
➡️ |
Custom Response - HTML, Stream, File, others |
❔ |
Additional Responses in OpenAPI |
❔ |
Response Cookies |
❔ |
Response Headers |
❔ |
Response - Change Status Code |
❔ |
Advanced Dependencies |
❔ |
Advanced Security |
❔ |
Using the Request Directly |
❔ |
Using Dataclasses |
❔ |
Advanced Middleware |
❔ |
SQL (Relational) Databases with Peewee |
❔ |
Async SQL (Relational) Databases |
❔ |
NoSQL (Distributed / Big Data) Databases |
❔ |
Sub Applications - Mounts |
❔ |
Behind a Proxy |
❔ |
Templates |
❔ |
GraphQL |
❔ |
WebSockets |
❔ |
Events: startup - shutdown |
❔ |
Custom Request and APIRoute class |
❔ |
Testing WebSockets |
❔ |
Testing Events: startup - shutdown |
❔ |
Testing Dependencies with Overrides |
❔ |
Testing a Database |
❔ |
Async Tests |
❔ |
Settings and Environment Variables |
❔ |
Conditional OpenAPI |
❔ |
Extending OpenAPI |
❔ |
OpenAPI Callbacks |
❔ |
Including WSGI - Flask, Django, others |
Description:
Symbol |
Meaning |
---|---|
➡️ |
Last checkpoint |
❌ |
Not covered |
✅ |
Covered |
❔ |
Next steps |
- Hold your horses, my good fellow nerd!
First of all:
Edit everything, copy-paste whatever you need, refactor the variables, exceptions, and/or whatever you like. There’s your API. Done. A few minutes of work. Over.
But I am a noob like you!
I guess not…
Do you already know something but are having trouble implementing it? Clone it, check how it’s done. Copy and edit. Done
Just started learning? Use it as reading material to fasten and further your learning. Now you won’t need to spend time thinking about what will you do. It’s already done. Practice it!
Are you lazy like me?
Copy and paste → refactor → repeat → API is done. You are getting out early today…
You are welcome!
There’s more info about the project on its page.
You can contact me in case you need help with anything.
I KNOW THAT I’VE BARELY STARTED THE TEST CASES, BUT I’LL DO IT! I’M JUST NOT DOING TDD RIGHT NOW…
Check out my other stories at https://hackernoon.com/u/hbrandao
Best regards!