Files
YG-Datasets/backend/app/api/response.py
Developer efe5d240ae feat(backend): 添加核心架构模块
- 添加认证模块 (auth.py)
- 添加 CRUD 基础操作 (crud.py)
- 添加异常处理 (exceptions.py)
- 添加日志模块 (logging.py)
- 添加响应格式 (response.py)
- 添加依赖注入 (dependencies.py)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-17 17:28:36 +08:00

76 lines
1.9 KiB
Python

"""
API Response Wrapper
统一 API 响应格式
"""
from datetime import datetime
from typing import Any, Generic, List, Optional, TypeVar
from uuid import UUID
from pydantic import BaseModel, ConfigDict, Field
T = TypeVar("T")
class ApiResponse(BaseModel, Generic[T]):
"""统一 API 响应格式"""
model_config = ConfigDict(from_attributes=True)
success: bool = True
message: str = "Success"
data: Optional[T] = None
error: Optional[dict] = None
timestamp: datetime = Field(default_factory=datetime.utcnow)
@classmethod
def ok(cls, data: T = None, message: str = "Success") -> "ApiResponse[T]":
"""成功响应"""
return cls(success=True, message=message, data=data)
@classmethod
def fail(cls, message: str, error: dict = None) -> "ApiResponse[None]":
"""失败响应"""
return cls(success=False, message=message, error=error)
class PaginatedResponse(BaseModel, Generic[T]):
"""分页响应格式"""
model_config = ConfigDict(from_attributes=True)
success: bool = True
message: str = "Success"
data: List[T] = []
pagination: dict = Field(default_factory=lambda: {
"page": 1,
"page_size": 20,
"total": 0,
"total_pages": 0
})
@classmethod
def ok(
cls,
items: List[T],
page: int = 1,
page_size: int = 20,
total: int = 0
) -> "PaginatedResponse[T]":
"""创建分页响应"""
total_pages = (total + page_size - 1) // page_size if page_size > 0 else 0
return cls(
success=True,
data=items,
pagination={
"page": page,
"page_size": page_size,
"total": total,
"total_pages": total_pages
}
)
class ErrorDetail(BaseModel):
"""错误详情"""
code: str
message: str
details: Optional[dict] = None
field: Optional[str] = None