Skip to content

added new stream and update shopify library #12

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

Merged
merged 4 commits into from
Dec 16, 2022
Merged
Changes from all 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -25,9 +25,9 @@ This tap:

## Quick Start

1. Install
1. Install the tap locally

pip install tap-shopify
pip install -e .

2. Create the config file

4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -3,15 +3,15 @@

setup(
name="tap-shopify",
version="1.4.12",
version="1.4.13",
description="Singer.io tap for extracting Shopify data",
author="Stitch",
url="http://github.com/singer-io/tap-shopify",
classifiers=["Programming Language :: Python :: 3 :: Only"],
python_requires='>=3.5.2',
py_modules=["tap_shopify"],
install_requires=[
"ShopifyAPI==8.4.1",
"ShopifyAPI==12.1.0",
"singer-python==5.12.1",
],
extras_require={
2 changes: 1 addition & 1 deletion tap_shopify/__init__.py
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@
def initialize_shopify_client():
api_key = Context.config.get('access_token', Context.config.get("api_key"))
shop = Context.config['shop']
version = '2021-04'
version = '2022-10'
session = shopify.Session(shop, version, api_key)
shopify.ShopifyResource.activate_session(session)
# Shop.current() makes a call for shop details with provided shop and api_key
20 changes: 20 additions & 0 deletions tap_shopify/schemas/product_category.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"type": "object",
"properties": {
"id": {
"type": ["string", "null"]
},
"category_id": {
"type": ["string", "null"]
},
"full_name": {
"type": ["string", "null"]
},
"is_leaf": {
"type": ["boolean", "null"]
},
"is_root": {
"type": ["boolean", "null"]
}
}
}
1 change: 1 addition & 0 deletions tap_shopify/streams/__init__.py
Original file line number Diff line number Diff line change
@@ -16,3 +16,4 @@
import tap_shopify.streams.incoming_items
import tap_shopify.streams.events_products
import tap_shopify.streams.smart_collections
import tap_shopify.streams.product_category
80 changes: 80 additions & 0 deletions tap_shopify/streams/product_category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import json
import os
import sys
import shopify
import singer
from singer.utils import strftime, strptime_to_utc

from tap_shopify.context import Context
from tap_shopify.streams.base import Stream, shopify_error_handling

LOGGER = singer.get_logger()


class HiddenPrints:
def __enter__(self):
self._original_stdout = sys.stdout
sys.stdout = open(os.devnull, 'w')

def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdout.close()
sys.stdout = self._original_stdout


class ProductCategory(Stream):
name = 'product_category'
replication_key = 'createdAt'
gql_query = """
query product($id: ID!){
product(id: $id){
id,
productType,
createdAt,
productCategory{
productTaxonomyNode{
id,
fullName,
isLeaf,
isRoot

}
}
}
}
"""

@shopify_error_handling
def call_api_for_product_categories(self, parent_object):
gql_client = shopify.GraphQL()
with HiddenPrints():
response = gql_client.execute(self.gql_query, dict(id=parent_object.admin_graphql_api_id))
return json.loads(response)

def get_objects(self):
selected_parent = Context.stream_objects['products']()
selected_parent.name = "products_categories"
for parent_object in selected_parent.get_objects():
product_category = self.call_api_for_product_categories(parent_object)
item = product_category["data"].get("product")
if item.get('productCategory'):
item['category_id'] = item['productCategory']['productTaxonomyNode']['id']
item['full_name'] = item['productCategory']['productTaxonomyNode']['fullName']
item['is_leaf'] = item['productCategory']['productTaxonomyNode']['isLeaf']
item['is_root'] = item['productCategory']['productTaxonomyNode']['isRoot']
yield item

def sync(self):
bookmark = self.get_bookmark()
self.max_bookmark = bookmark
for incoming_item in self.get_objects():
replication_value = strptime_to_utc(incoming_item[self.replication_key])
if replication_value >= bookmark:

yield incoming_item
if replication_value > self.max_bookmark:
self.max_bookmark = replication_value

self.update_bookmark(strftime(self.max_bookmark))


Context.stream_objects['product_category'] = ProductCategory