54
54
VALID_PREFIXES = {'dataset' : 'datasets/' }
55
55
56
56
57
+ def get_api_state (state = None , ** kwargs ):
58
+ state = state or State (is_using_cli = False )
59
+
60
+ if 'debug' in kwargs .keys ():
61
+ state .debug = kwargs .get ('debug' )
62
+ if 'verbose' in kwargs .keys ():
63
+ state .verbose = kwargs .get ('verbose' )
64
+ if 'organisation' in kwargs .keys ():
65
+ state .organisation = kwargs .get ('organisation' )
66
+
67
+ return state
68
+
69
+
57
70
def set_api_factory (cls ):
58
71
def factory (endpoint_class , state , ** kwargs ):
59
72
return ArcsecondAPI (endpoint_class , state , ** kwargs )
@@ -82,18 +95,11 @@ def register(cls, username, email, password1, password2, state=None):
82
95
83
96
class ArcsecondAPI (object ):
84
97
def __init__ (self , endpoint_class = None , state = None , ** kwargs ):
85
- self .state = state or State ( is_using_cli = False )
98
+ self .state = get_api_state ( state , ** kwargs )
86
99
87
100
if not self .__class__ .is_logged_in (self .state ):
88
101
raise ArcsecondNotLoggedInError ()
89
102
90
- if 'debug' in kwargs .keys ():
91
- self .state .debug = kwargs .get ('debug' )
92
- if 'verbose' in kwargs .keys ():
93
- self .state .verbose = kwargs .get ('verbose' )
94
- if 'organisation' in kwargs .keys ():
95
- self .state .organisation = kwargs .get ('organisation' )
96
-
97
103
prefix = kwargs .get ('prefix' , '' )
98
104
possible_prefixes = set (kwargs .keys ()).intersection (VALID_PREFIXES .keys ())
99
105
if len (possible_prefixes ) > 1 :
@@ -224,11 +230,12 @@ def _get_and_save_api_key(cls, state, username, auth_token):
224
230
225
231
@classmethod
226
232
def is_logged_in (cls , state = None ):
233
+ state = get_api_state (state )
227
234
return config_file_read_api_key (debug = state .debug ) is not None
228
235
229
236
@classmethod
230
237
def login (cls , username , password , subdomain , state = None ):
231
- state = state or State ( )
238
+ state = get_api_state ( state )
232
239
result , error = AuthAPIEndPoint (state ).login (username , password )
233
240
if error :
234
241
return ArcsecondAPI ._echo_error (state , error )
@@ -240,7 +247,7 @@ def login(cls, username, password, subdomain, state=None):
240
247
241
248
@classmethod
242
249
def register (cls , username , email , password1 , password2 , state = None ):
243
- state = state or State ( )
250
+ state = get_api_state ( state )
244
251
result , error = AuthAPIEndPoint (state ).register (username , email , password1 , password2 )
245
252
if error :
246
253
return ArcsecondAPI ._echo_error (state , error )
0 commit comments