-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathsearch_utils.py
31 lines (24 loc) · 950 Bytes
/
search_utils.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
import base64
from typing import Any, AsyncIterator, Callable, TypeVar
T = TypeVar('T')
def offset_to_page_id(offset: int, has_next: bool) -> str | None:
if not has_next:
return None
next_page_id = base64.b64encode(str(offset).encode()).decode()
return next_page_id
def page_id_to_offset(page_id: str | None) -> int:
if not page_id:
return 0
offset = int(base64.b64decode(page_id).decode())
return offset
async def iterate(fn: Callable[..., Any], **kwargs: Any) -> AsyncIterator[Any]:
"""Iterate over paged result sets. Assumes that the results sets contain an array of result objects, and a next_page_id"""
kwargs = {**kwargs}
kwargs['page_id'] = None
while True:
result_set = await fn(**kwargs)
for result in result_set.results:
yield result
if result_set.next_page_id is None:
return
kwargs['page_id'] = result_set.next_page_id