405 lines
13 KiB
Python
405 lines
13 KiB
Python
import datetime, json, pprint, pytz, random, requests, string, time
|
|
|
|
from typing import Dict, List, Optional, Set, Union
|
|
from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, validator
|
|
|
|
from app.db_sql import redis_lookup_id_random, sql_insert, sql_select, sql_update
|
|
from app.lib_general import log, logging, logger_reset
|
|
|
|
|
|
headers = { 'Content-Type': 'application/json;charset=UTF-8' }
|
|
|
|
app = {}
|
|
app['user_email'] = 'oneskyit_integration@oneskyit.com'
|
|
app['user_password'] = 'dAPpHDE6d5vLHjsk'
|
|
|
|
app['name'] = 'IshltOneSkyITLIVE'
|
|
app['key'] = '98yp4fa57mJX6nU4'
|
|
app['id'] = 'IshltOneSkyITLIVE'
|
|
app['password'] = '98yp4fa57mJX6nU4'
|
|
log.debug('App data', app)
|
|
|
|
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
|
|
|
|
|
|
# ### BEGIN ### API External Impexium Methods ### get_access_token() ###
|
|
# Updated 2022-02-18
|
|
@logger_reset
|
|
def get_access_token():
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
log.debug(f'App data:\n{app}')
|
|
log.debug(f'API data:\n{api}')
|
|
|
|
endpoint = '/WebApiUrl'
|
|
uri = api['base_url']+endpoint
|
|
|
|
data = { 'AppName': app['name'], 'AppKey': app['key'] }
|
|
|
|
resp = requests.post(url=uri, json=data, headers=api['headers'])
|
|
|
|
log.debug(f'Status Code: {resp.status_code}')
|
|
log.debug(f'Headers: {resp.headers}')
|
|
# log.debug(f'Encoding: {resp.encoding}')
|
|
if resp.status_code == 200 and resp.json():
|
|
log.debug(f'JSON: {resp.json()}')
|
|
else:
|
|
log.warning('No JSON data found')
|
|
# log.debug('Text:', resp.text)
|
|
|
|
response_data = resp.json()
|
|
log.debug(json.dumps(response_data, indent=2, default=str))
|
|
|
|
api['access_token'] = response_data['accessToken']
|
|
api['headers']['AccessToken'] = response_data['accessToken']
|
|
api['auth_uri'] = response_data['uri']
|
|
|
|
log.debug(api)
|
|
|
|
return api
|
|
# ### END ### API External Impexium Methods ### get_access_token() ###
|
|
|
|
|
|
# ### BEGIN ### API External Impexium Methods ### authenticate() ###
|
|
# Updated 2022-03-22
|
|
@logger_reset
|
|
def authenticate():
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
if api.get('access_token'): pass
|
|
else:
|
|
result = get_access_token()
|
|
log.debug(result)
|
|
|
|
log.debug(f'App data:\n{app}')
|
|
log.debug(f'API data:\n{api}')
|
|
|
|
data = { 'AppId': app['id'], 'AppPassword': app['password'], 'AppUserEmail': app['user_email'], 'AppUserPassword': app['user_password'] }
|
|
|
|
try_request = True
|
|
max_tries = 5
|
|
try_count = 0
|
|
while try_request and try_count < max_tries:
|
|
try_count = try_count + 1
|
|
|
|
resp = requests.post(url=api['auth_uri'], json=data, headers=api['headers'])
|
|
|
|
log.debug(f'Status Code: {resp.status_code}')
|
|
log.debug(f'Headers: {resp.headers}')
|
|
# log.debug(f'Encoding: {resp.encoding}')
|
|
# log.debug('Text:', resp.text)
|
|
|
|
if resp.status_code == 200:
|
|
log.info('Got a result from request')
|
|
if resp.json():
|
|
log.debug(f'JSON: {resp.json()}')
|
|
response_data = resp.json()
|
|
else:
|
|
log.warning('No JSON data found')
|
|
response_data = None
|
|
try_request = False
|
|
elif resp.status_code == 429:
|
|
log.warning('Hit rate limit. Sleeping for .1 seconds...')
|
|
time.sleep(.1)
|
|
try_request = True
|
|
else:
|
|
log.error('Unexpected result from request')
|
|
try_request = False
|
|
response_data = False
|
|
|
|
api['headers']['AppToken'] = response_data['appToken']
|
|
api['headers']['UserToken'] = response_data['userToken']
|
|
api['base_url'] = response_data['uri']
|
|
|
|
log.debug(api)
|
|
|
|
return api
|
|
# ### END ### API External Impexium Methods ### authenticate() ###
|
|
|
|
|
|
|
|
# ### BEGIN ### API External Impexium Methods ### get_custom_fields() ###
|
|
# Updated 2022-02-18
|
|
@logger_reset
|
|
def get_custom_fields(api, name=None, page=1):
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
endpoint = f'/Setup/customfields/{page}'
|
|
uri = api['base_url']+endpoint
|
|
|
|
print('Endpoint URI', uri)
|
|
|
|
params = {}
|
|
|
|
resp = requests.get(url=uri, params=params, headers=api['headers'])
|
|
|
|
log.debug(f'Status Code: {resp.status_code}')
|
|
log.debug(f'Headers: {resp.headers}')
|
|
# log.debug(f'Encoding: {resp.encoding}')
|
|
log.debug(f'JSON: {resp.json()}')
|
|
# log.debug('Text:', resp.text)
|
|
|
|
response_data = resp.json()
|
|
|
|
custom_field_li = response_data
|
|
|
|
return custom_field_li
|
|
# ### END ### API External Impexium Methods ### get_custom_fields() ###
|
|
|
|
|
|
# ### BEGIN ### API External Impexium Methods ### get_events() ###
|
|
# Updated 2022-02-18
|
|
@logger_reset
|
|
def get_events(page=1):
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
endpoint = f'/Events/All/{page}'
|
|
uri = api['base_url']+endpoint
|
|
|
|
print('Endpoint URI', uri)
|
|
|
|
params = {}
|
|
|
|
resp = requests.get(url=uri, params=params, headers=api['headers'])
|
|
|
|
log.debug(f'Status Code: {resp.status_code}')
|
|
log.debug(f'Headers: {resp.headers}')
|
|
# log.debug(f'Encoding: {resp.encoding}')
|
|
log.debug(f'JSON: {resp.json()}')
|
|
# log.debug('Text:', resp.text)
|
|
|
|
response_data = resp.json()
|
|
|
|
# pp = pprint.PrettyPrinter(indent=2)
|
|
# pp.pprint(response_data)
|
|
# print('**************************')
|
|
|
|
event_li = response_data
|
|
|
|
return event_li
|
|
# ### END ### API External Impexium Methods ### get_events() ###
|
|
|
|
|
|
# ### BEGIN ### API External Impexium Methods ### get_event_registrants() ###
|
|
# Updated 2021-10-07
|
|
def get_event_registrants(
|
|
event_code: str,
|
|
registered_since: datetime.datetime = None,
|
|
details: bool = False,
|
|
page: int = 0,
|
|
# return_all: bool = False
|
|
):
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
result = authenticate()
|
|
log.debug(result)
|
|
|
|
try_page = True
|
|
|
|
if page > 0: # Only get the specific page
|
|
page_num = page
|
|
max_page = page
|
|
else: # Get all of the pages
|
|
page = 1
|
|
page_num = 0 # Will actually be the first page
|
|
max_page = 15
|
|
impexium_event_registration_list = []
|
|
while try_page and page_num <= max_page:
|
|
page_num = page_num + 1
|
|
log.info(f'Getting page number {page_num}')
|
|
|
|
endpoint = f'/Events/{event_code}/Registrations/{page_num}'
|
|
uri = api['base_url']+endpoint
|
|
params = { 'includeDetails': details }
|
|
if registered_since:
|
|
params['registeredSince'] = registered_since.isoformat()
|
|
log.debug(params)
|
|
|
|
try_request = True
|
|
max_tries = 5
|
|
try_count = 0
|
|
while try_request and try_count <= max_tries:
|
|
try_count = try_count + 1
|
|
|
|
resp = requests.get(url=uri, params=params, headers=api['headers'])
|
|
|
|
log.debug(f'Status Code: {resp.status_code}')
|
|
log.debug(f'Headers: {resp.headers}')
|
|
# log.debug(f'Encoding: {resp.encoding}')
|
|
# log.debug('Text:')
|
|
# log.debug(resp.text)
|
|
|
|
if resp.status_code == 200:
|
|
response_data = resp.json()
|
|
|
|
current_page = response_data.get('pageNumber')
|
|
log.info(f'Current Page: {current_page}')
|
|
|
|
impexium_event_registration_list = impexium_event_registration_list + response_data.get('dataList')
|
|
|
|
# page = current_page + 1
|
|
|
|
# log.warning('Avoiding rate limit. Sleeping for .1 seconds...')
|
|
# time.sleep(.1)
|
|
try_request = False
|
|
elif resp.status_code == 404:
|
|
try_request = False
|
|
try_page = False
|
|
elif resp.status_code == 429:
|
|
log.warning('Hit rate limit. Sleeping for .1 seconds...')
|
|
time.sleep(.1)
|
|
try_request = True
|
|
response_data = False
|
|
else:
|
|
try_request = False
|
|
try_page = False
|
|
impexium_event_registration_list = False
|
|
|
|
# if current_page >= 1:
|
|
# impexium_event_registration_list = impexium_event_registration_list + response_data.get('dataList')
|
|
|
|
# page = current_page + 1
|
|
|
|
# log.warning('Avoiding rate limit. Sleeping for .1 seconds...')
|
|
# time.sleep(.1)
|
|
# else:
|
|
# try_request = False
|
|
|
|
# loop_count = loop_count + 1
|
|
|
|
return impexium_event_registration_list
|
|
# ### END ### API Impexium Methods ### get_event_registrants() ###
|
|
|
|
|
|
# ### BEGIN ### API External Impexium Methods ### get_individual_registrations() ###
|
|
# Updated 2022-02-18
|
|
def get_individual_registrations(api, record_number, event_code=None, page=1):
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
endpoint = f'/Individuals/{record_number}/Registrations/{page}'
|
|
uri = api['base_url']+endpoint
|
|
|
|
params = { 'eventCode': event_code }
|
|
|
|
resp = requests.get(url=uri, params=params, headers=api['headers'])
|
|
|
|
log.debug(f'Status Code: {resp.status_code}')
|
|
log.debug(f'Headers: {resp.headers}')
|
|
# log.debug(f'Encoding: {resp.encoding}')
|
|
log.debug(f'JSON: {resp.json()}')
|
|
# log.debug('Text:', resp.text)
|
|
|
|
response_data = resp.json()
|
|
|
|
# pp = pprint.PrettyPrinter(indent=2)
|
|
# pp.pprint(response_data)
|
|
# print('**************************')
|
|
|
|
individual_registraion_li = response_data
|
|
|
|
return individual_registraion_li
|
|
# ### END ### API External Impexium Methods ### get_individual_registrations() ###
|
|
|
|
|
|
# ### BEGIN ### API External Impexium Methods ### get_individual_purchases() ###
|
|
# Updated 2022-02-18
|
|
def get_individual_purchases(api, record_number, product_code=None, purchased_since=None, product_category_code=None, page=1):
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
endpoint = f'/Individuals/{record_number}/Purchases/{page}'
|
|
uri = api['base_url']+endpoint
|
|
|
|
params = { 'productCode': product_code, 'purchasedSince': purchased_since, 'productCategoryCode': product_category_code }
|
|
|
|
resp = requests.get(url=uri, params=params, headers=api['headers'])
|
|
|
|
log.debug(f'Status Code: {resp.status_code}')
|
|
log.debug(f'Headers: {resp.headers}')
|
|
# log.debug(f'Encoding: {resp.encoding}')
|
|
log.debug(f'JSON: {resp.json()}')
|
|
# log.debug('Text:', resp.text)
|
|
|
|
response_data = resp.json()
|
|
|
|
# pp = pprint.PrettyPrinter(indent=2)
|
|
# pp.pprint(response_data)
|
|
# print('**************************')
|
|
|
|
individual_purchase_li = response_data
|
|
|
|
return individual_purchase_li
|
|
# ### END ### API External Impexium Methods ### get_individual_purchases() ###
|
|
|
|
|
|
# ### BEGIN ### API External Impexium Methods ### get_individual_custom_fields() ###
|
|
# Updated 2022-02-18
|
|
def get_individual_custom_fields(api, individual_id):
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
endpoint = f'/Individuals/{individual_id}/CustomFields'
|
|
uri = api['base_url']+endpoint
|
|
|
|
params = { }
|
|
|
|
resp = requests.get(url=uri, params=params, headers=api['headers'])
|
|
|
|
log.debug(f'Status Code: {resp.status_code}')
|
|
log.debug(f'Headers: {resp.headers}')
|
|
# log.debug(f'Encoding: {resp.encoding}')
|
|
log.debug(f'JSON: {resp.json()}')
|
|
# log.debug('Text:', resp.text)
|
|
|
|
response_data = resp.json()
|
|
|
|
# pp = pprint.PrettyPrinter(indent=2)
|
|
# pp.pprint(response_data)
|
|
# print('**************************')
|
|
|
|
individual_custom_field_li = response_data
|
|
|
|
return individual_custom_field_li
|
|
# ### END ### API External Impexium Methods ### get_individual_custom_fields() ###
|
|
|
|
|
|
# ### BEGIN ### API External Impexium Methods ### get_individual_orders_open() ###
|
|
# Updated 2022-02-18
|
|
def get_individual_orders_open(api, record_number, include_line_items=False, from_date=None, to_date=None, page=1):
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
endpoint = f'/Individuals/{record_number}/Orders/Open/{page}'
|
|
uri = api['base_url']+endpoint
|
|
print(uri)
|
|
|
|
params = { 'includeLineItems': include_line_items, 'fromDate': from_date, 'toDate': to_date }
|
|
|
|
resp = requests.get(url=uri, params=params, headers=api['headers'])
|
|
|
|
log.debug(f'Status Code: {resp.status_code}')
|
|
log.debug(f'Headers: {resp.headers}')
|
|
# log.debug(f'Encoding: {resp.encoding}')
|
|
log.debug(f'JSON: {resp.json()}')
|
|
# log.debug('Text:', resp.text)
|
|
|
|
response_data = resp.json()
|
|
|
|
# pp = pprint.PrettyPrinter(indent=2)
|
|
# pp.pprint(response_data)
|
|
# print('**************************')
|
|
|
|
individual_orders_open_li = response_data
|
|
|
|
return individual_orders_open_li
|
|
# ### END ### API External Impexium Methods ### get_individual_orders_open() ###
|