From d5eaee1718e51caac4959ce543ba05be08458252 Mon Sep 17 00:00:00 2001 From: Anton Burnashev Date: Thu, 22 Feb 2024 12:31:23 +0300 Subject: [PATCH] Add `records_key` to `SinglePagePaginator` --- sources/rest_api/paginators.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/sources/rest_api/paginators.py b/sources/rest_api/paginators.py index e63754e85..565b8c3a8 100644 --- a/sources/rest_api/paginators.py +++ b/sources/rest_api/paginators.py @@ -75,6 +75,13 @@ def extract_records(self, response: Response) -> Any: class SinglePagePaginator(BasePaginator): """A paginator for single-page API responses.""" + def __init__( + self, + records_key: Union[str, Sequence[str]] = None, + ): + super().__init__() + self.records_key = records_key + self._records_accessor = create_nested_accessor(records_key) def update_state(self, response: Response) -> None: self._has_next_page = False @@ -83,7 +90,9 @@ def prepare_next_request_args(self, url, params, json): return None, None, None def extract_records(self, response: Response) -> Any: - return response.json() + if self.records_key is None: + return response.json() + return self._records_accessor(response.json()) class OffsetPaginator(BasePaginator):