-
Notifications
You must be signed in to change notification settings - Fork 421
/
pager.py
71 lines (55 loc) · 2.62 KB
/
pager.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import copy
from functools import partial
from typing import Iterable, Iterator, List, Optional, Protocol, Tuple, TypeVar, Union, runtime_checkable
from tableauserverclient.models.pagination_item import PaginationItem
from tableauserverclient.server.request_options import RequestOptions
T = TypeVar("T")
@runtime_checkable
class Endpoint(Protocol[T]):
def get(self, req_options: Optional[RequestOptions]) -> Tuple[List[T], PaginationItem]:
...
@runtime_checkable
class CallableEndpoint(Protocol[T]):
def __call__(self, __req_options: Optional[RequestOptions], **kwargs) -> Tuple[List[T], PaginationItem]:
...
class Pager(Iterable[T]):
"""
Generator that takes an endpoint (top level endpoints with `.get)` and lazily loads items from Server.
Supports all `RequestOptions` including starting on any page. Also used by models to load sub-models
(users in a group, views in a workbook, etc) by passing a different endpoint.
Will loop over anything that returns (List[ModelItem], PaginationItem).
"""
def __init__(
self,
endpoint: Union[CallableEndpoint[T], Endpoint[T]],
request_opts: Optional[RequestOptions] = None,
**kwargs,
) -> None:
if isinstance(endpoint, Endpoint):
# The simpliest case is to take an Endpoint and call its get
endpoint = partial(endpoint.get, **kwargs)
self._endpoint = endpoint
elif isinstance(endpoint, CallableEndpoint):
# but if they pass a callable then use that instead (used internally)
endpoint = partial(endpoint, **kwargs)
self._endpoint = endpoint
else:
# Didn't get something we can page over
raise ValueError("Pager needs a server endpoint to page through.")
self._options = request_opts or RequestOptions()
def __iter__(self) -> Iterator[T]:
options = copy.deepcopy(self._options)
while True:
# Fetch the first page
current_item_list, pagination_item = self._endpoint(options)
if pagination_item.total_available is None:
# This endpoint does not support pagination, drain the list and return
yield from current_item_list
return
yield from current_item_list
if pagination_item.page_size * pagination_item.page_number >= pagination_item.total_available:
# Last page, exit
return
# Update the options to fetch the next page
options.pagenumber = pagination_item.page_number + 1
options.pagesize = pagination_item.page_size