7
7
8
8
class Api :
9
9
10
- def __init__ (self , api_key : str = None ):
10
+ def __init__ (self , api_key : str = None , base : str = None ):
11
11
"""
12
12
API can be initialized directly by passing string, if not it looks for prevedere_api.ini in current working directory.
13
13
Copy the prevedere_api.ini.example file and remove `.example` from the end.
@@ -38,12 +38,16 @@ def __init__(self, api_key: str = None):
38
38
raise FileNotFoundError ('prevedere_api.ini config file not found in directory: ' + str (filepath .parent ))
39
39
logging .exception ()
40
40
41
+ if base :
42
+ self .base = base
43
+ logging .debug (f'Using "{ self .base } " instead of "api."' )
44
+ else :
45
+ self .base = 'api'
46
+
41
47
try :
42
48
self .api_key = str (UUID (api_key ))
43
- company = self .fetch ('/company' )
44
- if len (company ) == 1 :
45
- self .company = company [0 ]
46
- logging .info ('Successfully validated as ' + self .company ['Name' ])
49
+ self .context = self .fetch ('/context' )
50
+ logging .debug (f"Hello { self .context ['User' ]['FirstName' ]} , you're now connected to the { self .context ['Company' ]['Prefix' ]} instance." )
47
51
except (TypeError , ValueError , requests .exceptions .HTTPError ) as e :
48
52
raise ApiKeyError (f"'{ api_key } ' is not a valid API Key. " + \
49
53
"Please check the config file or string that was passed to the constructor and try again." ) from e
@@ -53,7 +57,7 @@ def fetch(self, path: str, payload: dict = None) -> dict:
53
57
if payload is None :
54
58
payload = {}
55
59
payload ['ApiKey' ] = self .api_key
56
- url = f'https://api .prevedere.com{ path } '
60
+ url = f'https://{ self . base } .prevedere.com{ path } '
57
61
try :
58
62
r = requests .get (url , params = payload )
59
63
r .raise_for_status ()
0 commit comments