FastAPI could be a modern, high-performance, web framework for building APIs with Python language. Good artificial language frameworks make it easy to provide quality products faster. Great frameworks even make the entire development experience enjoyable. FastAPI could be a new Python web framework that’s powerful and enjoyable to use.FastAPI is an ASGI web framework. What this implies is that different requests don’t necessarily sit up for the others before them to end up doing their tasks. Additional requests can do their task completed in no particular order. On the other hand, the WSGI frameworks process requests in a sequential manner.
ASGI:
ASGI is structured as one, asynchronous callable. It takes a scope, which could be a dict containing details about the precise connection, sends an asynchronous callable that lets the appliance send event messages to the client, and receives an asynchronous callable that lets the application receive event messages from the client.
Does FastAPI need Uvicorn?
The main thing needed to run a FastAPI application in an exceedingly remote server machine is an ASGI server program like Uvicorn.
Using WSGIMiddleware:
Need to import WSGIMiddleware.Then wrap the WSGI (e.g. Flask) app with the middleware. Then mount that beneath a path.
FastAPI Different from other Frameworks:
Let us walk through a journey of building a CRUD application with FAST API and understand how transactions, persistence/database layer, exception handling, and request/response mapping are done.
Building a CRUD Application with FastAPI
Setup:
Start by creating a brand new folder to carry your project called “sql_app”.
Create and activate a new virtual environment:
Next, create the following files and folders for fastapi:
Install the following dependencies:
In the sql_app/main.py ,and define an entry point for running the fastapi application:
In this case, we stated the file to run a Uvicorn server. Before starting the server via the entry point file, create a base route in api.py:
Difference between Database Models & Pydantic Models:
FastAPI suggests calling Pydantic models schemas to assist make the excellence clear. Appropriately, let’s put all our database models into a python models.py file and every one of our Pydantic models into a schemas.py file. In doing this, we’ll also have to update database.py and main.py.
Models.py:
database.py:
schema.py:
FastAPI interactive documentation
A feature that I like about API is its interactive documentation. FastAPI is based on OpenAPI, which is a set of rules that defines how to describe, create and visualize APIs. OpenAPI needs software, which is Swagger, which is the one that allows us to show the API documented. To access this interactive documentation you simply need to go to “/docs”.
Structuring of FastAPI:
By using __init__ everywhere, we are able to access the variables from everywhere in the app, similar to Django.
Models:
It is for your database models, by doing this you’ll import the identical database session or object from v1 and v2.
Schemas:
It is for Pydantic’s Settings Management which is extremely useful, you will be ready to use the identical variables without redeclaring it, to work out how it should somewhat be useful for taking a glance at our documentation for Settings and Environment Variables.
Settings.py:
It is for Pydantic’s Settings Management which is extremely useful, you’ll be able to use the identical variables without redeclaring it, to determine how it may well be useful for you take a look at our documentation for Settings and Environment Variables
Views:
This is optional if you’re visiting render your frontend with Jinja, you’ll have something near MVC pattern.
Core views
- v1_views.py
- v2_views.py
It would look something like this if you wish to feature views.
Tests:
It is good to own your tests inside your backend folder.
APIs:
Create them independently by APIRouter, rather than gathering all of your APIs inside one file.
Logging
It could be a means of tracking events that happen when some software runs. The software’s developer adds logging calls to their code to point out that certain events have occurred. an occasion is described by a descriptive message which might optionally contain variable data (ex. data that’s potentially different for every occurrence of the event). Events even have an importance that the developer ascribes to the event; the importance also can be called the amount or severity.
Github Link: https://github.com/keerthanakumar-08/FastAPI
Conclusion
Modern Python Frameworks and Async capabilities are evolving to support robust implementation of web applications and API endpoints. FAST API is definitely one strong contender. In this blog, we had a quick look at a simple implementation of the FAST API and code structure. Many tech giants like Microsoft, Uber, and Netflix are beginning to adopt. This will result in growing developer maturity and stability of the framework.