Skip to content

Commit 83c2765

Browse files
tgsergeantTimothy Sergeant
authored andcommitted
Reorganise to use an api class rather than a global connection
1 parent a5ff30f commit 83c2765

31 files changed

Lines changed: 479 additions & 445 deletions

bigcommerce/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
from bigcommerce.api.connection import Connection, HttpException, ClientRequestException, \
2-
EmptyResponseWarning, RedirectionException, ServerException
3-
from bigcommerce.api.oauthconnection import OAuthConnection
1+
# from bigcommerce.connection import Connection, OAuthConnection, HttpException, ClientRequestException, \
2+
# EmptyResponseWarning, RedirectionException, ServerException
3+
import resources
4+
import api

bigcommerce/api.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import sys
2+
from bigcommerce import connection
3+
from bigcommerce.resources import *
4+
5+
6+
class BigcommerceApi(object):
7+
8+
def __init__(self, store_endpoint=None, basic_auth=None, client_id=None, store_hash=None, access_token=None):
9+
if store_endpoint and basic_auth:
10+
self.connection = connection.Connection(store_endpoint, basic_auth)
11+
elif client_id and store_hash:
12+
self.connection = connection.OAuthConnection(client_id, store_hash, access_token)
13+
else:
14+
raise Exception("Must provide either (client_id and store_hash) or (store_endpoint and basic_auth)")
15+
16+
def oauth_fetch_token(self, client_secret, code, context, scope, redirect_uri):
17+
if isinstance(self.connection, connection.OAuthConnection):
18+
return self.connection.fetch_token(client_secret, code, context, scope, redirect_uri)
19+
20+
@classmethod
21+
def oauth_verify_payload(cls, signed_payload, client_secret):
22+
return connection.OAuthConnection.verify_payload(signed_payload, client_secret)
23+
24+
def __getattr__(self, item):
25+
return ApiResourceWrapper(item, self)
26+
27+
28+
class ApiResourceWrapper(object):
29+
def __init__(self, resource_class, api):
30+
if isinstance(resource_class, str):
31+
self.resource_class = self.str_to_class(resource_class)
32+
else:
33+
self.resource_class = resource_class
34+
self.connection = api.connection
35+
36+
def __getattr__(self, item):
37+
return lambda *args, **kwargs: (getattr(self.resource_class, item))(*args, connection=self.connection, **kwargs)
38+
39+
def str_to_class(self, str):
40+
return getattr(sys.modules[__name__], str)

bigcommerce/api/__init__.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

bigcommerce/api/auth.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

bigcommerce/api/resources/base.py

Lines changed: 0 additions & 108 deletions
This file was deleted.

0 commit comments

Comments
 (0)