Features
Link : https://www.youtube.com/watch?v=H9Blu0kWdZE&list=PLK8U0kF0E_D6l19LhOGWhVZ3sQ6ujJKq_
1. Automatic docs :
Fast API provides a swagger UI which shows what are the routes we have created and what are the
-- Swagger UI
-- ReDoc
Fast API : https://fastapi.tiangolo.com/
Core FastAPI Terminologies
1. App Instance
from fastapi import FastAPI
app = FastAPI()
2. Path Operation (Route)
@app.get("/users")
/users)
GET, POST, etc.)
👉 Meaning:
“When a GET request comes to
/users, run this function.”
3. HTTP Methods
-
GET→ Read data -
POST→ Create data -
PUT→ Update (full) -
PATCH→ Update (partial) -
DELETE→ Remove
4. Path Parameters
@app.get("/users/{user_id}")
def get_user(user_id: int):
/users/10 → user_id = 105. Query Parameters
@app.get("/users")
def get_users(limit: int = 10):
? in URL
/users?limit=56. Request Body
from pydantic import BaseModel
class User(BaseModel):
name: str
age: int
@app.post("/users")
def create_user(user: User):
7. Pydantic Models
-
Used for:
- Validation
- Serialization
- Ensures correct data types\
8. Response Model
@app.get("/users", response_model=User)
9. Dependency Injection
from fastapi import Depends
def get_db():
return "DB Connection"
@app.get("/")
def read(db = Depends(get_db)):
10. Middleware
- Runs before/after every request
-
Used for:
- Logging
- Authentication
- CORS
11. Status Codes
from fastapi import status
@app.get("/", status_code=status.HTTP_200_OK)
12. Exception Handling
from fastapi import HTTPException
raise HTTPException(status_code=404, detail="Not found")
13. APIRouter
from fastapi import APIRouter
router = APIRouter()
Used to split large apps into modules
14. OpenAPI & Docs
-
Auto-generated docs:
-
/docs→ Swagger UI -
/redoc→ ReDoc
-
15. Async / Await
@app.get("/")
async def read():
Enables high performance (non-blocking)
🧠 Simple Mental Model
Think of FastAPI like this:
URL → Route → Function → Validate Input → Process → Return Response
Comments
Post a Comment