ds_protocol_http_py_lib.dataset.pagination ========================================== .. py:module:: ds_protocol_http_py_lib.dataset.pagination .. autoapi-nested-parse:: **File:** ``__init__.py`` **Region:** ``ds_protocol_http_py_lib/dataset/pagination`` HTTP dataset pagination: settings, enums, strategy registry, and Paginate composer. Submodules ---------- .. toctree:: :maxdepth: 1 /autoapi/ds_protocol_http_py_lib/dataset/pagination/enums/index /autoapi/ds_protocol_http_py_lib/dataset/pagination/paginate/index /autoapi/ds_protocol_http_py_lib/dataset/pagination/registry/index /autoapi/ds_protocol_http_py_lib/dataset/pagination/settings/index /autoapi/ds_protocol_http_py_lib/dataset/pagination/strategies/index Classes ------- .. autoapisummary:: ds_protocol_http_py_lib.dataset.pagination.ExtractSource ds_protocol_http_py_lib.dataset.pagination.PaginationStrategy ds_protocol_http_py_lib.dataset.pagination.Paginate ds_protocol_http_py_lib.dataset.pagination.CursorPaginationSettings ds_protocol_http_py_lib.dataset.pagination.OffsetPaginationSettings ds_protocol_http_py_lib.dataset.pagination.PageNumberPaginationSettings ds_protocol_http_py_lib.dataset.pagination.PaginationSettings ds_protocol_http_py_lib.dataset.pagination.CursorPaginationStrategy ds_protocol_http_py_lib.dataset.pagination.OffsetPaginationStrategy ds_protocol_http_py_lib.dataset.pagination.PageNumberPaginationStrategy ds_protocol_http_py_lib.dataset.pagination.PaginationStrategyHandler Functions --------- .. autoapisummary:: ds_protocol_http_py_lib.dataset.pagination.get_strategy ds_protocol_http_py_lib.dataset.pagination.register Package Contents ---------------- .. py:class:: ExtractSource Bases: :py:obj:`enum.StrEnum` Where a pagination value is read from the response. .. py:attribute:: BODY :value: 'body' JSON response body path. .. py:attribute:: HEADER :value: 'header' Response header name. .. py:class:: PaginationStrategy Bases: :py:obj:`enum.StrEnum` Declared pagination mechanisms supported by HttpDataset. .. py:attribute:: OFFSET :value: 'offset' Offset / limit pagination. .. py:attribute:: PAGE_NUMBER :value: 'page_number' Page ordinal / page size pagination. .. py:attribute:: CURSOR :value: 'cursor' Opaque cursor / token pagination. .. py:class:: Paginate(strategy: ds_protocol_http_py_lib.dataset.pagination.strategies.base.PaginationStrategyHandler[Any], pagination: ds_protocol_http_py_lib.dataset.pagination.settings.PaginationSettings, incremental_rules: ds_protocol_http_py_lib.dataset.incremental.rules.IncrementalRules, incremental: ds_protocol_http_py_lib.dataset.incremental.settings.IncrementalSettings | None) Composer for a paginated ``HttpDataset.read``. Resolves a registered page strategy and optional incremental ruleset, then runs prepare → drain → commit. Strategy hooks supply page variation only; this class owns HTTP, checkpoint pagination slice, and error mapping. .. py:attribute:: strategy .. py:attribute:: pagination .. py:attribute:: incremental_rules .. py:attribute:: incremental .. py:method:: from_dataset(dataset: ds_protocol_http_py_lib.dataset.http.HttpDataset[Any, Any]) -> Paginate :classmethod: Build from ``dataset.settings.read`` and the strategy / rules registries. .. py:method:: run(dataset: ds_protocol_http_py_lib.dataset.http.HttpDataset[Any, Any]) -> None Prepare → drain → assign ``output`` / checkpoint. On full success: clear ``checkpoint["pagination"]``, then incremental ``commit`` if configured. On failure: assign partial ``output``, keep mid-run pagination resume, hold watermark. .. py:method:: drain(dataset: ds_protocol_http_py_lib.dataset.http.HttpDataset[Any, Any], request: ds_protocol_http_py_lib.utils.http.request.RequestSnapshot, frames: list[pandas.DataFrame] | None = None) -> list[pandas.DataFrame] Page loop via strategy hooks; write mid-run ``checkpoint["pagination"]``. Appends page frames to ``frames`` (created if omitted) so callers retain partial output when this method raises mid-drain. Each frame is ``dataset.deserializer(response.content)`` — the API body the caller configured the deserializer for. ``items_path`` is paging control only. An empty body is an empty frame and does not call the deserializer. :raises ValueError: If ``dataset.deserializer`` is missing. :raises ReadError: If ``max_pages`` is exceeded without a natural stop. .. py:method:: _snapshot(dataset: ds_protocol_http_py_lib.dataset.http.HttpDataset[Any, Any]) -> ds_protocol_http_py_lib.utils.http.request.RequestSnapshot Build an isolated request copy from dataset settings. .. py:method:: _validate_checkpoint_strategy(checkpoint_slice: dict[str, Any] | None, expected: str) -> None :staticmethod: .. py:method:: _concat_frames(frames: list[pandas.DataFrame]) -> pandas.DataFrame :staticmethod: Concatenate page frames, or return an empty frame when none were fetched. .. py:function:: get_strategy(strategy: ds_protocol_http_py_lib.dataset.pagination.enums.PaginationStrategy) -> ds_protocol_http_py_lib.dataset.pagination.strategies.base.PaginationStrategyHandler[Any] Resolve a registered strategy handler. :raises KeyError: If no handler is registered for ``strategy``. .. py:function:: register(strategy: ds_protocol_http_py_lib.dataset.pagination.enums.PaginationStrategy) -> collections.abc.Callable[[T], T] Class decorator that registers a strategy handler. Adding a new strategy is backward compatible: register a new enum member and handler module without changing Paginate. .. py:class:: CursorPaginationSettings Bases: :py:obj:`ds_common_serde_py_lib.Serializable` Configuration for opaque cursor / token pagination. .. py:attribute:: cursor_path :type: str Body path or header name where the next cursor is read. .. py:attribute:: cursor_source :type: ds_protocol_http_py_lib.dataset.pagination.enums.ExtractSource Whether the cursor is extracted from the response body or a header. .. py:attribute:: cursor_param :type: str :value: 'cursor' Parameter / header / body key used to inject the cursor. .. py:attribute:: cursor_location :type: ds_protocol_http_py_lib.utils.http.request.InjectLocation Where the cursor is injected on the next request. .. py:attribute:: page_size_param :type: str | None :value: None Optional page-size query parameter name (always injected as QUERY). .. py:attribute:: page_size :type: int | None :value: None Optional requested page size (sent with ``page_size_param``). .. py:class:: OffsetPaginationSettings Bases: :py:obj:`ds_common_serde_py_lib.Serializable` Configuration for offset / limit pagination. .. py:attribute:: offset_param :type: str :value: 'offset' Query/body/header parameter name for the offset. .. py:attribute:: limit_param :type: str :value: 'limit' Query/body/header parameter name for the page size. .. py:attribute:: page_size :type: int :value: 100 Number of records requested per page. .. py:attribute:: initial_offset :type: int :value: 0 Offset used for the first page when no checkpoint exists. .. py:attribute:: total_path :type: str | None :value: None Optional body path to a total record count. When the path resolves, a non-empty page stops once ``offset + len(items) >= total``. The next offset advances by the rows returned, and the request still asks for ``page_size``. An empty page stops immediately. A short page ends the read only when this path is unset or missing. .. py:attribute:: inject_location :type: ds_protocol_http_py_lib.utils.http.request.InjectLocation Where offset and limit are injected on the request. .. py:class:: PageNumberPaginationSettings Bases: :py:obj:`ds_common_serde_py_lib.Serializable` Configuration for page-number / page-size pagination. .. py:attribute:: page_param :type: str :value: 'page' Parameter name for the page ordinal. .. py:attribute:: page_size_param :type: str :value: 'per_page' Parameter name for the page size. .. py:attribute:: page_size :type: int :value: 100 Number of records requested per page. .. py:attribute:: start_page :type: int :value: 1 First page ordinal (0- or 1-based, must match the API). .. py:attribute:: total_pages_path :type: str | None :value: None Optional body path to a total page count. When the path resolves, a non-empty page stops on the last ordinal. An empty page stops immediately. A short page ends the read only when this path is unset or missing. .. py:attribute:: inject_location :type: ds_protocol_http_py_lib.utils.http.request.InjectLocation Where page and page size are injected on the request. .. py:class:: PaginationSettings Bases: :py:obj:`ds_common_serde_py_lib.Serializable` Declared pagination mechanism and strategy-specific nests. Provide the nest matching ``strategy`` (same pattern as auth nests on ``HttpLinkedServiceSettings``). Other nests should be omitted from the payload; only the selected nest is read at runtime. .. py:attribute:: strategy :type: ds_protocol_http_py_lib.dataset.pagination.enums.PaginationStrategy Pagination mechanism. Declared explicitly; never inferred. .. py:attribute:: max_pages :type: int | None :value: None Optional client-side safety ceiling on pages fetched per ``read()``. ``None`` (default) means no ceiling — keep paging until the strategy signals stop (short page, total count, empty cursor, …). Set an ``int`` only when you want a hard guard against runaway loops. .. py:attribute:: items_path :type: str :value: 'data' Path to the record array used for paging control (stop / advance). Use ``$`` for a root array. Does not select what the deserializer sees. Each page frame is ``dataset.deserializer(response.content)``. .. py:attribute:: offset :type: OffsetPaginationSettings | None :value: None Required when ``strategy`` is ``offset``. .. py:attribute:: page_number :type: PageNumberPaginationSettings | None :value: None Required when ``strategy`` is ``page_number``. .. py:attribute:: cursor :type: CursorPaginationSettings | None :value: None Required when ``strategy`` is ``cursor``. .. py:method:: __post_init__() -> None .. py:method:: _selected_nest() -> StrategyConfig | None .. py:property:: strategy_config :type: StrategyConfig Strategy-specific nest for ``strategy`` (guaranteed by ``__post_init__``). .. py:class:: CursorPaginationStrategy Bases: :py:obj:`ds_protocol_http_py_lib.dataset.pagination.strategies.base.PaginationStrategyHandler`\ [\ :py:obj:`ds_protocol_http_py_lib.dataset.pagination.settings.CursorPaginationSettings`\ ] Opaque cursor pagination: echo the server token until it is absent. .. py:method:: initial_state(cfg: ds_protocol_http_py_lib.dataset.pagination.settings.CursorPaginationSettings, checkpoint_slice: dict[str, Any] | None) -> ds_protocol_http_py_lib.dataset.pagination.strategies.base.PageState Build the starting page state, optionally restoring from checkpoint. .. py:method:: inject(request: ds_protocol_http_py_lib.utils.http.request.RequestSnapshot, state: ds_protocol_http_py_lib.dataset.pagination.strategies.base.PageState, cfg: ds_protocol_http_py_lib.dataset.pagination.settings.CursorPaginationSettings) -> ds_protocol_http_py_lib.utils.http.request.RequestSnapshot Return a request snapshot with pagination values applied. .. py:method:: advance(*, body: Any, headers: Any, items: list[Any], state: ds_protocol_http_py_lib.dataset.pagination.strategies.base.PageState, cfg: ds_protocol_http_py_lib.dataset.pagination.settings.CursorPaginationSettings) -> ds_protocol_http_py_lib.dataset.pagination.strategies.base.PageState Advance state after a successful page response. .. py:method:: should_stop(*, body: Any, headers: Any, items: list[Any], state: ds_protocol_http_py_lib.dataset.pagination.strategies.base.PageState, cfg: ds_protocol_http_py_lib.dataset.pagination.settings.CursorPaginationSettings) -> bool Return True when traversal should terminate after this page. .. py:method:: to_checkpoint(state: ds_protocol_http_py_lib.dataset.pagination.strategies.base.PageState) -> dict[str, Any] Serialize traversal state into the checkpoint ``pagination`` slice. .. py:method:: from_checkpoint(data: dict[str, Any]) -> ds_protocol_http_py_lib.dataset.pagination.strategies.base.PageState Restore traversal state from a checkpoint ``pagination`` slice. .. py:class:: OffsetPaginationStrategy Bases: :py:obj:`ds_protocol_http_py_lib.dataset.pagination.strategies.base.PaginationStrategyHandler`\ [\ :py:obj:`ds_protocol_http_py_lib.dataset.pagination.settings.OffsetPaginationSettings`\ ] Client-derived offset pagination: ``offset(n+1) = offset(n) + len(items)``. .. py:method:: initial_state(cfg: ds_protocol_http_py_lib.dataset.pagination.settings.OffsetPaginationSettings, checkpoint_slice: dict[str, Any] | None) -> ds_protocol_http_py_lib.dataset.pagination.strategies.base.PageState Build the starting page state, optionally restoring from checkpoint. .. py:method:: inject(request: ds_protocol_http_py_lib.utils.http.request.RequestSnapshot, state: ds_protocol_http_py_lib.dataset.pagination.strategies.base.PageState, cfg: ds_protocol_http_py_lib.dataset.pagination.settings.OffsetPaginationSettings) -> ds_protocol_http_py_lib.utils.http.request.RequestSnapshot Return a request snapshot with pagination values applied. .. py:method:: advance(*, body: Any, headers: Any, items: list[Any], state: ds_protocol_http_py_lib.dataset.pagination.strategies.base.PageState, cfg: ds_protocol_http_py_lib.dataset.pagination.settings.OffsetPaginationSettings) -> ds_protocol_http_py_lib.dataset.pagination.strategies.base.PageState Advance state after a successful page response. .. py:method:: should_stop(*, body: Any, headers: Any, items: list[Any], state: ds_protocol_http_py_lib.dataset.pagination.strategies.base.PageState, cfg: ds_protocol_http_py_lib.dataset.pagination.settings.OffsetPaginationSettings) -> bool Return True when traversal should terminate after this page. .. py:method:: to_checkpoint(state: ds_protocol_http_py_lib.dataset.pagination.strategies.base.PageState) -> dict[str, Any] Serialize traversal state into the checkpoint ``pagination`` slice. .. py:method:: from_checkpoint(data: dict[str, Any]) -> ds_protocol_http_py_lib.dataset.pagination.strategies.base.PageState Restore traversal state from a checkpoint ``pagination`` slice. .. py:class:: PageNumberPaginationStrategy Bases: :py:obj:`ds_protocol_http_py_lib.dataset.pagination.strategies.base.PaginationStrategyHandler`\ [\ :py:obj:`ds_protocol_http_py_lib.dataset.pagination.settings.PageNumberPaginationSettings`\ ] Client-derived page ordinal pagination: ``page(n+1) = page(n) + 1``. .. py:method:: initial_state(cfg: ds_protocol_http_py_lib.dataset.pagination.settings.PageNumberPaginationSettings, checkpoint_slice: dict[str, Any] | None) -> ds_protocol_http_py_lib.dataset.pagination.strategies.base.PageState Build the starting page state, optionally restoring from checkpoint. .. py:method:: inject(request: ds_protocol_http_py_lib.utils.http.request.RequestSnapshot, state: ds_protocol_http_py_lib.dataset.pagination.strategies.base.PageState, cfg: ds_protocol_http_py_lib.dataset.pagination.settings.PageNumberPaginationSettings) -> ds_protocol_http_py_lib.utils.http.request.RequestSnapshot Return a request snapshot with pagination values applied. .. py:method:: advance(*, body: Any, headers: Any, items: list[Any], state: ds_protocol_http_py_lib.dataset.pagination.strategies.base.PageState, cfg: ds_protocol_http_py_lib.dataset.pagination.settings.PageNumberPaginationSettings) -> ds_protocol_http_py_lib.dataset.pagination.strategies.base.PageState Advance state after a successful page response. .. py:method:: should_stop(*, body: Any, headers: Any, items: list[Any], state: ds_protocol_http_py_lib.dataset.pagination.strategies.base.PageState, cfg: ds_protocol_http_py_lib.dataset.pagination.settings.PageNumberPaginationSettings) -> bool Return True when traversal should terminate after this page. .. py:method:: to_checkpoint(state: ds_protocol_http_py_lib.dataset.pagination.strategies.base.PageState) -> dict[str, Any] Serialize traversal state into the checkpoint ``pagination`` slice. .. py:method:: from_checkpoint(data: dict[str, Any]) -> ds_protocol_http_py_lib.dataset.pagination.strategies.base.PageState Restore traversal state from a checkpoint ``pagination`` slice. .. py:class:: PaginationStrategyHandler Bases: :py:obj:`abc.ABC`, :py:obj:`Generic`\ [\ :py:obj:`ConfigT`\ ] Page-variation contract parameterized by the strategy settings nest. Each concrete strategy binds ``ConfigT`` to its nest type (e.g. ``CursorPaginationSettings``) so hook signatures stay precise. .. py:method:: initial_state(cfg: ConfigT, checkpoint_slice: dict[str, Any] | None) -> PageState :abstractmethod: Build the starting page state, optionally restoring from checkpoint. .. py:method:: inject(request: ds_protocol_http_py_lib.utils.http.request.RequestSnapshot, state: PageState, cfg: ConfigT) -> ds_protocol_http_py_lib.utils.http.request.RequestSnapshot :abstractmethod: Return a request snapshot with pagination values applied. .. py:method:: advance(*, body: Any, headers: Any, items: list[Any], state: PageState, cfg: ConfigT) -> PageState :abstractmethod: Advance state after a successful page response. .. py:method:: should_stop(*, body: Any, headers: Any, items: list[Any], state: PageState, cfg: ConfigT) -> bool :abstractmethod: Return True when traversal should terminate after this page. .. py:method:: to_checkpoint(state: PageState) -> dict[str, Any] :abstractmethod: Serialize traversal state into the checkpoint ``pagination`` slice. .. py:method:: from_checkpoint(data: dict[str, Any]) -> PageState :abstractmethod: Restore traversal state from a checkpoint ``pagination`` slice.