
使用方式
关于
使用 FastAPI 和 Pydantic V2 构建高性能异步 Python API。用于创建 REST 接口、定义 Pydantic 模型、实现认证流程、配置异步 SQLAlchemy 数据库操作、添加 JWT 认证、构建 WebSocket 接口或生成 OpenAPI 文档。
FastAPI 专家
精通异步 Python、Pydantic V2 和使用 FastAPI 进行生产级 API 开发。
何时使用此技能
- 使用 FastAPI 构建 REST API
- 实现 Pydantic V2 验证模式
- 设置异步数据库操作
- 实现 JWT 认证/授权
- 创建 WebSocket 端点
- 优化 API 性能
核心工作流
- 分析需求 — 确定端点、数据模型、认证需求
- 设计模式 — 创建 Pydantic V2 模型用于验证
- 实现 — 编写带有正确依赖注入的异步端点
- 安全 — 添加认证、授权、速率限制
- 测试 — 使用 pytest 和 httpx 编写异步测试;每组端点完成后运行
pytest并在/docs验证 OpenAPI 文档
每步之后检查点: 确认模式正确验证、端点返回预期的 HTTP 状态码、
/docs反映预期的 API 接口,然后再继续。
最小完整示例
模式 + 端点 + 依赖注入的完整单元:
# schemas.py
from pydantic import BaseModel, EmailStr, field_validator, model_config
class UserCreate(BaseModel):
model_config = model_config(str_strip_whitespace=True)
email: EmailStr
password: str
name: str | None = None
@field_validator("password")
@classmethod
def password_strength(cls, v: str) -> str:
if len(v) < 8:
raise ValueError("Password must be at least 8 characters")
return v
class UserResponse(BaseModel):
model_config = model_config(from_attributes=True)
id: int
email: EmailStr
name: str | None = None
# routers/users.py
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession
from typing import Annotated
from app.database import get_db
from app.schemas import UserCreate, UserResponse
from app import crud
router = APIRouter(prefix="/users", tags=["users"])
DbDep = Annotated[AsyncSession, Depends(get_db)]
@router.post("/", response_model=UserResponse, status_code=status.HTTP_201_CREATED)
async def create_user(payload: UserCreate, db: DbDep) -> UserResponse:
existing = await crud.get_user_by_email(db, payload.email)
if existing:
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Email already registered")
return await crud.create_user(db, payload)
# crud.py
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models import User
from app.schemas import UserCreate
from app.security import hash_password
async def get_user_by_email(db: AsyncSession, email: str) -> User | None:
result = await db.execute(select(User).where(User.email == email))
return result.scalar_one_or_none()
async def create_user(db: AsyncSession, payload: UserCreate) -> User:
user = User(email=payload.email, hashed_password=hash_password(payload.password), name=payload.name)
db.add(user)
await db.commit()
await db.refresh(user)
return user
JWT 认证代码片段
# security.py
from datetime import datetime, timedelta, timezone
from jose import JWTError, jwt
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from typing import Annotated
SECRET_KEY = "read-from-env" # use os.environ / settings
ALGORITHM = "HS256"
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/token")
def create_access_token(subject: str, expires_delta: timedelta = timedelta(minutes=30)) -> str:
payload = {"sub": subject, "exp": datetime.now(timezone.utc) + expires_delta}
return jwt.encode(payload, SECRET_KEY, algorithm=ALGORITHM)
async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]) -> str:
try:
data = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
subject: str | None = data.get("sub")
if subject is None:
raise ValueError
return subject
except (JWTError, ValueError):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid credentials")
CurrentUser = Annotated[str, Depends(get_current_user)]
参考指南
根据上下文加载详细指导:
| 主题 | 参考文件 | 加载时机 |
|------|----------|----------|
| Pydantic V2 | references/pydantic-v2.md | 创建模式、验证、model_config |
| SQLAlchemy | references/async-sqlalchemy.md | 异步数据库、模型、CRUD 操作 |
| 端点 | references/endpoints-routing.md | APIRouter、依赖、路由 |
| 认证 | references/authentication.md | JWT、OAuth2、get_current_user |
| 测试 | references/testing-async.md | pytest-asyncio、httpx、fixtures |
| Django 迁移 | references/migration-from-django.md | 从 Django/DRF 迁移到 FastAPI |
约束
必须做到
- 处处使用类型提示(FastAPI 要求)
- 使用 Pydantic V2 语法(
field_validator、model_config等)
兼容工具
Claude CodeCursor
标签
后端开发

