Skip to content

Uttampatel1/Fast_API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

FastAPI Cheat Sheet 🚀

Installation ⚙️

pip install fastapi
pip install uvicorn

Basic Usage 🌐

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def read_root():
    return {"message": "Hello, World! 🌍"}

Request Handling 📥

  • Path Parameters:
@app.get("/items/{item_id}")
async def read_item(item_id: int):
    return {"item_id": item_id}
  • Query Parameters:
@app.get("/items/")
async def read_item(skip: int = 0, limit: int = 10):
    return {"skip": skip, "limit": limit}
  • Request Body:
from pydantic import BaseModel

class Item(BaseModel):
    name: str
    description: str = None

@app.post("/items/")
async def create_item(item: Item):
    return item

Response Handling 📤

  • JSON Response:
@app.get("/items/{item_id}")
async def read_item(item_id: int):
    return {"item_id": item_id}
  • Custom Response Model:
class ItemResponse(BaseModel):
    name: str
    description: str

@app.get("/items/{item_id}", response_model=ItemResponse)
async def read_item(item_id: int):
    return {"name": "Item Name", "description": "Item Description"}

Path Operations 🛤️

  • GET, POST, PUT, DELETE:
@app.get("/get-example")
@app.post("/post-example")
@app.put("/put-example")
@app.delete("/delete-example")

Dependency Injection 🧩

from fastapi import Depends

def get_db():
    db = ...
    return db

@app.get("/items/")
async def read_items(db: Database = Depends(get_db)):
    return db.query(...)

Authentication 🔐

  • OAuth2 with Password Flow:
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")

@app.post("/token")
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
    return {"access_token": form_data.username, "token_type": "bearer"}

@app.get("/secure-data")
async def get_secure_data(token: str = Depends(oauth2_scheme)):
    return {"message": "You have access to secure data"}

Background Tasks 📨

from fastapi import BackgroundTasks

def send_email(background_tasks: BackgroundTasks, email: str):
    # Logic to send email
    pass

@app.post("/send-email")
async def send_email_route(email: str, background_tasks: BackgroundTasks):
    background_tasks.add_task(send_email, email)
    return {"message": "Email sent"}

File Upload 📁

from fastapi import File, UploadFile

@app.post("/upload-file/")
async def upload_file(file: UploadFile = File(...)):
    # Process the uploaded file
    return {"filename": file.filename}

WebSocket 🌐

from fastapi import WebSocket

class WebSocketEndpoint(WebSocket):
    async def on_connect(self, websocket: WebSocket):
        await websocket.accept()
    
    async def on_receive(self, websocket: WebSocket, data: str):
        await websocket.send_text(f"You said: {data}")
    
    async def on_disconnect(self, websocket: WebSocket, close_code: int):
        pass

Exception Handling ❗

from fastapi import HTTPException

@app.get("/items/{item_id}")
async def read_item(item_id: int):
    if item_id < 0:
        raise HTTPException(status_code=400, detail="Item not found")
    return {"item_id": item_id}

😄 For more detailed information, refer to the official documentation.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages