Skip to content

Commit 9063964

Browse files
committed
Add support for v3
- Pass through api_path object to connection - Append catalog word to resource url - Ignore meta field when returning response
1 parent af02613 commit 9063964

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ 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+
url = 'catalog/{}'.format(url)
59+
5660
# make full path if not given
5761
if url and url[:4] != "http":
5862
if url[0] == '/': # can call with /resource if you want
@@ -156,6 +160,9 @@ def _handle_response(self, url, res, suppress_empty=True):
156160
if res.status_code in (200, 201, 202):
157161
try:
158162
result = res.json()
163+
# Support v3
164+
if self.api_path and 'v3' in self.api_path:
165+
result = result['data'] #TODO ignore meta field for now
159166
except Exception as e: # json might be invalid, or store might be down
160167
e.message += " (_handle_response failed to decode JSON: " + str(res.content) + ")"
161168
raise # TODO better exception
@@ -187,11 +194,11 @@ class OAuthConnection(Connection):
187194
"""
188195

189196
def __init__(self, client_id, store_hash, access_token=None, host='api.bigcommerce.com',
190-
api_path='/stores/{}/v2/{}', rate_limiting_management=None):
197+
api_path=None, rate_limiting_management=None):
191198
self.client_id = client_id
192199
self.store_hash = store_hash
193200
self.host = host
194-
self.api_path = api_path
201+
self.api_path = api_path if api_path else "/stores/{}/v2/{}"
195202
self.timeout = 7.0 # can attach to session?
196203
self.rate_limiting_management = rate_limiting_management
197204

0 commit comments

Comments
 (0)