-
Notifications
You must be signed in to change notification settings - Fork 122
/
default.py
51 lines (37 loc) 路 1.1 KB
/
default.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from __future__ import annotations
from typing import Generic, Sequence, TypeVar
from fastapi import Query
from pydantic import BaseModel, conint
from .bases import AbstractParams, BasePage, RawParams
T = TypeVar("T")
class Params(BaseModel, AbstractParams):
page: int = Query(1, ge=1, description="Page number")
size: int = Query(50, ge=1, le=100, description="Page size")
def to_raw_params(self) -> RawParams:
return RawParams(
limit=self.size,
offset=self.size * (self.page - 1),
)
class Page(BasePage[T], Generic[T]):
page: conint(ge=1) # type: ignore
size: conint(ge=1) # type: ignore
__params_type__ = Params
@classmethod
def create(
cls,
items: Sequence[T],
total: int,
params: AbstractParams,
) -> Page[T]:
if not isinstance(params, Params):
raise ValueError("Page should be used with Params")
return cls(
total=total,
items=items,
page=params.page,
size=params.size,
)
__all__ = [
"Params",
"Page",
]