Skip to content

Commit

Permalink
RESTClient: add docs for init_request (#1442)
Browse files Browse the repository at this point in the history
  • Loading branch information
burnash authored Jun 4, 2024
1 parent bad7645 commit 964a94d
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion docs/website/docs/general-usage/http/rest-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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():
Expand Down

0 comments on commit 964a94d

Please sign in to comment.