astrada.pagination
Iterate over every item of a list call, page after page. Hand-written; not generated.
for subaccount in astrada.paginate(client.subaccounts.list_subaccounts, limit=100):
print(subaccount.id)
Works with every list_* method: the card APIs page with cursor or offset and say where
the next page starts in _links.next; the Banking API pages with cursor and says it in
pagination.cursor and pagination.hasMore. The next page is requested only when the loop
reaches it.
1# coding: utf-8 2"""Iterate over every item of a list call, page after page. Hand-written; not generated. 3 4 for subaccount in astrada.paginate(client.subaccounts.list_subaccounts, limit=100): 5 print(subaccount.id) 6 7Works with every ``list_*`` method: the card APIs page with ``cursor`` or ``offset`` and say where 8the next page starts in ``_links.next``; the Banking API pages with ``cursor`` and says it in 9``pagination.cursor`` and ``pagination.hasMore``. The next page is requested only when the loop 10reaches it. 11""" 12 13from typing import Any, Callable, Dict, Iterator, List, Optional, Tuple 14from urllib.parse import parse_qs, urlsplit 15 16from pydantic import BaseModel 17 18 19def _items(page: Any) -> List[Any]: 20 items = getattr(page, "items", None) 21 if isinstance(items, list): 22 return items 23 embedded = getattr(page, "embedded", None) 24 if isinstance(embedded, BaseModel): 25 for name in type(embedded).model_fields: 26 value = getattr(embedded, name) 27 if isinstance(value, list): 28 return value 29 return [] 30 31 32def _next_params(page: Any) -> Optional[Dict[str, Any]]: 33 """The query parameters of the next page, or None on the last page.""" 34 pagination = getattr(page, "pagination", None) 35 if pagination is not None and hasattr(pagination, "has_more"): 36 if pagination.has_more and pagination.cursor: 37 return {"cursor": pagination.cursor} 38 return None 39 links = getattr(page, "links", None) 40 next_link = getattr(links, "next", None) if links is not None else None 41 href = getattr(next_link, "href", None) 42 if not href: 43 return None 44 query = parse_qs(urlsplit(href).query) 45 if "cursor" in query: 46 return {"cursor": query["cursor"][0]} 47 if "offset" in query: 48 return {"offset": int(float(query["offset"][0]))} 49 return None 50 51 52def pages(list_method: Callable[..., Any], *args: Any, **params: Any) -> Iterator[Any]: 53 """Each page a ``list_*`` method returns, following the next-page cursor or offset.""" 54 seen: List[Tuple[Tuple[str, Any], ...]] = [] 55 while True: 56 page = list_method(*args, **params) 57 yield page 58 following = _next_params(page) 59 if not following: 60 return 61 key = tuple(sorted(following.items())) 62 if key in seen: # the API pointed back at a page already read 63 return 64 seen.append(key) 65 params = {**params, **following} 66 67 68def paginate(list_method: Callable[..., Any], *args: Any, **params: Any) -> Iterator[Any]: 69 """Every item a ``list_*`` method returns across all pages. 70 71 :param list_method: a list method, for example ``client.subaccounts.list_subaccounts`` 72 :param args: its positional arguments (for example the subaccount id of a banking list) 73 :param params: its keyword arguments (filters and ``limit``, the page size) 74 """ 75 for page in pages(list_method, *args, **params): 76 yield from _items(page)
def
pages( list_method: Callable[..., Any], *args: Any, **params: Any) -> Iterator[Any]:
53def pages(list_method: Callable[..., Any], *args: Any, **params: Any) -> Iterator[Any]: 54 """Each page a ``list_*`` method returns, following the next-page cursor or offset.""" 55 seen: List[Tuple[Tuple[str, Any], ...]] = [] 56 while True: 57 page = list_method(*args, **params) 58 yield page 59 following = _next_params(page) 60 if not following: 61 return 62 key = tuple(sorted(following.items())) 63 if key in seen: # the API pointed back at a page already read 64 return 65 seen.append(key) 66 params = {**params, **following}
Each page a list_* method returns, following the next-page cursor or offset.
def
paginate( list_method: Callable[..., Any], *args: Any, **params: Any) -> Iterator[Any]:
69def paginate(list_method: Callable[..., Any], *args: Any, **params: Any) -> Iterator[Any]: 70 """Every item a ``list_*`` method returns across all pages. 71 72 :param list_method: a list method, for example ``client.subaccounts.list_subaccounts`` 73 :param args: its positional arguments (for example the subaccount id of a banking list) 74 :param params: its keyword arguments (filters and ``limit``, the page size) 75 """ 76 for page in pages(list_method, *args, **params): 77 yield from _items(page)
Every item a list_* method returns across all pages.
Parameters
- list_method: a list method, for example
client.subaccounts.list_subaccounts - args: its positional arguments (for example the subaccount id of a banking list)
- params: its keyword arguments (filters and
limit, the page size)