Skip to content

Commit

Permalink
Merge pull request #8 from groveco/ISS-4630-update-shopify-client-url…
Browse files Browse the repository at this point in the history
…-building-logic-for-sub-endpoints

Update shopify client url building logic for sub endpoints
  • Loading branch information
Anton-Shutik authored Nov 7, 2024
2 parents 0ef0c73 + 4e760b6 commit 2cff38f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
28 changes: 20 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,37 @@ for page in client.products.all(paginate=True, limit=100)
# Get specific product by id
product = client.products.get(resource_id=1234)

# List product metafields for product id
metafields = client.products.metafields.all(resource_id=1234)

# Get speficic metafield by id
metafield = client.products.metafields.all(resource_id=1234, sub_resource_id=5678)

# Create a product
data = {"product":{"title":"Burton Custom Freestyle 151","body_html":"<strong>Good snowboard!</strong>","vendor":"Burton","product_type":"Snowboard","status":"draft"}}
product = client.products.create(json=data)

# Update product
product = client.products.create(resource_id=1234, json=data)
product = client.products.update(resource_id=1234, json=data)

# Delete product
deleted = client.products.delete(resource=1234)
deleted = client.products.delete(resource_id=1234)

# Count of products
count = client.products.count()

# Get product metafields
metafields = client.products.metafields.all(resource_id=1234)

# Get product metafield
metafield = client.products.metafields.get(resource_id=1234, sub_resource_id=5678)

# Create product metafield
data = {"metafield": {"namespace": "warehouse", "key": "foo", "value": 25, "type": "integer"}}
metafield = client.products.metafields.create(resource_id=5678, json=data)

# Update product metafield
data = {"metafield": {"value": 30}}
metafield = client.products.metafields.update(resource_id=1234, sub_resource_id=5678, json=data)

# Delete product metafield
deleted = client.products.metafields.delete(resource_id=1234, sub_resource_id=5678)


# Cancel order
order = client.orders.cancel(resource_id=1234)

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.3
0.0.4
4 changes: 2 additions & 2 deletions shopify_client/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def get(self, resource_id, **params):
url = self.__build_url(resource_id=resource_id, **params)
return self.client.parse_response(self.client.get(url))

def create(self, json: dict):
url = self.__build_url()
def create(self, json: dict, **params):
url = self.__build_url(**params)
return self.client.parse_response(self.client.post(url, json=json))

def update(self, resource_id, json, **params):
Expand Down
25 changes: 25 additions & 0 deletions tests/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,31 @@ def test_build_url_with_params(endpoint):
url = endpoint._Endpoint__build_url(resource_id=1, action="test", param="value")
assert url == "test_endpoint/1/test.json?param=value"

def test_build_url_for_sub_endpoint_create(endpoint):
endpoint.sub_endpoint = "metafields"
url = endpoint._Endpoint__build_url(resource_id=1)
assert url == "test_endpoint/1/metafields.json"

def test_build_url_for_sub_endpoint_get(endpoint):
endpoint.sub_endpoint = "metafields"
url = endpoint._Endpoint__build_url(resource_id=1, sub_resource_id=2)
assert url == "test_endpoint/1/metafields/2.json"

def test_build_url_for_sub_endpoint_all(endpoint):
endpoint.sub_endpoint = "metafields"
url = endpoint._Endpoint__build_url(resource_id=1)
assert url == "test_endpoint/1/metafields.json"

def test_build_url_for_sub_endpoint_update(endpoint):
endpoint.sub_endpoint = "metafields"
url = endpoint._Endpoint__build_url(resource_id=1, sub_resource_id=2)
assert url == "test_endpoint/1/metafields/2.json"

def test_build_url_for_sub_endpoint_delete(endpoint):
endpoint.sub_endpoint = "metafields"
url = endpoint._Endpoint__build_url(resource_id=1, sub_resource_id=2)
assert url == "test_endpoint/1/metafields/2.json"

def test_get(endpoint, mock_client):
mock_client.get.return_value = {"result": "success"}
response = endpoint.get(1, param="value")
Expand Down

0 comments on commit 2cff38f

Please sign in to comment.