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

✨ Source Shopify: add resiliency on some transient errors using the HttpClient #38084

Merged
merged 44 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
c2b64ee
migrated product_variants to BULK
bazarnov May 1, 2024
db2f7ce
Merge remote-tracking branch 'origin/master' into baz/source/shopify/…
bazarnov May 2, 2024
b2f44ab
added tests for product_images/variants
bazarnov May 2, 2024
5cf76bb
updated changelog
bazarnov May 2, 2024
c9f9718
Merge remote-tracking branch 'origin/master' into baz/source/shopify/…
bazarnov May 2, 2024
9e486b8
Merge remote-tracking branch 'origin/master' into baz/source/shopify/…
bazarnov May 2, 2024
01c7626
fixed price numberic value casting for product_variants
bazarnov May 2, 2024
953a756
Added the Products stream migration
bazarnov May 2, 2024
6e80e7c
Merge remote-tracking branch 'origin/master' into baz/source/shopify/…
bazarnov May 2, 2024
bd17a5a
updated the tags for the products stream
bazarnov May 2, 2024
ae30a1f
Merge remote-tracking branch 'origin/master' into baz/source/shopify/…
bazarnov May 2, 2024
b6aa6d0
Merge remote-tracking branch 'origin/master' into baz/source/shopify/…
bazarnov May 2, 2024
d206b8f
updated abnormal values for incremental CAT, updated expected records…
bazarnov May 2, 2024
5385b30
Merge remote-tracking branch 'origin/master' into baz/source/shopify/…
bazarnov May 2, 2024
202048d
updated after review
bazarnov May 3, 2024
29cef5c
Merge remote-tracking branch 'origin/master' into baz/source/shopify/…
bazarnov May 3, 2024
6088f7c
updated migration guide
bazarnov May 3, 2024
0382003
updated CAT config
bazarnov May 3, 2024
d652858
Merge remote-tracking branch 'origin/master' into baz/source/shopify/…
bazarnov May 3, 2024
2866235
Add preliminary test to read records from entrypoint in Bulk GraphQL job
May 5, 2024
a37dea4
Fix test with basic read records job
May 7, 2024
bb56efc
Merge remote-tracking branch 'origin/master' into baz/source/shopify/…
bazarnov May 7, 2024
ab522ee
Organize test data
May 7, 2024
66398ba
updated after the live-coding session
bazarnov May 7, 2024
805cd29
Merge remote-tracking branch 'origin/master' into baz/source/shopify/…
bazarnov May 8, 2024
14861a9
Test refactor
May 8, 2024
cc45bee
Merge remote-tracking branch 'origin/baz/source/shopify/migrate-produ…
May 8, 2024
66898dc
Fix data graphql end date
May 9, 2024
9a93b79
Improve format
May 9, 2024
1d86e03
Make requests and responses re-usable
maxi297 May 14, 2024
ed5ba45
Merge branch 'master' into strosek/test-conn-err-retry
maxi297 May 14, 2024
da0cc1b
format
maxi297 May 24, 2024
88a21b9
Merge branch 'master' into strosek/test-conn-err-retry
maxi297 May 24, 2024
a2f7186
Update release information
maxi297 May 24, 2024
0025b62
Update poetry lock for CI
maxi297 May 24, 2024
deda781
Update following merge master
maxi297 May 24, 2024
789306b
Fix stateful class that would break tests
maxi297 May 25, 2024
7c33dd2
Code review
maxi297 May 27, 2024
cfcedc0
Update query to use 'ShopifyBulkTemplates' in mock server tests
maxi297 May 28, 2024
6df28d1
format
maxi297 May 28, 2024
a72fd04
Merge branch 'master' into strosek/test-conn-err-retry
maxi297 May 29, 2024
b604b1d
Merge branch 'master' into strosek/test-conn-err-retry
maxi297 May 29, 2024
f6757f4
Issue 7935/integrate httpclient to access scopes (#38678)
maxi297 Jun 5, 2024
b705ec8
Merge branch 'master' into strosek/test-conn-err-retry
maxi297 Jun 5, 2024
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 @@ -11,7 +11,7 @@ data:
connectorSubtype: api
connectorType: source
definitionId: 9da77001-af33-4bcd-be46-6252bf9342b9
dockerImageTag: 2.1.4
dockerImageTag: 2.1.5
dockerRepository: airbyte/source-shopify
documentationUrl: https://docs.airbyte.com/integrations/sources/shopify
githubIssueLabel: source-shopify
Expand Down
18 changes: 16 additions & 2 deletions airbyte-integrations/connectors/source-shopify/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = [ "poetry-core>=1.0.0",]
build-backend = "poetry.core.masonry.api"

[tool.poetry]
version = "2.1.4"
version = "2.1.5"
name = "source-shopify"
description = "Source CDK implementation for Shopify."
authors = [ "Airbyte <[email protected]>",]
Expand All @@ -28,3 +28,4 @@ source-shopify = "source_shopify.run:run"
requests-mock = "^1.11.0"
pytest-mock = "^3.12.0"
pytest = "^8.0.0"
freezegun = "^1.4.0"
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,14 @@ class ShopifyScopes:
logger = logging.getLogger("airbyte")

def __init__(self, config: Mapping[str, Any]) -> None:
self.permitted_streams: List[str] = list(ALWAYS_PERMITTED_STREAMS)
self.not_permitted_streams: List[set[str, str]] = []
self.user_scopes = self.get_user_scopes(config)
# for each stream check the authenticated user has all scopes required
self.get_streams_from_user_scopes()
# log if there are streams missing scopes and should be omitted
self.emit_missing_scopes()

# the list of validated streams
permitted_streams: List[str] = ALWAYS_PERMITTED_STREAMS
# the list of not permitted streams
not_permitted_streams: List[set[str, str]] = []
# template for the log message
missing_scope_message: str = (
"The stream `{stream}` could not be synced without the `{scope}` scope. Please check the `{scope}` is granted."
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Copyright (c) 2024 Airbyte, Inc., all rights reserved.

import json

from airbyte_cdk.test.mock_http import HttpMocker, HttpRequest, HttpResponse
from airbyte_cdk.test.mock_http.request import ANY_QUERY_PARAMS
from airbyte_cdk.test.mock_http.response_builder import find_template

_ALL_SCOPES = [
"read_all_cart_transforms",
"read_all_checkout_completion_target_customizations",
"read_all_orders",
"read_analytics",
"read_assigned_fulfillment_orders",
"read_cart_transforms",
"read_channels",
"read_companies",
"read_content",
"read_custom_fulfillment_services",
"read_customer_data_erasure",
"read_customer_merge",
"read_customers",
"read_dery_customizations",
"read_discounts",
"read_draft_orders",
"read_files",
"read_fulfillment_constraint_rules",
"read_fulfillments",
"read_gates",
"read_gdpr_data_request",
"read_gift_cards",
"read_inventory",
"read_legal_policies",
"read_locales",
"read_locations",
"read_marketing_events",
"read_markets",
"read_merchant_managed_fulfillment_orders",
"read_online_store_navigation",
"read_online_store_pages",
"read_order_edits",
"read_order_submission_rules",
"read_orders",
"read_packing_slip_templates",
"read_payment_customizations",
"read_payment_terms",
"read_pixels",
"read_price_rules",
"read_product_feeds",
"read_product_listings",
"read_products",
"read_publications",
"read_purchase_options",
"read_reports",
"read_resource_feedbacks",
"read_returns",
"read_script_tags",
"read_shipping",
"read_shopify_credit",
"read_shopify_payments_accounts",
"read_shopify_payments_bank_accounts",
"read_shopify_payments_disputes",
"read_shopify_payments_payouts",
"read_shopify_payments_provider_accounts_sensitive",
"read_store_credit_account_transactions",
"read_themes",
"read_third_party_fulfillment_orders",
"read_translations"
]


def set_up_shop(http_mocker: HttpMocker, shop_name: str) -> None:
http_mocker.get(
HttpRequest(f"https://{shop_name}.myshopify.com/admin/api/2024-04/shop.json", query_params=ANY_QUERY_PARAMS),
HttpResponse(json.dumps(find_template("shop", __file__)), status_code=200),
)


def grant_all_scopes(http_mocker: HttpMocker, shop_name: str) -> None:
http_mocker.get(
HttpRequest(f"https://{shop_name}.myshopify.com/admin/oauth/access_scopes.json"),
HttpResponse(json.dumps({"access_scopes": [{"handle": scope} for scope in _ALL_SCOPES]}), status_code=200),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
# Copyright (c) 2024 Airbyte, Inc., all rights reserved.

import json
from datetime import datetime
from random import randint

from airbyte_cdk.test.mock_http import HttpRequest, HttpResponse


def _create_job_url(shop_name: str) -> str:
return f"https://{shop_name}.myshopify.com/admin/api/2024-04/graphql.json"


def create_job_creation_request(shop_name: str, lower_boundary: datetime, upper_boundary: datetime) -> HttpRequest:
outer_query = """mutation {
bulkOperationRunQuery(
query: \"\"\"
%INNER_QUERY_TOKEN%
\"\"\"
) {
bulkOperation {
id
status
createdAt
}
userErrors {
field
message
}
}
}"""
inner_query = """{
orders(
query: \"updated_at:>='%LOWER_BOUNDARY_TOKEN%' AND updated_at:<='%UPPER_BOUNDARY_TOKEN%'\"
sortKey: UPDATED_AT
) {
edges {
node {
__typename
id
metafields {
edges {
node {
__typename
id
namespace
value
key
description
createdAt
updatedAt
type
}
}
}
}
}
}
}"""
inner_query = inner_query.replace("%LOWER_BOUNDARY_TOKEN%", lower_boundary.isoformat())
inner_query = inner_query.replace("%UPPER_BOUNDARY_TOKEN%", upper_boundary.isoformat())
outer_query = outer_query.replace("%INNER_QUERY_TOKEN%", inner_query)
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can also leverage the stream_slices() method, to produce 1 slice, or multiple slices, instead of directly injecting the date-time str.

Copy link
Contributor

@maxi297 maxi297 May 27, 2024

Choose a reason for hiding this comment

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

It feels like we like this isn't reducing the readability of the test and that we should not rely on the source code to create our expectations for the slices that will be produced


return HttpRequest(
url=_create_job_url(shop_name),
body=json.dumps({"query": outer_query})
)


def create_job_status_request(shop_name: str, job_id: str) -> HttpRequest:
return HttpRequest(
url=_create_job_url(shop_name),
body=f"""query {{
node(id: "{job_id}") {{
... on BulkOperation {{
id
status
errorCode
createdAt
objectCount
fileSize
url
partialDataUrl
}}
}}
}}"""
)


class JobCreationResponseBuilder:
def __init__(self) -> None:
self._template = {
"data": {
"bulkOperationRunQuery": {
"bulkOperation": {
"id": "gid://shopify/BulkOperation/0",
"status": "CREATED",
"createdAt": "2024-05-05T02:00:00Z"
},
"userErrors": []
}
},
"extensions": {
"cost": {
"requestedQueryCost": 10,
"actualQueryCost": 10,
"throttleStatus": {
"maximumAvailable": 2000.0,
"currentlyAvailable": 1990,
"restoreRate": 100.0
}
}
}
}

def with_bulk_operation_id(self, bulk_operation_id: str) -> "JobCreationResponseBuilder":
self._template["data"]["bulkOperationRunQuery"]["bulkOperation"]["id"] = bulk_operation_id
return self

def build(self) -> HttpResponse:
return HttpResponse(json.dumps(self._template), status_code=200)


class JobStatusResponseBuilder:
def __init__(self) -> None:
self._template = {
"data": {
"node": {},
"extensions": {
"cost": {
"requestedQueryCost": 1,
"actualQueryCost": 1,
"throttleStatus": {
"maximumAvailable": 2000.0,
"currentlyAvailable": 1999,
"restoreRate": 100.0
}
}
}
}
}

def with_completed_status(self, bulk_operation_id: str, job_result_url: str) -> "JobStatusResponseBuilder":
self._template["data"]["node"] = {
"id": bulk_operation_id,
"status": "COMPLETED",
"errorCode": None,
"createdAt": "2024-05-05T00:45:48Z",
"objectCount": "4",
"fileSize": "774",
"url": job_result_url,
"partialDataUrl": None
}
return self

def build(self) -> HttpResponse:
return HttpResponse(json.dumps(self._template), status_code=200)


class MetafieldOrdersJobResponseBuilder:
def __init__(self) -> None:
self._records = []

def _any_record(self) -> str:
an_id = str(randint(1000000000000, 9999999999999))
a_parent_id = str(randint(1000000000000, 9999999999999))
return f"""{{"__typename":"Order","id":"gid:\/\/shopify\/Order\/{a_parent_id}"}}
{{"__typename":"Metafield","id":"gid:\/\/shopify\/Metafield\/{an_id}","namespace":"my_fields","value":"asdfasdf","key":"purchase_order","description":null,"createdAt":"2023-04-13T12:09:50Z","updatedAt":"2024-05-05T01:09:50Z","type":"single_line_text_field","__parentId":"gid:\/\/shopify\/Order\/{a_parent_id}"}}
"""


def with_record(self) -> "MetafieldOrdersJobResponseBuilder":
self._records.append(self._any_record())
return self

def build(self) -> HttpResponse:
return HttpResponse("".join(self._records), status_code=200)
Loading
Loading