-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Source Monday: fix pagination for the items stream (#25881)
* source monday: fix pagination for the items stream * source monday: refactor item pagination strategy * source monday: update pagination strategy docstring * formatting fixes and update version --------- Co-authored-by: Mal Hancock <mallory@archangelic.space> Co-authored-by: Marcos Marx <marcosmarxm@users.noreply.github.com>
- Loading branch information
1 parent
b8b1335
commit c296687
Showing
8 changed files
with
112 additions
and
12 deletions.
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
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
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
47 changes: 47 additions & 0 deletions
47
airbyte-integrations/connectors/source-monday/source_monday/item_pagination_strategy.py
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# | ||
# Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
from typing import Any, List, Mapping, Optional, Tuple | ||
|
||
from airbyte_cdk.sources.declarative.requesters.paginators.strategies.page_increment import PageIncrement | ||
|
||
|
||
class ItemPaginationStrategy(PageIncrement): | ||
""" | ||
Page increment strategy with subpages for the `items` stream. | ||
From the `items` documentation https://developer.monday.com/api-reference/docs/items: | ||
Please note that you cannot return more than 100 items per query when using items at the root. | ||
To adjust your query, try only returning items on a specific board, nesting items inside a boards query, | ||
looping through the boards on your account, or querying less than 100 items at a time. | ||
This pagination strategy supports nested loop through `boards` on the top level and `items` on the second. | ||
See boards documentation for more details: https://developer.monday.com/api-reference/docs/boards#queries. | ||
""" | ||
|
||
def __post_init__(self, parameters: Mapping[str, Any]): | ||
# `self._page` corresponds to board page number | ||
# `self._sub_page` corresponds to item page number within its board | ||
self.start_from_page = 1 | ||
self._page: Optional[int] = self.start_from_page | ||
self._sub_page: Optional[int] = self.start_from_page | ||
|
||
def next_page_token(self, response, last_records: List[Mapping[str, Any]]) -> Optional[Tuple[Optional[int], Optional[int]]]: | ||
""" | ||
Determines page and subpage numbers for the `items` stream | ||
Attributes: | ||
response: Contains `boards` and corresponding lists of `items` for each `board` | ||
last_records: Parsed `items` from the response | ||
""" | ||
if len(last_records) >= self.page_size: | ||
self._sub_page += 1 | ||
else: | ||
self._sub_page = self.start_from_page | ||
if len(response.json()["data"]["boards"]): | ||
self._page += 1 | ||
else: | ||
return None | ||
|
||
return self._page, self._sub_page |
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
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
42 changes: 42 additions & 0 deletions
42
airbyte-integrations/connectors/source-monday/unit_tests/test_item_pagination_strategy.py
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# | ||
# Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
from source_monday.item_pagination_strategy import ItemPaginationStrategy | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("response_json", "last_records", "expected"), | ||
[ | ||
pytest.param( | ||
{"data": {"boards": [{"items": [{"id": "1"}]}]}}, | ||
[{"id": "1"}], | ||
(1, 2), | ||
id="test_next_item_page_for_the_same_board", | ||
), | ||
pytest.param( | ||
{"data": {"boards": [{"items": []}]}}, | ||
[], | ||
(2, 1), | ||
id="test_next_board_page_with_item_page_reset", | ||
), | ||
pytest.param( | ||
{"data": {"boards": []}}, | ||
[], | ||
None, | ||
id="test_end_pagination", | ||
) | ||
] | ||
) | ||
def test_item_pagination_strategy(response_json, last_records, expected): | ||
strategy = ItemPaginationStrategy( | ||
page_size=1, | ||
parameters={"items_per_page": 1}, | ||
) | ||
response = MagicMock() | ||
response.json.return_value = response_json | ||
|
||
assert strategy.next_page_token(response, last_records) == expected |
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