api/app/schemas/response.py
2025-04-09 16:18:52 +08:00

37 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from typing import Any, Generic, Optional, TypeVar
from pydantic import BaseModel, Field
# from pydantic.generics import GenericModel
T = TypeVar('T')
class StandardResponse(BaseModel, Generic[T]):
"""标准API响应格式"""
code: int = Field(200, description="状态码200表示成功其他值表示错误")
message: Optional[str] = Field(None, description="消息,通常在错误时提供")
data: Optional[T] = Field(None, description="响应数据")
class Config:
from_attributes = True
json_schema_extra = {
"example": {
"code": 200,
"message": None,
"data": {}
}
}
class ErrorResponse(BaseModel):
"""错误响应格式"""
code: int = Field(500, description="错误码非200值")
message: str = Field(..., description="错误消息")
data: None = Field(None, description="数据字段错误时为null")
class Config:
from_attributes = True
json_schema_extra = {
"example": {
"code": 500,
"message": "业务处理错误",
"data": None
}
}