44 lines
909 B
Python
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,
|
|
)
|