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

Allow implicit options to be passed into product.sku #84

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion bigcommerce/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class BigcommerceApi(object):
def __init__(self, host=None, basic_auth=None,
def __init__(self, host=None, api_path=None, basic_auth=None,
client_id=None, store_hash=None, access_token=None, rate_limiting_management=None):
self.api_service = os.getenv('BC_API_ENDPOINT', 'api.bigcommerce.com')
self.auth_service = os.getenv('BC_AUTH_SERVICE', 'login.bigcommerce.com')
Expand All @@ -14,6 +14,7 @@ def __init__(self, host=None, basic_auth=None,
self.connection = connection.Connection(host, basic_auth)
elif client_id and store_hash:
self.connection = connection.OAuthConnection(client_id, store_hash, access_token, self.api_service,
api_path=api_path,
rate_limiting_management=rate_limiting_management)
else:
raise Exception("Must provide either (client_id and store_hash) or (host and basic_auth)")
Expand Down
11 changes: 9 additions & 2 deletions bigcommerce/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def _run_method(self, method, url, data=None, query=None, headers=None):
if headers is None:
headers = {}

# Support v3

Choose a reason for hiding this comment

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

@VDuda with this bit, the orders API no longer works, as this seems to have an explicit /v2/ path as per the docs here:
https://developer.bigcommerce.com/api-reference/orders/orders-api/orders/getallorders

This bit of the code injects catalog in when it is not needed, the url for orders is /store/{store_hash}/v2/orders

Using the code this way would require making a fresh connection for a v2 connection so we don't break the rest of the API

@bookernath am I missing anything with your API endpoints here?

if self.api_path and 'v3' in self.api_path:
url = 'catalog/{}'.format(url)

# make full path if not given
if url and url[:4] != "http":
if url[0] == '/': # can call with /resource if you want
Expand Down Expand Up @@ -156,6 +160,9 @@ def _handle_response(self, url, res, suppress_empty=True):
if res.status_code in (200, 201, 202):
try:
result = res.json()
# Support v3
if self.api_path and 'v3' in self.api_path:
result = result['data'] #TODO ignore meta field for now
except Exception as e: # json might be invalid, or store might be down
e.message += " (_handle_response failed to decode JSON: " + str(res.content) + ")"
raise # TODO better exception
Expand Down Expand Up @@ -187,11 +194,11 @@ class OAuthConnection(Connection):
"""

def __init__(self, client_id, store_hash, access_token=None, host='api.bigcommerce.com',
api_path='/stores/{}/v2/{}', rate_limiting_management=None):
api_path=None, rate_limiting_management=None):
self.client_id = client_id
self.store_hash = store_hash
self.host = host
self.api_path = api_path
self.api_path = api_path if api_path else "/stores/{}/v2/{}"
self.timeout = 7.0 # can attach to session?
self.rate_limiting_management = rate_limiting_management

Expand Down
1 change: 1 addition & 0 deletions bigcommerce/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
from .store import *
from .tax_classes import *
from .time import *
from .variants import *
from .webhooks import *
23 changes: 19 additions & 4 deletions bigcommerce/resources/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ def rules(self, id=None):
else:
return ProductRules.all(self.id, connection=self._connection)

def skus(self, id=None):
def skus(self, id=None, **kwargs):
if id:
return ProductSkus.get(self.id, id, connection=self._connection)
return ProductSkus.get(self.id, id, connection=self._connection, **kwargs)
else:
return ProductSkus.all(self.id, connection=self._connection)
return ProductSkus.all(self.id, connection=self._connection, **kwargs)

def variants(self, id=None, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer to have the V3 products-related endpoints in a completely separate file from the V2 ones - that namespacing will help a lot if we try to apply different logic to them (pagination comes to mind)

if id:
return ProductVariants.get(self.id, id, connection=self._connection, **kwargs)
else:
return ProductVariants.all(self.id, connection=self._connection, **kwargs)

def videos(self, id=None):
if id:
Expand Down Expand Up @@ -99,7 +105,9 @@ class ProductImages(ListableApiSubResource, CreateableApiSubResource,
count_resource = 'products/images'


class ProductOptions(ListableApiSubResource):
class ProductOptions(ListableApiSubResource, CreateableApiSubResource,
UpdateableApiSubResource, DeleteableApiSubResource,
CollectionDeleteableApiSubResource, CountableApiSubResource):
resource_name = 'options'
parent_resource = 'products'
parent_key = 'product_id'
Expand Down Expand Up @@ -131,6 +139,13 @@ class ProductSkus(ListableApiSubResource, CreateableApiSubResource,
parent_key = 'product_id'
count_resource = 'products/skus'

class ProductVariants(ListableApiSubResource, CreateableApiSubResource,
UpdateableApiSubResource, DeleteableApiSubResource,
CollectionDeleteableApiSubResource, CountableApiSubResource):
resource_name = 'variants'
parent_resource = 'products'
parent_key = 'product_id'
count_resource = 'products/variants'

class ProductVideos(ListableApiSubResource, CountableApiSubResource,
CreateableApiSubResource, DeleteableApiSubResource,
Expand Down
10 changes: 10 additions & 0 deletions bigcommerce/resources/variants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .base import *


class Variants(ListableApiResource, CreateableApiSubResource,
UpdateableApiSubResource, DeleteableApiSubResource,
CollectionDeleteableApiSubResource, CountableApiSubResource):
resource_name = 'variants'
parent_resource = 'products'
parent_key = 'product_id'
count_resource = 'products/variants'