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

update low-code connectors using last_records #36409

Closed
wants to merge 15 commits into from
Closed
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 @@ -353,11 +353,12 @@ definitions:
interpolation_context:
- config
- headers
- last_records
- last_page_size
- last_record
- response
examples:
- "{{ headers.link.next.cursor }}"
- "{{ last_records[-1]['key'] }}"
- "{{ last_record['key'] }}"
- "{{ response['nextPage'] }}"
page_size:
title: Page Size
Expand All @@ -372,7 +373,7 @@ definitions:
interpolation_context:
- config
- headers
- last_records
- last_record
- response
examples:
- "{{ response.data.has_more is false }}"
Expand Down Expand Up @@ -2253,20 +2254,20 @@ interpolation:
x-ratelimit-limit: "600"
x-ratelimit-remaining: "598"
x-ratelimit-reset: "39"
- title: last_records
description: List of records extracted from the last response received from the API.
type: list
- title: last_record
description: Last record extracted from the response received from the API.
type: object
examples:
- name: "Test List: 19"
id: 0236d6d2
contact_count: 20
_metadata:
self: https://api.sendgrid.com/v3/marketing/lists/0236d6d2
- title: last_page_size
description: Number of records extracted from the last response received from the API.
type: object
examples:
- - name: "Test List: 19"
id: 0236d6d2
contact_count: 20
_metadata:
self: https://api.sendgrid.com/v3/marketing/lists/0236d6d2
- name: List for CI tests, number 30
id: 041ee031
contact_count: 0
_metadata:
self: https://api.sendgrid.com/v3/marketing/lists/041ee031
- 2
- title: next_page_token
description: Object describing the token to fetch the next page of records. The object has a single key "next_page_token".
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ class CursorPagination(BaseModel):
description='Value of the cursor defining the next page to fetch.',
examples=[
'{{ headers.link.next.cursor }}',
"{{ last_records[-1]['key'] }}",
"{{ last_record['key'] }}",
"{{ response['nextPage'] }}",
],
title='Cursor Value',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#

from dataclasses import InitVar, dataclass
from typing import Any, List, Mapping, Optional, Union
from typing import Any, List, Mapping, Optional, Union, Dict

import requests
from airbyte_cdk.sources.declarative.decoders.decoder import Decoder
Expand All @@ -13,6 +13,8 @@
from airbyte_cdk.sources.declarative.requesters.paginators.strategies.pagination_strategy import PaginationStrategy
from airbyte_cdk.sources.declarative.types import Config

from airbyte_cdk.sources.declarative.types import Record


@dataclass
class CursorPaginationStrategy(PaginationStrategy):
Expand All @@ -34,32 +36,36 @@ class CursorPaginationStrategy(PaginationStrategy):
stop_condition: Optional[Union[InterpolatedBoolean, str]] = None
decoder: Decoder = JsonDecoder(parameters={})

def __post_init__(self, parameters: Mapping[str, Any]):
def __post_init__(self, parameters: Mapping[str, Any]) -> None:
if isinstance(self.cursor_value, str):
self.cursor_value = InterpolatedString.create(self.cursor_value, parameters=parameters)
self._cursor_value = InterpolatedString.create(self.cursor_value, parameters=parameters)
else:
self._cursor_value = self.cursor_value
if isinstance(self.stop_condition, str):
self.stop_condition = InterpolatedBoolean(condition=self.stop_condition, parameters=parameters)
else:
self._stop_condition = self.stop_condition

@property
def initial_token(self) -> Optional[Any]:
return None

def next_page_token(self, response: requests.Response, last_records: List[Mapping[str, Any]]) -> Optional[Any]:
def next_page_token(self, response: requests.Response, last_records: List[Record]) -> Optional[Any]:
decoded_response = self.decoder.decode(response)

# The default way that link is presented in requests.Response is a string of various links (last, next, etc). This
# is not indexable or useful for parsing the cursor, so we replace it with the link dictionary from response.links
headers = response.headers
headers: Dict[str, Any] = dict(response.headers)
headers["link"] = response.links

if self.stop_condition:
should_stop = self.stop_condition.eval(self.config, response=decoded_response, headers=headers, last_records=last_records)
if self._stop_condition:
should_stop = self._stop_condition.eval(self.config, response=decoded_response, headers=headers, last_records=last_records, last_record=last_records[-1], last_page_size=len(last_records))
if should_stop:
return None
token = self.cursor_value.eval(config=self.config, last_records=last_records, response=decoded_response, headers=headers)
token = self._cursor_value.eval(config=self.config, last_records=last_records, response=decoded_response, headers=headers, last_record=last_records[-1], last_page_size=len(last_records))
return token if token else None

def reset(self):
def reset(self) -> None:
# No state to reset
pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ def test_full_config_stream():

assert isinstance(stream.retriever.paginator.pagination_strategy, CursorPaginationStrategy)
assert isinstance(stream.retriever.paginator.pagination_strategy.decoder, JsonDecoder)
assert stream.retriever.paginator.pagination_strategy.cursor_value.string == "{{ response._metadata.next }}"
assert stream.retriever.paginator.pagination_strategy.cursor_value.default == "{{ response._metadata.next }}"
assert stream.retriever.paginator.pagination_strategy._cursor_value.string == "{{ response._metadata.next }}"
assert stream.retriever.paginator.pagination_strategy._cursor_value.default == "{{ response._metadata.next }}"
assert stream.retriever.paginator.pagination_strategy.page_size == 10

assert isinstance(stream.retriever.requester, HttpRequester)
Expand Down Expand Up @@ -1128,7 +1128,7 @@ def test_create_default_paginator():

assert isinstance(paginator.pagination_strategy, CursorPaginationStrategy)
assert paginator.pagination_strategy.page_size == 50
assert paginator.pagination_strategy.cursor_value.string == "{{ response._metadata.next }}"
assert paginator.pagination_strategy._cursor_value.string == "{{ response._metadata.next }}"

assert isinstance(paginator.page_size_option, RequestOption)
assert paginator.page_size_option.inject_into == RequestOptionType.request_parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ definitions:
type: "DefaultPaginator"
pagination_strategy:
type: "CursorPagination"
cursor_value: "{{ last_records['href'] }}"
cursor_value: "{{ last_record['href'] }}"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

confirmed CATs pass

Uploading Screenshot 2024-03-25 at 1.57.08 PM.png…

page_token_option:
type: "RequestPath"
field_name: "from"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ definitions:
type: "DefaultPaginator"
pagination_strategy:
type: "CursorPagination"
cursor_value: "{{ last_records['meta', 'pagination', 'next_offset'] }}"
cursor_value: "{{ last_record['meta', 'pagination', 'next_offset'] }}"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CATs are failing on master too.

I'm also not sure how this can work on master since last_records should be an array, not an object

page_token_option:
type: "RequestPath"
field_name: "page[offset]"
Expand All @@ -65,7 +65,7 @@ definitions:
type: "DefaultPaginator"
pagination_strategy:
type: "CursorPagination"
cursor_value: "{{ last_records['meta', 'page', 'after'] }}"
cursor_value: "{{ last_record['meta', 'page', 'after'] }}"
page_token_option:
type: "RequestPath"
field_name: "page[cursor]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ definitions:
type: "DefaultPaginator"
pagination_strategy:
type: "CursorPagination"
cursor_value: "{{ last_records['next'] }}"
cursor_value: "{{ last_record['next'] }}"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

confirmed CATs pass
Screenshot 2024-03-25 at 2 13 06 PM

page_token_option:
type: "RequestPath"
field_name: "page_token"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ definitions:
type: "DefaultPaginator"
pagination_strategy:
type: "CursorPagination"
cursor_value: "{{ last_records[-1]['scrollId'] }}"
cursor_value: "{{ last_record['scrollId'] }}"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed CATs pass

Screenshot 2024-03-25 at 2 26 35 PM

page_size: 5
page_token_option:
type: "RequestPath"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ definitions:
type: "DefaultPaginator"
pagination_strategy:
type: "CursorPagination"
cursor_value: "{{ last_records['paging', 'next'] }}"
cursor_value: "{{ last_record['paging', 'next'] }}"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed CATs pass
Screenshot 2024-03-25 at 2 42 30 PM

page_token_option:
type: "RequestPath"
field_name: "from"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ definitions:
type: DefaultPaginator
pagination_strategy:
type: "CursorPagination"
cursor_value: "{{ last_records[-1]['key'] }}"
cursor_value: "{{ last_record['key'] }}"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests don't pass on master

stop_condition: "{{ response.data.has_more is false }}"
page_size: 250
page_size_option:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,185 +1,5 @@
{
"streams": [
{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIX:ME undo this change

"stream": {
"name": "coupons",
"json_schema": {},
"supported_sync_modes": ["full_refresh", "incremental"]
},
"sync_mode": "incremental",
"destination_sync_mode": "append"
},
{
"stream": {
"name": "customers",
"json_schema": {},
"supported_sync_modes": ["full_refresh", "incremental"]
},
"sync_mode": "full_refresh",
"destination_sync_mode": "append"
},
{
"stream": {
"name": "order_notes",
"json_schema": {},
"supported_sync_modes": ["full_refresh"]
},
"sync_mode": "full_refresh",
"destination_sync_mode": "overwrite"
},
{
"stream": {
"name": "orders",
"json_schema": {},
"supported_sync_modes": ["full_refresh", "incremental"]
},
"sync_mode": "incremental",
"destination_sync_mode": "append"
},
{
"stream": {
"name": "payment_gateways",
"json_schema": {},
"supported_sync_modes": ["full_refresh"]
},
"sync_mode": "full_refresh",
"destination_sync_mode": "overwrite"
},
{
"stream": {
"name": "product_attribute_terms",
"json_schema": {},
"supported_sync_modes": ["full_refresh"]
},
"sync_mode": "full_refresh",
"destination_sync_mode": "overwrite"
},
{
"stream": {
"name": "product_attributes",
"json_schema": {},
"supported_sync_modes": ["full_refresh"]
},
"sync_mode": "full_refresh",
"destination_sync_mode": "overwrite"
},
{
"stream": {
"name": "product_categories",
"json_schema": {},
"supported_sync_modes": ["full_refresh"]
},
"sync_mode": "full_refresh",
"destination_sync_mode": "overwrite"
},
{
"stream": {
"name": "product_reviews",
"json_schema": {},
"supported_sync_modes": ["full_refresh", "incremental"]
},
"sync_mode": "incremental",
"destination_sync_mode": "append"
},
{
"stream": {
"name": "product_shipping_classes",
"json_schema": {},
"supported_sync_modes": ["full_refresh"]
},
"sync_mode": "full_refresh",
"destination_sync_mode": "overwrite"
},
{
"stream": {
"name": "product_tags",
"json_schema": {},
"supported_sync_modes": ["full_refresh"]
},
"sync_mode": "full_refresh",
"destination_sync_mode": "overwrite"
},
{
"stream": {
"name": "product_variations",
"json_schema": {},
"supported_sync_modes": ["full_refresh"]
},
"sync_mode": "full_refresh",
"destination_sync_mode": "overwrite"
},
{
"stream": {
"name": "products",
"json_schema": {},
"supported_sync_modes": ["full_refresh", "incremental"]
},
"sync_mode": "incremental",
"destination_sync_mode": "append"
},
{
"stream": {
"name": "refunds",
"json_schema": {},
"supported_sync_modes": ["full_refresh"]
},
"sync_mode": "full_refresh",
"destination_sync_mode": "overwrite"
},
{
"stream": {
"name": "shipping_methods",
"json_schema": {},
"supported_sync_modes": ["full_refresh"]
},
"sync_mode": "full_refresh",
"destination_sync_mode": "overwrite"
},
{
"stream": {
"name": "shipping_zone_locations",
"json_schema": {},
"supported_sync_modes": ["full_refresh"]
},
"sync_mode": "full_refresh",
"destination_sync_mode": "overwrite"
},
{
"stream": {
"name": "shipping_zone_methods",
"json_schema": {},
"supported_sync_modes": ["full_refresh"]
},
"sync_mode": "full_refresh",
"destination_sync_mode": "overwrite"
},
{
"stream": {
"name": "shipping_zones",
"json_schema": {},
"supported_sync_modes": ["full_refresh"]
},
"sync_mode": "full_refresh",
"destination_sync_mode": "overwrite"
},
{
"stream": {
"name": "system_status_tools",
"json_schema": {},
"supported_sync_modes": ["full_refresh"]
},
"sync_mode": "full_refresh",
"destination_sync_mode": "overwrite"
},
{
"stream": {
"name": "tax_classes",
"json_schema": {},
"supported_sync_modes": ["full_refresh"]
},
"sync_mode": "full_refresh",
"destination_sync_mode": "overwrite"
},
{
"stream": {
"name": "tax_rates",
Expand Down
Loading
Loading