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(low-code cdk pagination): Fix the offset strategy so that it resets back to 0 when a stream is an incremental data feed #202

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ def next_page_token(
return self._offset

def reset(self, reset_value: Optional[Any] = 0) -> None:
if not isinstance(reset_value, int):
if reset_value is None:
self._offset = 0
elif not isinstance(reset_value, int):
raise ValueError(
f"Reset value {reset_value} for OffsetIncrement pagination strategy was not an integer"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ def next_page_token(
return self._delegate.next_page_token(response, last_page_size, last_record)

def reset(self, reset_value: Optional[Any] = None) -> None:
self._delegate.reset(reset_value)
if reset_value:
self._delegate.reset(reset_value)
else:
self._delegate.reset()

def get_page_size(self) -> Optional[int]:
return self._delegate.get_page_size()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import requests

from airbyte_cdk.sources.declarative.decoders import JsonDecoder, XmlDecoder
from airbyte_cdk.sources.declarative.incremental import DatetimeBasedCursor
from airbyte_cdk.sources.declarative.interpolation.interpolated_boolean import InterpolatedBoolean
from airbyte_cdk.sources.declarative.requesters.paginators.default_paginator import (
DefaultPaginator,
Expand All @@ -22,6 +23,10 @@
from airbyte_cdk.sources.declarative.requesters.paginators.strategies.offset_increment import (
OffsetIncrement,
)
from airbyte_cdk.sources.declarative.requesters.paginators.strategies.stop_condition import (
CursorStopCondition,
StopConditionPaginationStrategyDecorator,
)
from airbyte_cdk.sources.declarative.requesters.request_path import RequestPath


Expand Down Expand Up @@ -356,6 +361,40 @@ def test_reset(inject_on_first_request):
assert request_parameters_for_second_request != request_parameters_after_reset


def test_data_feed_paginator_with_stop_page_condition():
config = {}

cursor = DatetimeBasedCursor(
cursor_field="updated_at",
datetime_format="%Y-%m-%d",
start_datetime="2024-01-01",
config=config,
parameters={},
)

wrapped_strategy = StopConditionPaginationStrategyDecorator(
_delegate=OffsetIncrement(
config={}, page_size=2, inject_on_first_request=False, parameters={}
),
stop_condition=CursorStopCondition(cursor=cursor),
)

paginator = DefaultPaginator(
pagination_strategy=wrapped_strategy,
config=config,
url_base="https://airbyte.io",
parameters={},
page_size_option=RequestOption(
inject_into=RequestOptionType.request_parameter, field_name="limit", parameters={}
),
page_token_option=RequestOption(
inject_into=RequestOptionType.request_parameter, field_name="offset", parameters={}
),
)

paginator.reset()


def test_initial_token_with_offset_pagination():
page_size_request_option = RequestOption(
inject_into=RequestOptionType.request_parameter, field_name="limit", parameters={}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,5 @@ def test_offset_increment_reset(reset_value, expected_initial_token, expected_er
with pytest.raises(expected_error):
paginator_strategy.reset(reset_value=reset_value)
else:
if reset_value is None:
paginator_strategy.reset()
else:
paginator_strategy.reset(reset_value=reset_value)
paginator_strategy.reset(reset_value=reset_value)
assert paginator_strategy.initial_token == expected_initial_token
Loading