-
Notifications
You must be signed in to change notification settings - Fork 189
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
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
45c6c61
Add an example for post paginators
burnash be03338
update the example cursor key
burnash 7f3f0f0
Update docs/website/docs/general-usage/http/rest-client.md
burnash c8f413d
Update docs/website/docs/general-usage/http/rest-client.md
burnash 4c7fee7
Update docs/website/docs/general-usage/http/rest-client.md
burnash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
|
||||||
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: | ||||||
|
||||||
|
@@ -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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT cosmetics
Suggested change
|
||||||
|
||||||
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. | ||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT cosmetics
There was a problem hiding this comment.
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?