Skip to content

Commit 0d7207e

Browse files
authored
update to API v3 (#1)
1 parent 38cb03b commit 0d7207e

File tree

6 files changed

+49
-6
lines changed

6 files changed

+49
-6
lines changed

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ Connecting
2020
2121
# Public apps (OAuth)
2222
# Access_token is optional, if you don't have one you can use oauth_fetch_token (see below)
23+
# For connecting to the v2 api:
2324
api = bigcommerce.api.BigcommerceApi(client_id='', store_hash='', access_token='')
25+
# For connecting to the v3 api:
26+
api = bigcommerce.api.BigcommerceApi(client_id='', store_hash='', access_token='', api_path='/stores/{}/v3/{}'))
2427
2528
# Private apps (Basic Auth)
2629
api = bigcommerce.api.BigcommerceApi(host='store.mybigcommerce.com', basic_auth=('username', 'api token'))

bigcommerce/api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
class BigcommerceApi(object):
8-
def __init__(self, host=None, basic_auth=None,
8+
def __init__(self, host=None, api_path=None, basic_auth=None,
99
client_id=None, store_hash=None, access_token=None, rate_limiting_management=None):
1010
self.api_service = os.getenv('BC_API_ENDPOINT', 'api.bigcommerce.com')
1111
self.auth_service = os.getenv('BC_AUTH_SERVICE', 'login.bigcommerce.com')
@@ -14,6 +14,7 @@ def __init__(self, host=None, basic_auth=None,
1414
self.connection = connection.Connection(host, basic_auth)
1515
elif client_id and store_hash:
1616
self.connection = connection.OAuthConnection(client_id, store_hash, access_token, self.api_service,
17+
api_path=api_path,
1718
rate_limiting_management=rate_limiting_management)
1819
else:
1920
raise Exception("Must provide either (client_id and store_hash) or (host and basic_auth)")

bigcommerce/connection.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ def _run_method(self, method, url, data=None, query=None, headers=None):
5353
if headers is None:
5454
headers = {}
5555

56+
# Support v3
57+
if self.api_path and 'v3' in self.api_path:
58+
if url is 'orders':
59+
self.api_path = self.api_path.replace('v3', 'v2')
60+
else:
61+
url = 'catalog/{}'.format(url)
62+
5663
# make full path if not given
5764
if url and url[:4] != "http":
5865
if url[0] == '/': # can call with /resource if you want
@@ -156,6 +163,9 @@ def _handle_response(self, url, res, suppress_empty=True):
156163
if res.status_code in (200, 201, 202):
157164
try:
158165
result = res.json()
166+
# Support v3
167+
if self.api_path and 'v3' in self.api_path:
168+
result = result['data'] # TODO ignore meta field for now
159169
except Exception as e: # json might be invalid, or store might be down
160170
e.message += " (_handle_response failed to decode JSON: " + str(res.content) + ")"
161171
raise # TODO better exception
@@ -187,11 +197,12 @@ class OAuthConnection(Connection):
187197
"""
188198

189199
def __init__(self, client_id, store_hash, access_token=None, host='api.bigcommerce.com',
190-
api_path='/stores/{}/v2/{}', rate_limiting_management=None):
200+
api_path=None, rate_limiting_management=None):
191201
self.client_id = client_id
192202
self.store_hash = store_hash
193203
self.host = host
194204
self.api_path = api_path
205+
self.api_path = api_path if api_path else "/stores/{}/v2/{}"
195206
self.timeout = 7.0 # can attach to session?
196207
self.rate_limiting_management = rate_limiting_management
197208

bigcommerce/resources/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020
from .store import *
2121
from .tax_classes import *
2222
from .time import *
23+
from .variants import *
2324
from .webhooks import *

bigcommerce/resources/products.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,17 @@ def rules(self, id=None):
4848
else:
4949
return ProductRules.all(self.id, connection=self._connection)
5050

51-
def skus(self, id=None):
51+
def skus(self, id=None, **kwargs):
5252
if id:
53-
return ProductSkus.get(self.id, id, connection=self._connection)
53+
return ProductSkus.get(self.id, id, connection=self._connection, **kwargs)
5454
else:
55-
return ProductSkus.all(self.id, connection=self._connection)
55+
return ProductSkus.all(self.id, connection=self._connection, **kwargs)
56+
57+
def variants(self, id=None, **kwargs):
58+
if id:
59+
return ProductVariants.get(self.id, id, connection=self._connection, **kwargs)
60+
else:
61+
return ProductVariants.all(self.id, connection=self._connection, **kwargs)
5662

5763
def videos(self, id=None):
5864
if id:
@@ -99,7 +105,9 @@ class ProductImages(ListableApiSubResource, CreateableApiSubResource,
99105
count_resource = 'products/images'
100106

101107

102-
class ProductOptions(ListableApiSubResource):
108+
class ProductOptions(ListableApiSubResource, CreateableApiSubResource,
109+
UpdateableApiSubResource, DeleteableApiSubResource,
110+
CollectionDeleteableApiSubResource, CountableApiSubResource):
103111
resource_name = 'options'
104112
parent_resource = 'products'
105113
parent_key = 'product_id'
@@ -132,6 +140,15 @@ class ProductSkus(ListableApiSubResource, CreateableApiSubResource,
132140
count_resource = 'products/skus'
133141

134142

143+
class ProductVariants(ListableApiSubResource, CreateableApiSubResource,
144+
UpdateableApiSubResource, DeleteableApiSubResource,
145+
CollectionDeleteableApiSubResource, CountableApiSubResource):
146+
resource_name = 'variants'
147+
parent_resource = 'products'
148+
parent_key = 'product_id'
149+
count_resource = 'products/variants'
150+
151+
135152
class ProductVideos(ListableApiSubResource, CountableApiSubResource,
136153
CreateableApiSubResource, DeleteableApiSubResource,
137154
CollectionDeleteableApiSubResource):

bigcommerce/resources/variants.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from .base import *
2+
3+
4+
class Variants(ListableApiResource, CreateableApiSubResource,
5+
UpdateableApiSubResource, DeleteableApiSubResource,
6+
CollectionDeleteableApiSubResource, CountableApiSubResource):
7+
resource_name = 'variants'
8+
parent_resource = 'products'
9+
parent_key = 'product_id'
10+
count_resource = 'products/variants'

0 commit comments

Comments
 (0)