Skip to content

Instantly share code, notes, and snippets.

@dkgndianko
Last active December 19, 2019 13:47
Show Gist options
  • Save dkgndianko/f2a86009efcdccfcd3c4fd58f1349fbd to your computer and use it in GitHub Desktop.
Save dkgndianko/f2a86009efcdccfcd3c4fd58f1349fbd to your computer and use it in GitHub Desktop.
A type for pydantic paginated elements in response
from pydantic import BaseModel
class _Pagination:
def __init__(self):
self.cache = []
def __getitem__(self, klass):
target_class = None
key = str(klass)
try:
target_class = self.cache[key]
except KeyError:
class PaginatorSchema(BaseModel):
items: List[klass]
previous_page: int = None
current_page: int
next_page: int = None
per_page: int
total_pages: int
total_items: int
target_class = PaginatorSchema
self.cache[key] = target_class
return target_class
Pagination = _Pagination()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment