Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RESTClient: add an example for creating a custom POST paginator #1358

Merged
merged 5 commits into from
May 14, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion docs/website/docs/general-usage/http/rest-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ When working with APIs that use non-standard pagination schemes, or when you nee

- `update_request(request: Request) -> None`: Before making the next API call in `RESTClient.paginate` method, `update_request` is used to modify the request with the necessary parameters to fetch the next page (based on the current state of the paginator). For example, you can add query parameters to the request, or modify the URL.

#### Example: creating a query parameter paginator
#### Example 1: creating a query parameter paginator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT cosmetics

Suggested change
#### Example 1: creating a query parameter paginator
#### Example 1: Creating a query parameter paginator

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're using the sentence case for all headings, so I think it should start with a lowercase after ":". Or not?


Suppose an API uses query parameters for pagination, incrementing an page parameter for each subsequent page, without providing direct links to next pages in its responses. E.g. `https://api.example.com/posts?page=1`, `https://api.example.com/posts?page=2`, etc. Here's how you could implement a paginator for this scheme:

Expand Down Expand Up @@ -354,6 +354,38 @@ def get_data():
yield page
```

:::tip
[`PageNumberPaginator`](#pagenumberpaginator) that ships with dlt does the same thing, but with more flexibility and error handling. This example is meant to demonstrate how to implement a custom paginator. For most use cases, you should use the [built-in paginators](#paginators).
:::

#### Example 2: creating a paginator for POST requests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT cosmetics

Suggested change
#### Example 2: creating a paginator for POST requests
#### Example 2: Creating a paginator for POST requests


Some APIs use POST requests for pagination, where the next page is fetched by sending a POST request with a cursor or other parameters in the request body. This is frequently used in "search" API endpoints or other endpoints with big payloads. Here's how you could implement a paginator for a case like this:

```py
from dlt.sources.helpers.rest_client.paginators import BasePaginator
from dlt.sources.helpers.requests import Response, Request
burnash marked this conversation as resolved.
Show resolved Hide resolved

class PostBodyPaginator(BasePaginator):
def __init__(self):
super().__init__()
self.cursor = None

def update_state(self, response: Response) -> None:
# Assuming the API returns an empty list when no more data is available
if not response.json():
self._has_next_page = False
else:
self.cursor = response.json().get("next_page_cursor")

def update_request(self, request: Request) -> None:
if request.json is None:
request.json = {}

# Add the cursor to the request body
request.json["cursor"] = self.cursor
burnash marked this conversation as resolved.
Show resolved Hide resolved
```

## Authentication

The RESTClient supports various authentication strategies, such as bearer tokens, API keys, and HTTP basic auth, configured through the `auth` parameter of both the `RESTClient` and the `paginate()` method.
Expand Down
Loading