mirror of
http://88.130.71.182:3000/BlitTech/badoHair_be.git
synced 2026-06-12 23:23:22 +00:00
Initial Commit
This commit is contained in:
42
app/core/responses.py
Normal file
42
app/core/responses.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from typing import Any, TypeVar, Generic
|
||||
from pydantic import BaseModel
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class ErrorDetail(BaseModel):
|
||||
code: str
|
||||
message: str
|
||||
details: Any = None
|
||||
|
||||
|
||||
class APIResponse(BaseModel, Generic[T]):
|
||||
success: bool
|
||||
data: T | None = None
|
||||
error: ErrorDetail | None = None
|
||||
|
||||
|
||||
class PaginationMeta(BaseModel):
|
||||
total: int
|
||||
page: int
|
||||
per_page: int
|
||||
pages: int
|
||||
|
||||
|
||||
class PaginatedAPIResponse(BaseModel, Generic[T]):
|
||||
success: bool = True
|
||||
data: list[T] = []
|
||||
meta: PaginationMeta
|
||||
|
||||
|
||||
def ok(data: Any = None) -> dict:
|
||||
return {"success": True, "data": data}
|
||||
|
||||
|
||||
def paginated(data: list, total: int, page: int, per_page: int) -> dict:
|
||||
pages = max(1, (total + per_page - 1) // per_page)
|
||||
return {
|
||||
"success": True,
|
||||
"data": data,
|
||||
"meta": {"total": total, "page": page, "per_page": per_page, "pages": pages},
|
||||
}
|
||||
Reference in New Issue
Block a user