Compare commits

..

2 Commits

Author SHA1 Message Date
Rodrigo Rodrigues
6d8e810bb0
Merge ca3dd7c813 into c5916a3b66 2026-01-16 17:10:18 +00:00
Rodrigo Rodrigues
ca3dd7c813 CP-2451 Add paginator iterator helper 2026-01-16 17:09:05 +00:00
3 changed files with 9 additions and 103 deletions

View File

@ -18,6 +18,6 @@ from . import exceptions
from .api_client import ApiClient from .api_client import ApiClient
from .api_response import ApiResponse from .api_response import ApiResponse
from .configuration import Configuration from .configuration import Configuration
from .pagination_iterator import PaginatorIterator from .iterable import PaginatorIterator
import os.path import os.path

View File

@ -16,89 +16,61 @@
from __future__ import annotations from __future__ import annotations
from typing import Any, Callable, Iterator, Mapping, Optional, TypeVar, Generic from typing import Any, Callable, Iterator, Mapping, Optional
from urllib.parse import parse_qs, urlparse from urllib.parse import parse_qs, urlparse
from typing_extensions import ParamSpec
P = ParamSpec("P") class PaginatorIterator:
R = TypeVar("R") """Iterate over paginated responses for a method that accepts a cursor."""
class PaginatorIterator(Generic[P, R]):
"""Iterate over cursor-paginated responses.
Calls ``method`` repeatedly, passing a cursor parameter between calls.
The next cursor is derived from ``response.data.links`` or ``response.data._links``
(or mapping equivalents), supporting these link formats:
- a direct ``href`` string
- a mapping with a ``href`` key
- an object with a ``href`` attribute
Iteration stops when no next cursor is found or the cursor repeats.
"""
def __init__( def __init__(
self, self,
method: Callable[P, R], method: Callable[..., Any],
*, *,
cursor_param: str = "cursor", cursor_param: str = "cursor",
**params: P.kwargs, **params: Any,
) -> None: ) -> None:
self._method = method self._method = method
self._cursor_param = cursor_param self._cursor_param = cursor_param
self._params: dict[str, Any] = dict(params) self._params = dict(params)
def __iter__(self) -> Iterator[R]: def __iter__(self) -> Iterator[Any]:
params = dict(self._params) params = dict(self._params)
last_cursor = params.get(self._cursor_param) last_cursor = params.get(self._cursor_param)
while True: while True:
response = self._method(**params) response = self._method(**params)
yield response yield response
next_cursor = self._next_cursor_from_response(response) next_cursor = self._next_cursor_from_response(response)
if not next_cursor or next_cursor == last_cursor: if not next_cursor or next_cursor == last_cursor:
break break
params[self._cursor_param] = next_cursor params[self._cursor_param] = next_cursor
last_cursor = next_cursor last_cursor = next_cursor
def _next_cursor_from_response(self, response: Any) -> Optional[str]: def _next_cursor_from_response(self, response: Any) -> Optional[str]:
data = getattr(response, "data", response) data = getattr(response, "data", response)
links = getattr(data, "links", None) links = getattr(data, "links", None)
if links is None: if links is None:
links = getattr(data, "_links", None) links = getattr(data, "_links", None)
if links is None and isinstance(data, Mapping): if links is None and isinstance(data, Mapping):
links = data.get("_links") or data.get("links") links = data.get("_links") or data.get("links")
if links is None: if links is None:
return None return None
next_link = getattr(links, "next", None) next_link = getattr(links, "next", None)
if next_link is None and isinstance(links, Mapping): if next_link is None and isinstance(links, Mapping):
next_link = links.get("next") next_link = links.get("next")
if next_link is None: if next_link is None:
return None return None
if isinstance(next_link, str): if isinstance(next_link, str):
href = next_link href = next_link
elif isinstance(next_link, Mapping): elif isinstance(next_link, Mapping):
href = next_link.get("href") href = next_link.get("href")
else: else:
href = getattr(next_link, "href", None) href = getattr(next_link, "href", None)
if not href: if not href:
return None return None
parsed = urlparse(href) parsed = urlparse(href)
query_params = parse_qs(parsed.query) query_params = parse_qs(parsed.query)
cursor_values = query_params.get(self._cursor_param) cursor_values = query_params.get(self._cursor_param)
if cursor_values: if cursor_values:
return cursor_values[0] return cursor_values[0]
return None return href

View File

@ -1,66 +0,0 @@
from types import SimpleNamespace
from thousandeyes_sdk.core.iterable import PaginatorIterator
def test_iterator_uses_cursor_from_next_href():
calls = []
def method(**params):
calls.append(params.copy())
if params.get("cursor") is None:
links = SimpleNamespace(next="https://example.com/items?cursor=abc")
else:
links = SimpleNamespace(next=None)
data = SimpleNamespace(links=links)
return SimpleNamespace(data=data)
responses = list(PaginatorIterator(method))
assert len(responses) == 2
assert calls == [{}, {"cursor": "abc"}]
def test_iterator_reads_cursor_from_links_mapping():
calls = []
def method(**params):
calls.append(params.copy())
if params.get("pageCursor") is None:
data = {"_links": {"next": {"href": "https://example.com?foo=1&pageCursor=xyz"}}}
else:
data = {"_links": {"next": None}}
return SimpleNamespace(data=data)
list(PaginatorIterator(method, cursor_param="pageCursor"))
assert calls == [{}, {"pageCursor": "xyz"}]
def test_iterator_stops_when_no_cursor_param_present():
calls = []
def method(**params):
calls.append(params.copy())
if params.get("cursor") is None:
data = {"links": {"next": "/next/page"}}
else:
data = {"links": {"next": None}}
return SimpleNamespace(data=data)
list(PaginatorIterator(method))
assert calls == [{}]
def test_iterator_stops_on_repeated_cursor():
calls = []
def method(**params):
calls.append(params.copy())
data = {"links": {"next": "https://example.com?cursor=same"}}
return SimpleNamespace(data=data)
list(PaginatorIterator(method, cursor="same"))
assert calls == [{"cursor": "same"}]