Debian Codegraph @

src/globus_sdk/paging/marker.py

raw
1 from __future__ import annotations
2
3 import typing as t
4
5 from .base import PageT, Paginator
6
7
8 class MarkerPaginator(Paginator[PageT]):
9 """
10 A paginator which uses `has_next_page` and `marker` from payloads, sets the `marker`
11 query param to page.
12
13 This is the default method for GCS pagination, so it's very simple.
14 """
15
16 _REQUIRES_METHOD_KWARGS = ("marker",)
17
18 def __init__(
19 self,
20 method: t.Callable[..., t.Any],
21 *,
22 items_key: str | None = None,
23 marker_key: str = "marker",
24 client_args: tuple[t.Any, ...],
25 client_kwargs: dict[str, t.Any],
26 ) -> None:
27 super().__init__(
28 method,
29 items_key=items_key,
30 client_args=client_args,
31 client_kwargs=client_kwargs,
32 )
33 self.marker: str | None = None
34 self.marker_key = marker_key
35
36 def _check_has_next_page(self, page: dict[str, t.Any]) -> bool:
37 return bool(page.get("has_next_page", False))
38
39 def pages(self) -> t.Iterator[PageT]:
40 has_next_page = True
41 while has_next_page:
42 if self.marker:
43 self.client_kwargs["marker"] = self.marker
44 current_page = self.method(*self.client_args, **self.client_kwargs)
45 yield current_page
46 self.marker = current_page.get(self.marker_key)
47 has_next_page = self._check_has_next_page(current_page)
48
49
50 class NullableMarkerPaginator(MarkerPaginator[PageT]):
51 """
52 A paginator which uses a ``marker`` from payloads and sets the ``marker`` query
53 param to page.
54
55 Unlike the base MarkerPaginator, it checks for a null marker to indicate an end to
56 pagination. (vs an explicit has_next_page key)
57 """
58
59 def _check_has_next_page(self, page: dict[str, t.Any]) -> bool:
60 return page.get(self.marker_key) is not None