paint-brush
My Cat, FastAPI and a Templateby@htbrandao
335 reads
335 reads

My Cat, FastAPI and a Template

by Henrique BrandãoAugust 3rd, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

I really liked FastAPI… Love at first sight kind of feeling… I decided to read all its docs, and instead of having study notes, memos, or whatever, I am writing it all as a template. 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.

People Mentioned

Mention Thumbnail
featured image - My Cat, FastAPI and a Template
Henrique Brandão HackerNoon profile picture

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!

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 endpoints
  • Sphinx: Create our project documentation
  • Uvicorn or GUnicorn: Launches our API
  • Pytest: 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:


TL;DR: Lots of stuff! It is a WIP (work in progress), but there’s already lots of stuff for you to rename and start using.


  • From the User Guide:

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

  • From the Advanced User Guide:

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

Yeah, sure, so what?! What will I do with it?

- 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!

    • Blessed are those who are noobs! Good thing that I wrote docstrings for everything, yeah? Yeah?! YEAH!
    • It’s true, everything is documented
    • The docs and source code were exported to HTML format, using Sphinx. So you won’t even need to run the code.
    • If this is your FIRST programming experience, I suggest that you go back to the docs ‘Hello Wolrd’ example. It’s minimal.

















  • Yo man, people already have examples like DEPLOY-WHATEVER-FASTAPI.
    • Yeah, I know. But does it…
      • Authentication?
      • Token validation?
      • Oauth2?
      • Custom exceptions?
      • Custom decorators?
      • Docstrings on everything?
      • Good practices?
      • Pedagogical learning curve?
      • An endpoint that says /BUY_ALL_TUNA_THERE_IS ???

I guess not…

For what it is worth, this is just a 3-way code approach:

  1. Do you already know something but are having trouble implementing it? Clone it, check how it’s done. Copy and edit. Done

  2. 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!

  3. 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!