| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import typing as t |
| 4 |
|
| 5 |
from globus_sdk.response import GlobusHTTPResponse |
| 6 |
|
| 7 |
from .base import Paginator |
| 8 |
|
| 9 |
C = t.TypeVar("C", bound=t.Callable[..., GlobusHTTPResponse]) |
| 10 |
|
| 11 |
|
| 12 |
class PaginatorTable: |
| 13 |
""" |
| 14 |
A PaginatorTable maps multiple methods of an SDK client to paginated variants. |
| 15 |
Given a method, client.foo annotated with the `has_paginator` decorator, the table |
| 16 |
will gain a function attribute `foo` (name matching is automatic) which returns a |
| 17 |
Paginator. |
| 18 |
|
| 19 |
Clients automatically build and attach paginator tables under the ``paginated`` |
| 20 |
attribute. |
| 21 |
That is, if `client` has two methods `foo` and `bar` which are marked as paginated, |
| 22 |
that will let us call |
| 23 |
|
| 24 |
>>> client.paginated.foo() |
| 25 |
>>> client.paginated.bar() |
| 26 |
|
| 27 |
where ``client.paginated`` is a ``PaginatorTable``. |
| 28 |
|
| 29 |
Paginators are iterables of response pages, so ultimate usage is like so: |
| 30 |
|
| 31 |
>>> paginator = client.paginated.foo() # returns a paginator |
| 32 |
>>> for page in paginator: # a paginator is an iterable of pages (response objects) |
| 33 |
>>> print(json.dumps(page.data)) # you can handle each response object in turn |
| 34 |
|
| 35 |
A ``PaginatorTable`` is built automatically as part of client instantiation. |
| 36 |
Creation of ``PaginatorTable`` objects is considered a private API. |
| 37 |
""" |
| 38 |
|
| 39 |
def __init__(self, client: t.Any) -> None: |
| 40 |
self._client = client |
| 41 |
# _bindings is a lazily loaded table of names -> callables which |
| 42 |
# return paginators |
| 43 |
self._bindings: dict[str, t.Callable[..., Paginator[t.Any]]] = {} |
| 44 |
|
| 45 |
def _add_binding( |
| 46 |
self, methodname: str, bound_method: t.Callable[..., t.Any] |
| 47 |
) -> None: |
| 48 |
self._bindings[methodname] = Paginator.wrap(bound_method) |
| 49 |
|
| 50 |
def __getattr__(self, attrname: str) -> t.Callable[..., Paginator[t.Any]]: |
| 51 |
if attrname not in self._bindings: |
| 52 |
# this could raise AttributeError -- in which case, let it! |
| 53 |
method = getattr(self._client, attrname) |
| 54 |
try: |
| 55 |
self._bindings[attrname] = Paginator.wrap(method) |
| 56 |
# ValueError is raised if the method being wrapped is not paginated |
| 57 |
except ValueError as e: |
| 58 |
raise AttributeError(f"'{attrname}' is not a paginated method") from e |
| 59 |
|
| 60 |
return self._bindings[attrname] |
| 61 |
|
| 62 |
# customize pickling methods to ensure that the object is pickle-safe |
| 63 |
|
| 64 |
def __getstate__(self) -> dict[str, t.Any]: |
| 65 |
# when pickling, drop any bound methods |
| 66 |
d = dict(self.__dict__) # copy |
| 67 |
d["_bindings"] = {} |
| 68 |
return d |
| 69 |
|
| 70 |
# custom __setstate__ to avoid an infinite loop on `getattr` before `_bindings` is |
| 71 |
# populated |
| 72 |
# see: https://docs.python.org/3/library/pickle.html#object.__setstate__ |
| 73 |
def __setstate__(self, d: dict[str, t.Any]) -> None: |
| 74 |
self.__dict__.update(d) |