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

fix: PageNumberPaginator not reset when iterating through multiple pa… #1924

Merged
merged 2 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions dlt/sources/helpers/rest_client/paginators.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import warnings
from abc import ABC, abstractmethod
from typing import Any, Dict, List, Optional
from urllib.parse import urlparse, urljoin
from urllib.parse import urljoin, urlparse

from requests import Request, Response

from requests import Response, Request
from dlt.common import jsonpath


Expand Down Expand Up @@ -127,6 +128,7 @@ def __init__(
" provided."
)
self.param_name = param_name
self.initial_value = initial_value
self.current_value = initial_value
self.value_step = value_step
self.base_index = base_index
Expand All @@ -136,6 +138,8 @@ def __init__(
self.stop_after_empty_page = stop_after_empty_page

def init_request(self, request: Request) -> None:
self._has_next_page = True
self.current_value = self.initial_value
if request.params is None:
request.params = {}

Expand Down
27 changes: 27 additions & 0 deletions tests/sources/helpers/rest_client/test_paginators.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,33 @@ def test_update_state(self):
paginator.update_state(response, data=NON_EMPTY_PAGE)
assert paginator.has_next_page is False

def test_init_request(self):
paginator = PageNumberPaginator(base_page=1, total_path=None)
request = Mock(Request)
request.params = {}
response = Mock(Response, json=lambda: "OK")

assert paginator.current_value == 1
assert paginator.has_next_page is True
paginator.init_request(request)

paginator.update_state(response, data=NON_EMPTY_PAGE)
paginator.update_request(request)

assert paginator.current_value == 2
assert paginator.has_next_page is True
assert request.params["page"] == 2

paginator.update_state(response, data=None)
paginator.update_request(request)

assert paginator.current_value == 2
assert paginator.has_next_page is False

paginator.init_request(request)
assert paginator.current_value == 1
assert paginator.has_next_page is True

def test_update_state_with_string_total_pages(self):
paginator = PageNumberPaginator(base_page=1, page=1)
response = Mock(Response, json=lambda: {"total": "3"})
Expand Down
Loading