fastapi-router-py
microsoft/skills
遵循既定模式,创建包含 CRUD 操作、身份验证依赖项和正确响应模型的 FastAPI 路由器。
...展开全部
10
更新时间 2026-09-12
FastAPI 路由器
遵循既定模式创建 FastAPI 路由器,并配置正确的身份验证、响应模型和 HTTP 状态码。
快速入门
从 assets/template.py 复制模板,并替换占位符:
{{ResourceName}}→ PascalCase 命名(例如,Project){{resource_name}}→ 蛇形命名法名称(例如,project){{resource_plural}}→ 复数形式(例如,projects)
身份验证模式
# Optional auth - returns None if not authenticated
current_user: Optional[User] = Depends(get_current_user)
# Required auth - raises 401 if not authenticated
current_user: User = Depends(get_current_user_required)
响应模型
@router.get("/items/{item_id}", response_model=Item)
async def get_item(item_id: str) -> Item:
...
@router.get("/items", response_model=list[Item])
async def list_items() -> list[Item]:
...
HTTP 状态码
@router.post("/items", status_code=status.HTTP_201_CREATED)
@router.delete("/items/{id}", status_code=status.HTTP_204_NO_CONTENT)
集成步骤
- 在
src/backend/app/routers/ - 挂载到
src/backend/app/main.py - 创建相应的 Pydantic 模型
- 如有需要,创建服务层
- 添加前端 API 函数
最佳实践
- 根据是否调用异步 I/O,为每个端点选择
def或async def;切勿在同一个处理程序中混合使用同步和异步阻塞调用。 - 在
lifespan中管理长生命周期资源(数据库连接池、HTTP 客户端),并通过Depends进行注入;使用with/async with用于按请求管理的资源。
在 GitHub 上查看
---
name: fastapi-router-py
description: Create FastAPI routers with CRUD operations, authentication dependencies, and proper response models following established patterns.
license: MIT
---
# FastAPI Router
Create FastAPI routers following established patterns with proper authentication, response models, and HTTP status codes.
## Quick Start
Copy the template from [assets/template.py](assets/template.py) and replace placeholders:
- `{{ResourceName}}` → PascalCase name (e.g., `Project`)
- `{{resource_name}}` → snake_case name (e.g., `project`)
- `{{resource_plural}}` → plural form (e.g., `projects`)
## Authentication Patterns
```python
# Optional auth - returns None if not authenticated
current_user: Optional[User] = Depends(get_current_user)
# Required auth - raises 401 if not authenticated
current_user: User = Depends(get_current_user_required)
```
## Response Models
```python
@router.get("/items/{item_id}", response_model=Item)
async def get_item(item_id: str) -> Item:
...
@router.get("/items", response_model=list[Item])
async def list_items() -> list[Item]:
...
```
## HTTP Status Codes
```python
@router.post("/items", status_code=status.HTTP_201_CREATED)
@router.delete("/items/{id}", status_code=status.HTTP_204_NO_CONTENT)
```
## Integration Steps
1. Create router in `src/backend/app/routers/`
2. Mount in `src/backend/app/main.py`
3. Create corresponding Pydantic models
4. Create service layer if needed
5. Add frontend API functions
## Best Practices
1. **Pick `def` or `async def` per endpoint based on whether you call async I/O;** do not mix sync and async blocking calls in one handler.
2. **Manage long-lived resources (DB pools, HTTP clients) in `lifespan` and inject via `Depends`;** use `with`/`async with` for per-request resources.
所有文件
0 个文件相关技能
algorithmic-art
更新时间 2026-08-27
receiving-code-review
更新时间 2026-09-03
tech-debt-tracker
更新时间 2026-08-29
deprecation-and-migration
更新时间 2026-09-03





首页
