31 lines
726 B
Python
31 lines
726 B
Python
from typing import Generic, TypeVar
|
|
|
|
from fastapi import Query
|
|
from pydantic import BaseModel
|
|
|
|
from .config import get_settings
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
class Page(BaseModel, Generic[T]):
|
|
total: int
|
|
limit: int
|
|
offset: int
|
|
items: list[T]
|
|
|
|
|
|
class PageParams:
|
|
"""Reusable dependency for `?limit=&offset=` with env-configured caps."""
|
|
|
|
def __init__(
|
|
self,
|
|
limit: int = Query(default=None, ge=1, description="Max rows to return"),
|
|
offset: int = Query(default=0, ge=0, description="Rows to skip"),
|
|
):
|
|
settings = get_settings()
|
|
if limit is None:
|
|
limit = settings.default_limit
|
|
self.limit = min(limit, settings.max_limit)
|
|
self.offset = offset
|