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 mispelled param name #7

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
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
Expand Up @@ -63,7 +63,7 @@ order = client.orders.close(resource_id=1234)

```python
# Initialize the client
client = ShopifyClient(api_url='your_api_url', api_token='your_token', api_version='your_api_version', grapgql_queries_dir="queries")
client = ShopifyClient(api_url='your_api_url', api_token='your_token', api_version='your_api_version', graphql_queries_dir="queries")

# queries/listProducts.graphql
query products($page_size: Int = 100) {
Expand All @@ -82,7 +82,7 @@ response = client.query(query_name="listProducts")
# Limit page size
response = client.query(query, variables={"page_size": 20})

# Use pagination.
# Use pagination.
# Note that "pageIngo" block with at least "hasNextPage" & "startCursor" is required
# $cursor value should be passed as "after" parameter
query = '''
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.2
0.0.3
12 changes: 6 additions & 6 deletions shopify_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

class ShopifyClient(requests.Session):

def __init__(self, api_url, api_token, api_version=SHOPIFY_API_VERSION, grapgql_queries_dir=None):
def __init__(self, api_url, api_token, api_version=SHOPIFY_API_VERSION, graphql_queries_dir=None):
super().__init__()
self.api_url = api_url
self.api_version = api_version
self.headers.update({"X-Shopify-Access-Token": api_token, "Content-Type": "application/json"})

# Access
self.storefront_access_tokens = Endpoint(client=self, endpoint="storefront_access_tokens")

# Billing
self.application_charges = Endpoint(client=self, endpoint="application_charges")
self.application_credits = Endpoint(client=self, endpoint="application_credits")
Expand Down Expand Up @@ -104,20 +104,20 @@ def __init__(self, api_url, api_token, api_version=SHOPIFY_API_VERSION, grapgql_

# Tender Transactions
self.tender_transactions = Endpoint(client=self, endpoint="tender_transactions")

# Webhooks
self.webhooks = Endpoint(client=self, endpoint="webhooks")

# GraphQL
self.query = GraphQL(client=self, grapgql_queries_dir=grapgql_queries_dir)
self.query = GraphQL(client=self, graphql_queries_dir=graphql_queries_dir)

self.hooks["response"].append(rate_limit)

def request(self, method, url, *args, **kwargs):
response = super().request(method, urljoin(f"{self.api_url}/admin/api/{self.api_version}/", url), *args, **kwargs)
logger.info(f"Requesting {method} {url}: {response.status_code}")
return response

def parse_response(self, response):
try:
response.raise_for_status()
Expand Down
16 changes: 8 additions & 8 deletions shopify_client/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@

class GraphQL:

def __init__(self, client, grapgql_queries_dir=None):
def __init__(self, client, graphql_queries_dir=None):
self.client = client
self.endpoint = "graphql.json"
self.grapgql_queries_dir = grapgql_queries_dir
self.graphql_queries_dir = graphql_queries_dir

def __build_url(self, **params):
return self.endpoint

def __call__(self, *args, **kwargs):
return self.__query(*args, **kwargs)

def query_from_name(self, name):
assert self.grapgql_queries_dir, "GraphQL queries directory is not set"
assert self.graphql_queries_dir, "GraphQL queries directory is not set"

query_path = os.path.join(self.grapgql_queries_dir, f"{name}.graphql")
query_path = os.path.join(self.graphql_queries_dir, f"{name}.graphql")
with open(query_path, "r") as f:
return f.read()

Expand All @@ -32,7 +32,7 @@ def __query(self, query=None, query_name=None, variables=None, operation_name=No

if query is None and query_name:
query = self.query_from_name(query_name)

if paginate:
return self.__paginate(query=query, variables=variables, operation_name=operation_name, page_size=page_size)
try:
Expand All @@ -47,7 +47,7 @@ def __query(self, query=None, query_name=None, variables=None, operation_name=No
except json.JSONDecodeError as e:
logger.warning(f"Failed to parse JSON response: {repr(e)}")
return {}

def __paginate(self, query, variables=None, operation_name=None, page_size=100):
assert "pageInfo" in query, "Query must contain a 'pageInfo' object to be paginated"

Expand Down
6 changes: 3 additions & 3 deletions tests/test_graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_graphql_query(graphql, mock_client):

def test_graphql_query_with_query_name(graphql, mock_client):
mock_query_content = "query { items { id } }"
graphql.grapgql_queries_dir = "queries"
graphql.graphql_queries_dir = "queries"
with patch("builtins.open", mock_open(read_data=mock_query_content)):
mock_client.post.return_value = {"data": {"items": []}}
response = graphql(query_name="test_query")
Expand Down Expand Up @@ -88,10 +88,10 @@ def test_graphql_query_paginated(graphql, mock_client, mocker):
}
}
}

mock_client.post = CopyingMock(side_effect = [mock_paginated_response_1, mock_paginated_response_2])
response = list(graphql(query="query { items { id } pageInfo { hasNextPage, endCursor } }", paginate=True))

assert response == [mock_paginated_response_1, mock_paginated_response_2]
assert mock_client.post.call_count == 2
mock_client.post.assert_has_calls([
Expand Down