Skip to content

Commit

Permalink
updates docs to new type signature
Browse files Browse the repository at this point in the history
  • Loading branch information
willi-mueller committed Aug 12, 2024
1 parent 5e78dcc commit 44b8274
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
14 changes: 7 additions & 7 deletions dlt/sources/helpers/rest_client/paginators.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def init_request(self, request: Request) -> None: # noqa: B027, optional overri
pass

@abstractmethod
def update_state(self, response: Response, data: List[Any] = None) -> None:
def update_state(self, response: Response, data: Optional[List[Any]] = None) -> None:
"""Updates the paginator's state based on the response from the API.
This method should extract necessary pagination details (like next page
Expand Down Expand Up @@ -73,7 +73,7 @@ def __str__(self) -> str:
class SinglePagePaginator(BasePaginator):
"""A paginator for single-page API responses."""

def update_state(self, response: Response, data: List[Any] = None) -> None:
def update_state(self, response: Response, data: Optional[List[Any]] = None) -> None:
self._has_next_page = False

def update_request(self, request: Request) -> None:
Expand Down Expand Up @@ -140,7 +140,7 @@ def init_request(self, request: Request) -> None:

request.params[self.param_name] = self.current_value

def update_state(self, response: Response, data: List[Any] = None) -> None:
def update_state(self, response: Response, data: Optional[List[Any]] = None) -> None:
if self._stop_after_this_page(data):
self._has_next_page = False
else:
Expand All @@ -164,7 +164,7 @@ def update_state(self, response: Response, data: List[Any] = None) -> None:
):
self._has_next_page = False

def _stop_after_this_page(self, data: List[Any]) -> bool:
def _stop_after_this_page(self, data: Optional[List[Any]]) -> bool:
return self.stop_after_empty_page and data == []

def _handle_missing_total(self, response_json: Dict[str, Any]) -> None:
Expand Down Expand Up @@ -508,7 +508,7 @@ def __init__(self, links_next_key: str = "next") -> None:
super().__init__()
self.links_next_key = links_next_key

def update_state(self, response: Response, data: List[Any] = None) -> None:
def update_state(self, response: Response, data: Optional[List[Any]] = None) -> None:
"""Extracts the next page URL from the 'Link' header in the response."""
self._next_reference = response.links.get(self.links_next_key, {}).get("url")

Expand Down Expand Up @@ -563,7 +563,7 @@ def __init__(
super().__init__()
self.next_url_path = jsonpath.compile_path(next_url_path)

def update_state(self, response: Response, data: List[Any] = None) -> None:
def update_state(self, response: Response, data: Optional[List[Any]] = None) -> None:
"""Extracts the next page URL from the JSON response."""
values = jsonpath.find_values(self.next_url_path, response.json())
self._next_reference = values[0] if values else None
Expand Down Expand Up @@ -642,7 +642,7 @@ def __init__(
self.cursor_path = jsonpath.compile_path(cursor_path)
self.cursor_param = cursor_param

def update_state(self, response: Response, data: List[Any] = None) -> None:
def update_state(self, response: Response, data: Optional[List[Any]] = None) -> None:
"""Extracts the cursor value from the JSON response."""
values = jsonpath.find_values(self.cursor_path, response.json())
self._next_reference = values[0] if values else None
Expand Down
8 changes: 5 additions & 3 deletions docs/website/docs/general-usage/http/rest-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ When working with APIs that use non-standard pagination schemes, or when you nee

- `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.
- `update_state(response: Response, data: Optional[List[Any]]) -> 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.

- `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.

Expand All @@ -348,6 +348,7 @@ When working with APIs that use non-standard pagination schemes, or when you nee
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:

```py
from typing import Any, List, Optional
from dlt.sources.helpers.rest_client.paginators import BasePaginator
from dlt.sources.helpers.requests import Response, Request

Expand All @@ -361,7 +362,7 @@ class QueryParamPaginator(BasePaginator):
# This will set the initial page number (e.g. page=1)
self.update_request(request)

def update_state(self, response: Response) -> None:
def update_state(self, response: Response, data: Optional[List[Any]] = None) -> None:
# Assuming the API returns an empty list when no more data is available
if not response.json():
self._has_next_page = False
Expand Down Expand Up @@ -399,6 +400,7 @@ def get_data():
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 typing import Any, List, Optional
from dlt.sources.helpers.rest_client.paginators import BasePaginator
from dlt.sources.helpers.rest_client import RESTClient
from dlt.sources.helpers.requests import Response, Request
Expand All @@ -408,7 +410,7 @@ class PostBodyPaginator(BasePaginator):
super().__init__()
self.cursor = None

def update_state(self, response: Response) -> None:
def update_state(self, response: Response, data: Optional[List[Any]] = None) -> None:
# Assuming the API returns an empty list when no more data is available
if not response.json():
self._has_next_page = False
Expand Down

0 comments on commit 44b8274

Please sign in to comment.