DEV Community

Saoni Deb
Saoni Deb

Posted on

FastAPI

Documentation: https://fastapi.tiangolo.com

Source Code: https://github.com/tiangolo/fastapi

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.

Key Features:

  • Fast
  • Fewer bugs
  • Intuitive
  • Easy
  • Short
  • Robust
  • Standards-based

Python Type Code below:

from datetime import date

from pydantic import BaseModel

# Declare a variable as a str
# and get editor support inside the function
def main(user_id: str):
    return user_id


# A Pydantic model
class User(BaseModel):
    id: int
    name: str
    joined: date

Enter fullscreen mode Exit fullscreen mode

This can be used as:

my_user: User = User(id=3, name="John Doe", joined="2018-07-19")

second_user_data = {
    "id": 4,
    "name": "Mary",
    "joined": "2018-11-30",
}

my_second_user: User = User(**second_user_data)

Enter fullscreen mode Exit fullscreen mode

Validation Types:

Validation for most (or all?) Python data types, including:

  • JSON objects (dict).
  • JSON array (list) defining item types.
  • String (str) fields, defining min and max lengths.
  • Numbers (int, float) with min and max values, etc.

Validation for more exotic types, like:

  • URL.
  • Email.
  • UUID.
  • ...and others.

Asynchronous Code
Asynchronous code means that the language has a way to tell the computer / program that at some point in the code, it will have to wait for something else to finish somewhere else.

That "wait for something else" normally refers to I/O operations that are relatively "slow":

  • the data from the client to be sent through the network
  • the data sent by your program to be received by the client through the network
  • the contents of a file in the disk to be read by the system and given to your program
  • the contents your program gave to the system to be written to disk
  • a remote API operation
  • a database operation to finish
  • a database query to return the results

As the execution time is consumed mostly by waiting for I/O operations, they call them "I/O bound" operations.

It's called "asynchronous" because the computer / program doesn't have to be "synchronized" with the slow task, waiting for the exact moment that the task finishes, while doing nothing, to be able to take the task result and continue the work.

For "synchronous" (contrary to "asynchronous") they commonly also use the term "sequential", because the computer / program follows all the steps in sequence before switching to a different task, even if those steps involve waiting.

Difference between Concurrent & Parallelism -
This has been explained quite well using the burger example here:
https://fastapi.tiangolo.com/async/

JWT means "JSON Web Tokens"

Uvicorn is an ASGI (Asynchronous Server Gateway Interface) compatible server that will be used for standing up the backend API.

Top comments (0)