From 964a94d61f6a8243cab3195a0bbbe7e65bc6384b Mon Sep 17 00:00:00 2001 From: Anton Burnashev Date: Tue, 4 Jun 2024 15:24:34 +0200 Subject: [PATCH] RESTClient: add docs for `init_request` (#1442) --- docs/website/docs/general-usage/http/rest-client.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/website/docs/general-usage/http/rest-client.md b/docs/website/docs/general-usage/http/rest-client.md index 1093428b0f..8f517389c6 100644 --- a/docs/website/docs/general-usage/http/rest-client.md +++ b/docs/website/docs/general-usage/http/rest-client.md @@ -305,7 +305,9 @@ client = RESTClient( ### Implementing a custom paginator -When working with APIs that use non-standard pagination schemes, or when you need more control over the pagination process, you can implement a custom paginator by subclassing the `BasePaginator` class and `update_state` and `update_request` methods: +When working with APIs that use non-standard pagination schemes, or when you need more control over the pagination process, you can implement a custom paginator by subclassing the `BasePaginator` class and implementing `init_request`, `update_state` and `update_request` methods: + +- `init_request(request: Request) -> None`: This method is called before making the first API call in the `RESTClient.paginate` method. You can use this method to set up the initial request query parameters, headers, etc. For example, you can set the initial page number or cursor value. - `update_state(response: Response) -> None`: This method updates the paginator's state based on the response of the API call. Typically, you extract pagination details (like the next page reference) from the response and store them in the paginator instance. @@ -325,6 +327,10 @@ class QueryParamPaginator(BasePaginator): self.page_param = page_param self.page = initial_page + def init_request(self, request: Request) -> None: + # This will set the initial page number (e.g. page=1) + self.update_request(request) + def update_state(self, response: Response) -> None: # Assuming the API returns an empty list when no more data is available if not response.json():