PaginationManager.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. from typing import Optional, Dict, Any
  3. from .PaginationLinks import PaginationLinks
  4. from .PaginationMetadata import PaginationMetadata
  5. from .ResponseMeta import ResponseMeta
  6. class PaginationManager:
  7. def __init__(self, limit: int) -> None:
  8. self.limit = limit # The limit of items per page
  9. self.meta = None # type: Optional[ResponseMeta] # The metadata of the paginated response
  10. self.links = None # type: Optional[PaginationLinks] # The pagination-related links
  11. def setResponseMeta(self, meta: Optional[Dict[str, Any]]) -> None:
  12. self.meta = None
  13. if meta:
  14. page = None
  15. if "page" in meta:
  16. page = PaginationMetadata(**meta["page"])
  17. self.meta = ResponseMeta(page)
  18. def setLinks(self, links: Optional[Dict[str, str]]) -> None:
  19. self.links = PaginationLinks(**links) if links else None
  20. def setLimit(self, new_limit: int) -> None:
  21. """
  22. Sets the limit of items per page.
  23. :param new_limit: The new limit of items per page
  24. """
  25. self.limit = new_limit
  26. self.reset()
  27. def reset(self) -> None:
  28. """
  29. Sets the metadata and links to None.
  30. """
  31. self.meta = None
  32. self.links = None