Files
yaoyaoai/app/schemas/common.py
fengchuanhn@gmail.com 3db327d93b 22
2026-05-17 12:54:13 +08:00

44 lines
909 B
Python

from typing import Generic, TypeVar
from pydantic import BaseModel, ConfigDict
T = TypeVar("T")
class ApiResponse(BaseModel, Generic[T]):
"""与 aiclient 前端约定的统一响应结构。"""
model_config = ConfigDict(from_attributes=True)
ok: bool
message: str = ""
data: T | None = None
class PaginatedData(BaseModel, Generic[T]):
"""分页列表载荷。"""
items: list[T]
total: int
page: int
page_size: int
total_pages: int
@classmethod
def build(
cls,
items: list[T],
*,
total: int,
page: int,
page_size: int,
) -> "PaginatedData[T]":
total_pages = (total + page_size - 1) // page_size if total > 0 else 0
return cls(
items=items,
total=total,
page=page,
page_size=page_size,
total_pages=total_pages,
)