<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pritam Ghosh</title>
    <description>The latest articles on DEV Community by Pritam Ghosh (@pritamdev).</description>
    <link>https://dev.to/pritamdev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1014345%2Fd5690ece-ff0a-44fe-81b5-a6d0203976f9.jpg</url>
      <title>DEV Community: Pritam Ghosh</title>
      <link>https://dev.to/pritamdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pritamdev"/>
    <language>en</language>
    <item>
      <title>An Introduction to FastAPI</title>
      <dc:creator>Pritam Ghosh</dc:creator>
      <pubDate>Wed, 25 Jan 2023 10:11:59 +0000</pubDate>
      <link>https://dev.to/epam_india_python/an-introduction-to-fastapi-5cjh</link>
      <guid>https://dev.to/epam_india_python/an-introduction-to-fastapi-5cjh</guid>
      <description>&lt;p&gt;FastAPI is a modern, very fast (high-performance), web framework for building RESTful APIs. Here we discuss its features and usage briefly by creating a simple Blog app for better understanding.&lt;/p&gt;

&lt;h2&gt;
  
  
  Features:
&lt;/h2&gt;

&lt;p&gt;Some of the big tech companies like Netflix, Microsoft, Uber and so on are also using FastAPI.&lt;br&gt;
The main features are,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;FastAPI supports asynchronous endpoints by default, which allows Python developers to &lt;u&gt;write concurrent code&lt;/u&gt;. This is a huge advantage over Flask, Django.&lt;/li&gt;
&lt;li&gt;Makes use of Python 3.6 type declarations that allows you to specify the type of a variable, Autocompletion works amazingly.&lt;/li&gt;
&lt;li&gt;Security and authentication integrated like - HTTP Basic, OAuth2 (including JWT tokens), pass API keys in Headers or in Query params.&lt;/li&gt;
&lt;li&gt;Supports an extremely &lt;u&gt;powerful Dependency Injection&lt;/u&gt; system, dependencies can be nested (dependencies can have dependencies) as well.&lt;/li&gt;
&lt;li&gt;As it is &lt;u&gt;based on Starlette&lt;/u&gt;, we can use all its features like - Startup and shutdown events, Session, and Cookie, WebSocket, CORS, GZip, Static Files, Streaming responses etc.&lt;/li&gt;
&lt;li&gt;It can validate request data, you can have &lt;u&gt;deeply nested JSON objects&lt;/u&gt; and have them all validated and annotated by Pydantic.&lt;/li&gt;
&lt;li&gt;Automatic data model documentation with JSON Schema (as OpenAPI itself is based on JSON Schema). Supports &lt;u&gt;automatic swagger UI documentation&lt;/u&gt; for APIs.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  FastAPI depends on three major tools:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Starlette:&lt;/strong&gt; A lightweight ASGI (Asynchronous Server Gateway Interface) &lt;u&gt;HTTP web framework&lt;/u&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pydantic:&lt;/strong&gt; A Python library for data validation, request parsing and response model. It has efficient error handling and a custom validation mechanism.&lt;br&gt;
Below is a pydantic Model for response sending. We will use it later with some practical examples.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from pydantic import BaseModel

class PostResponse(BaseModel):
    id: int
    title: str
    body: str | None = None

    class Config:
        orm_mode = True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Uvicorn:&lt;/strong&gt; An ASGI web server implementation. It includes a &lt;u&gt;Gunicorn worker class&lt;/u&gt; allowing you to run &lt;u&gt;ASGI applications&lt;/u&gt;, with all Uvicorn's performance benefits.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def main():
    uvicorn.run(
        app,
        host="0.0.0.0",
        port=8000,
        log_level="info"
    )

if __name__ == "__main__":
    main()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Let’s create a simple Blog app:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Install:&lt;br&gt;
    a. Python 3.6+,&lt;br&gt;
    b. PostgreSQL&lt;br&gt;
    create virtualenv: python -m venv env&lt;br&gt;
    activate virtualenv: source env/bin/activate&lt;br&gt;
    c. Dependencies: pip install fastapi uvicorn[standard] psycopg2 sqlalchemy&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add a main.py file in project folder which will be the entry point of the app and include these lines, here we,&lt;br&gt;
    a. Instantiate the app object of FastAPI&lt;br&gt;
    b. Made the connection with postgresql database&lt;br&gt;
    c. And, included the router of API endpoints&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import uvicorn
from fastapi import FastAPI

from .views import router
from .models import Base

app = FastAPI()
app.include_router(router, prefix="/api")
Base.metadata.create_all(bind=engine)

def main():
    uvicorn.run(
app, host="0.0.0.0", port=8000, log_level="info"
)

if __name__ == "__main__":
    main()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
Add all the database configuration related things in database.py file
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

DATABASE_URL = "postgresql+psycopg2://username:password@localhost:5432/blog"
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
Create a sqlalchemy model name Post with fields(id, title, body) where we store our posts of Blog and a pydantic model (use the above mentioned pydantic model) inherited from pydantic BaseModel in models.py file.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Text

Base = declarative_base()

class Post(Base):
    __tablename__ = "post"

    id = Column(Integer, primary_key=True, index=True)
    title = Column(String)
    body = Column(Text)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
Let’s add an API endpoint to save a Post using the post method in views.py file, this is a simple synchronous call.
To make it an asynchronous call we just need to add an async statement before the def. &lt;u&gt;FastAPI is based on AnyIO which makes it compatible with both Python's standard library asyncio&lt;/u&gt;. Here, we use dependency injection to make the endpoint depend on our database connection which itself is a sqlalchemy session type.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before look into the API just a short comparison &lt;u&gt;on sync &lt;br&gt;
   and async Python&lt;/u&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def sync_function():
    results = blocking_function()
    return results

async def async_function():
    results = await non_blocking_awaitable()
    return results
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, it will be easy to understand the REST endpoints&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session

from .database import get_db
from .models import Post, PostResponse

router = APIRouter()

@router.post("/posts")
def add_post(post_data, db: Session = Depends(get_db)):
    post = Post(title=post_data.title, body=post_data.body)
    db.add(post)
    db.commit()
    return post

@router.post("/posts")
async def add_post(post_data, db: Session = Depends(get_db)):
    ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use Postman or FastAPI swagger to test APIs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
Now, retrieve the Post we just saved in the database using the get method. Here we’re using the pydantic model to structure the response json based.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@router.get("/posts/{post_id}", response_model=PostResponse)
def read_post(post_id, db: Session = Depends(get_db)):
    post_obj = db.query(Post).filter(Post.id == post_id).one_or_none()
    if not post_obj:
        raise HTTPException(status_code=400, detail="Post does not exist with this Id")
    return post_obj
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
Similarly, below are APIs to update and delete Post.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@router.put("/posts/{post_id}", response_model=Post)
def update_post(post_id, post_data, db: Session = Depends(get_db)):
    post_obj = db.query(Post).filter(Post.id == post_id).one_or_none()
    if not post_obj:
        raise HTTPException(status_code=400, detail="Post does not exist with this Id")
    post_obj.title = post_data.title
    post_obj.body = post_data.body
    db.commit()
    return post_obj
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use the patch method for partial updates.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@router.delete("/posts/{post_id}")
def delete_post(post_id, db: Session = Depends(get_db)):
    post_obj = db.query(Post).filter(Post.id == post_id).one_or_none()
    if not post_obj:
        raise HTTPException(status_code=400, detail="Post does not exist with this Id")
    db.delete(post_obj)
    db.commit()
    return {"Post Deleted": True}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Advanced Topics:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
Events Handling:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@app.on_event("startup")
async def startup():
    # connect any resources like database, redis etc...
    resource.connect()

@app.on_event("shutdown")
async def shutdown():
    # disconnect resources
    resource.disconnect()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
Middleware usage: When we need to do some stuff before and after the response like authentication, authorization, process time and many more.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from starlette.middleware.base import BaseHTTPMiddleware
from fastapi.responses import Response

class CustomMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request, call_next) -&amp;gt; Response:
        response = await call_next(request)
        ...

# Include this line in main.py file
app.add_middleware(CustomMiddleware)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
FastAPI comes with powerful security features in the &lt;u&gt;fastapi.security&lt;/u&gt; module,
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from fastapi.security import (
    OAuth2PasswordBearer,
    OAuth2PasswordRequestForm,
    SecurityScopes,
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By now, I have covered the best possible way to use the Framework and its features including async, typing, dependency injection, response model, events handling etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some reference links:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://fastapi.tiangolo.com/" rel="noopener noreferrer"&gt;https://fastapi.tiangolo.com/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://codingnomads.co/blog/python-fastapi-tutorial" rel="noopener noreferrer"&gt;https://codingnomads.co/blog/python-fastapi-tutorial&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;The framework is now fully production-ready, with excellent documentation, support and easy to use. I hope you liked it and Thanks for your time.&lt;br&gt;
HAPPY CODING... &lt;/p&gt;

&lt;h2&gt;
  
  
  Disclaimer:
&lt;/h2&gt;

&lt;p&gt;This is a personal blog. The views and opinions expressed here are only those of the author and do not represent those of any organization or any individual with whom the author may be associated, professionally or personally.&lt;/p&gt;

</description>
      <category>android</category>
      <category>software</category>
      <category>privacy</category>
      <category>mobile</category>
    </item>
  </channel>
</rss>
