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 )
0 commit comments