forked from singer-io/tap-shopify
-
Notifications
You must be signed in to change notification settings - Fork 3
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
HG-3697: Use GraphQL API for Products Stream #28
Open
nassredean
wants to merge
12
commits into
master
Choose a base branch
from
feature/hg-3697
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
932f0e3
Add basic GraphQL query replacement for products
nassredean 8bf162d
Add pagination to Products class using GraphQL
nassredean 7cf2171
Add compatability method for GraphQL product shape to RestAPI
nassredean 01ad766
Extract converter to its own class
nassredean a81aa73
Extract a small utility method in the ProductConverter class to extra…
nassredean a3a24ff
Extract options for ProductVariant. Add admin_graphql_api_id key
nassredean 4769111
Add value map for variants
nassredean 8b2bfdd
Recursive compatability mapping lookup, added status column to mapping
nassredean 99a4764
Add alt and image_id to ProductCompatibility
nassredean 401e670
Add tap_shopify prefix to import statment to ensure dependency resolu…
nassredean 5e4b85f
Add metafields method to ProductCompatability and update callsites
nassredean a598b2e
Uncomment exception retry
nassredean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
145 changes: 145 additions & 0 deletions
145
tap_shopify/streams/compatibility/product_compatibility.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,145 @@ | ||
import json | ||
import os | ||
import shopify.resources | ||
|
||
class ProductCompatibility(): | ||
def __init__(self, graphql_product): | ||
"""Initialize with a GraphQL product object.""" | ||
self.graphql_product = graphql_product | ||
self.admin_graphql_api_id = graphql_product["id"] | ||
self.product_id = self._extract_int_id(graphql_product["id"]) | ||
|
||
current_dir = os.path.dirname(os.path.abspath(__file__)) | ||
value_map_path = os.path.join(current_dir, "value_maps", "product.json") | ||
with open(value_map_path, 'r') as file: | ||
self.value_map = json.load(file) | ||
|
||
def metafields(self, _options=None, **kwargs): | ||
if _options is None: | ||
_options = kwargs | ||
return shopify.resources.Metafield.find(resource="products", resource_id=self.product_id, **_options) | ||
|
||
def _extract_int_id(self, string_id): | ||
return int(string_id.split("/")[-1]) | ||
|
||
def _convert_options(self): | ||
return [ | ||
{ | ||
"id": self._extract_int_id(option["id"]), | ||
"product_id": self.product_id, | ||
"name": option["name"], | ||
"position": option["position"], | ||
"values": option["values"] | ||
} | ||
for option in self.graphql_product["options"] | ||
] | ||
|
||
def _convert_images(self): | ||
return [ | ||
{ | ||
"id": self._extract_int_id(image["id"]), | ||
"admin_graphql_api_id": image["id"], | ||
"position": idx + 1, | ||
"alt": image["altText"], | ||
"created_at": None, # No longer supported by GraphQL API | ||
"updated_at": None, # No longer supported by GraphQL API | ||
"width": image["width"], | ||
"height": image["height"], | ||
"src": image["src"], | ||
"variant_ids": None # No longer supported by GraphQL API | ||
} | ||
for idx, image in enumerate(self.graphql_product.get("images", {}).get("nodes", [])) | ||
] | ||
|
||
def _extract_variant_options(self, variant): | ||
option_dict = { | ||
"option1": None, | ||
"option2": None, | ||
"option3": None | ||
} # The maximum number of selectedOptions returned from a ProductVariant is 3 | ||
selected_options = variant["selectedOptions"] | ||
for idx, option in enumerate(selected_options): | ||
option_dict[f"option{idx + 1}"] = option["value"] | ||
return option_dict | ||
|
||
def _cast_variant_values(self, variant): | ||
"""Cast variant values based on the value_map.""" | ||
for key, value in variant.items(): | ||
if key in self.value_map["variants"]: | ||
key_map = self.value_map["variants"][key] | ||
if value in key_map: | ||
variant[key] = key_map[value] | ||
return variant | ||
|
||
def _convert_variants(self): | ||
return [ | ||
{ | ||
"admin_graphql_api_id": variant["id"], | ||
"barcode": variant["barcode"], | ||
"compare_at_price": variant["compareAtPrice"], | ||
"created_at": variant["createdAt"], | ||
"fulfillment_service": variant["fulfillmentService"]["handle"], | ||
"grams": None, # No longer supported by GraphQL API | ||
"id": self._extract_int_id(variant["id"]), | ||
"image_id": self._extract_int_id(variant["image"]["id"]) if variant.get("image") else None, | ||
"inventory_item_id": self._extract_int_id(variant["inventoryItem"]["id"]), | ||
"inventory_management": None, # No longer supported by GraphQL API | ||
"inventory_policy": variant["inventoryPolicy"], | ||
"inventory_quantity": variant["inventoryQuantity"], | ||
"old_inventory_quantity": None, # No longer supported by GraphQL API | ||
"position": variant["position"], | ||
"price": variant["price"], | ||
"requires_shipping": variant["inventoryItem"]["requiresShipping"], | ||
"sku": variant["sku"], | ||
"tax_code": variant["taxCode"], | ||
"taxable": variant["taxable"], | ||
"title": variant["title"], | ||
"updated_at": variant["updatedAt"], | ||
"weight": variant["weight"], | ||
"weight_unit": variant["weightUnit"], | ||
} | self._extract_variant_options(variant) | ||
for variant in self.graphql_product.get("variants", {}).get("nodes", []) | ||
] | ||
|
||
def _cast_values(self, data, mappings): | ||
""" | ||
Recursively traverse and cast values in a dictionary or list based on mappings. | ||
:param data: The data to process (dictionary, list, or scalar). | ||
:param mappings: The mapping dictionary to use for casting. | ||
:return: The processed data with values cast according to the mappings. | ||
""" | ||
if isinstance(data, dict): | ||
return { | ||
key: self._cast_values(value, mappings.get(key, {})) | ||
for key, value in data.items() | ||
} | ||
elif isinstance(data, list): | ||
return [self._cast_values(item, mappings) for item in data] | ||
elif data in mappings: | ||
return mappings[data] | ||
return data | ||
|
||
def to_dict(self): | ||
"""Return the REST API-compatible product as a dictionary.""" | ||
product_dict = { | ||
"admin_graphql_api_id": self.graphql_product["id"], | ||
"body_html": self.graphql_product["descriptionHtml"] or "", | ||
"created_at": self.graphql_product["createdAt"], | ||
"handle": self.graphql_product["handle"], | ||
"id": self.product_id, | ||
"image": None, # No longer supported by GraphQL API | ||
"product_type": self.graphql_product["productType"], | ||
"published_at": self.graphql_product["publishedAt"], | ||
"published_scope": None, # No longer supported by GraphQL API | ||
"status": self.graphql_product["status"], | ||
"tags": ", ".join(self.graphql_product["tags"]), | ||
"template_suffix": self.graphql_product["templateSuffix"], | ||
"title": self.graphql_product["title"], | ||
"updated_at": self.graphql_product["updatedAt"], | ||
"vendor": self.graphql_product["vendor"], | ||
"options": self._convert_options(), | ||
"images": self._convert_images(), | ||
"variants": self._convert_variants() | ||
} | ||
|
||
return self._cast_values(product_dict, self.value_map) |
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,19 @@ | ||
{ | ||
"status": { | ||
"ACTIVE": "active", | ||
"ARCHIVED": "archived", | ||
"DRAFT": "draft" | ||
}, | ||
"variants": { | ||
"inventory_policy": { | ||
"DENY": "deny", | ||
"CONTINUE": "continue" | ||
}, | ||
"weight_unit": { | ||
"GRAMS": "g", | ||
"KILOGRAMS": "kg", | ||
"OUNCES": "oz", | ||
"POUNDS": "lb" | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,138 @@ | ||
import shopify | ||
|
||
from tap_shopify.streams.base import Stream | ||
from tap_shopify.streams.base import (Stream, shopify_error_handling) | ||
from tap_shopify.context import Context | ||
import json | ||
from datetime import timedelta | ||
import singer | ||
from singer.utils import strftime | ||
from tap_shopify.streams.compatibility.product_compatibility import ProductCompatibility | ||
|
||
LOGGER = singer.get_logger() | ||
|
||
class Products(Stream): | ||
name = 'products' | ||
replication_object = shopify.Product | ||
status_key = "published_status" | ||
|
||
gql_query = """ | ||
query GetProducts($query: String, $cursor: String) { | ||
products(first: 250, after: $cursor, query: $query) { | ||
nodes { | ||
status | ||
publishedAt | ||
createdAt | ||
vendor | ||
updatedAt | ||
descriptionHtml | ||
productType | ||
tags | ||
handle | ||
templateSuffix | ||
title | ||
id | ||
options { | ||
id | ||
name | ||
position | ||
values | ||
} | ||
images(first: 250) { | ||
nodes { | ||
id | ||
altText | ||
src | ||
height | ||
width | ||
} | ||
} | ||
variants(first: 100) { | ||
nodes { | ||
id | ||
title | ||
sku | ||
position | ||
price | ||
compareAtPrice | ||
weight | ||
weightUnit | ||
inventoryPolicy | ||
inventoryQuantity | ||
taxable | ||
taxCode | ||
updatedAt | ||
image { | ||
id | ||
} | ||
inventoryItem { | ||
id | ||
requiresShipping | ||
} | ||
createdAt | ||
barcode | ||
fulfillmentService { | ||
handle | ||
} | ||
selectedOptions { | ||
name | ||
value | ||
} | ||
} | ||
} | ||
} | ||
pageInfo { | ||
hasNextPage | ||
endCursor | ||
} | ||
} | ||
} | ||
""" | ||
|
||
@shopify_error_handling | ||
def call_api_for_products(self, gql_client, query, cursor=None): | ||
variables = { | ||
"query": query, | ||
"cursor": cursor | ||
} | ||
response = gql_client.execute(self.gql_query, variables) | ||
result = json.loads(response) | ||
if result.get("errors"): | ||
raise Exception(result['errors']) | ||
return result | ||
|
||
def get_products(self, updated_at_min, updated_at_max, cursor=None): | ||
gql_client = shopify.GraphQL() | ||
query = f"updated_at:>'{updated_at_min.isoformat()}' AND updated_at:<'{updated_at_max.isoformat()}'" | ||
page = self.call_api_for_products(gql_client, query, cursor) | ||
return page | ||
|
||
def get_objects(self): | ||
updated_at_min = self.get_bookmark() | ||
stop_time = singer.utils.now().replace(microsecond=0) | ||
date_window_size = float(Context.config.get("date_window_size", 1)) | ||
|
||
while updated_at_min < stop_time: | ||
updated_at_max = updated_at_min + timedelta(days=date_window_size) | ||
if updated_at_max > stop_time: | ||
updated_at_max = stop_time | ||
|
||
LOGGER.info(f"Fetching products updated between {updated_at_min} and {updated_at_max}") | ||
cursor = None | ||
|
||
while True: | ||
page = self.get_products(updated_at_min, updated_at_max, cursor) | ||
products = page['data']['products']['nodes'] | ||
page_info = page['data']['products']['pageInfo'] | ||
|
||
for product in products: | ||
yield ProductCompatibility(product) | ||
|
||
# Update the cursor and check if there's another page | ||
if page_info['hasNextPage']: | ||
cursor = page_info['endCursor'] | ||
else: | ||
break | ||
|
||
# Update the bookmark for the next batch | ||
updated_at_min = updated_at_max | ||
self.update_bookmark(strftime(updated_at_min)) | ||
|
||
Context.stream_objects['products'] = Products |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was added to support the metafields stream, which calls this method here.
Unfortunately, it seems that the request
shopify.resources.Metafield.find
issues looks like this:Which calls the
/products
endpoint. Looking into how to resolve this.