Modified the authenticate function for Impexium. It should be smarter now... It should only try to re-auth if something went wrong on another request.

This commit is contained in:
Scott Idem
2023-02-02 14:54:29 -05:00
parent 242fc146e9
commit 0e482a3ccd

View File

@@ -23,6 +23,8 @@ api = {}
api['base_url'] = 'https://public.impexium.com/Api/v1' # or https://ishlt.mpxapi.com:443/api/v1 ??
api['headers'] = { 'Content-Type': 'application/json;charset=UTF-8' }
api['access_token'] = None
api['access_token_datetime'] = None # This is NOT generated by Impexium
api['app_user_token_datetime'] = None # This is NOT generated by Impexium
# ### BEGIN ### API External Impexium Methods ### get_access_token() ###
@@ -46,6 +48,7 @@ def get_access_token():
log.debug(f'Headers: {resp.headers}')
# log.debug(f'Encoding: {resp.encoding}')
if resp.status_code == 200 and resp.json():
log.info('Got access token. Setting API header and related...')
log.debug(f'JSON: {resp.json()}')
else:
log.warning('No JSON data found')
@@ -59,6 +62,9 @@ def get_access_token():
api['headers']['AccessToken'] = response_data['accessToken']
api['auth_uri'] = response_data['uri']
api['access_token_datetime'] = datetime.datetime.utcnow() # This is NOT generated by Impexium
log.info('Finished setting API access token information.')
log.debug(api)
return True
@@ -72,7 +78,15 @@ def authenticate():
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if api.get('access_token'): pass
if api.get('access_token') and api.get('app_user_token_datetime'):
log.info(f'Found API Access Token and timestamps: Access={api.get("access_token_datetime")}; App/User={api.get("app_user_token_datetime")}')
log.debug(api.get('access_token'))
# log.debug(api.get('access_token_datetime'))
# log.debug(api.get('app_user_token_datetime'))
return True
elif api.get('access_token'): # pass
log.info('Found API Access Token but no timestamp. Need to authenticate.')
log.debug(api.get('access_token'))
else:
log.warning('Access token not found. Calling get_access_token()...')
if result := get_access_token():
@@ -101,8 +115,9 @@ def authenticate():
if resp.status_code == 200:
log.info('Got a result from request')
if resp.json():
log.debug(f'JSON: {resp.json()}')
# log.debug(f'JSON: {resp.json()}')
response_data = resp.json()
log.debug(json.dumps(response_data, indent=2, default=str))
else:
log.warning('No JSON data found')
response_data = None
@@ -120,6 +135,8 @@ def authenticate():
api['headers']['UserToken'] = response_data['userToken']
api['base_url'] = response_data['uri']
api['app_user_token_datetime'] = datetime.datetime.utcnow() # This is NOT generated by Impexium
log.debug(api)
return api
@@ -262,10 +279,14 @@ def get_event_registrants(
try_request = True
response_data = False
else:
log.info('Not trying again')
try_request = False
try_page = False
impexium_event_registration_list = False
log.warning('Something may have gone wrong. Setting the API app_user_token_datetime value to None to re-authenticate with Impexium on the next request.')
api['app_user_token_datetime'] = None # Resetting this just in case the App and or User token expired.
return impexium_event_registration_list
# ### END ### API Impexium Methods ### get_event_registrants() ###
@@ -335,6 +356,9 @@ def get_individual_profile(
try_request = False
impexium_event_registration_list = False
log.warning('Something may have gone wrong. Setting the API app_user_token_datetime value to None to re-authenticate with Impexium on the next request.')
api['app_user_token_datetime'] = None # Resetting this just in case the App and or User token expired.
return impexium_individual_profile
# ### END ### API External Impexium Methods ### get_individual_profile() ###