def get_facilities(headers, instance): dir_path = os.path.dirname(os.path.realpath(__file__)) log_file = dir_path + api_const.log_file today_date = get_today_date_yyyymmdd() wkday = get_today_wkday() logging.basicConfig( filename = log_file, \ filemode = 'a', \ level = logging.INFO, \ format = "%(levelname)s %(name)s %(asctime)s %(lineno)s - %(message)s ") logger = logging.getLogger(__name__) logger.info(wkday + ". Starting GET FACILITIES API") api_url = api_const.api_url + instance + "/facilities" logger.info(wkday + ". API URL: " + api_url) response = requests.get(api_url, headers=headers) response_json = response.json() for i in response_json: uid = i["Uid"] if i["Code"] == "VHCDOWNTOWNCLINIC": continue logger.info(wkday + ". Facility UID is " + uid) return (uid)
def get_auth_token(instance="sccin"): dir_path = os.path.dirname(os.path.realpath(__file__)) output_file = dir_path + "/data/mdstaff_api_authentication.txt" hostname = socket.gethostname().lower().strip() log_file = "" password = "" facility_id = "" if instance == api_const.inpatient_instance: password = api_const.inpatient_password facility_id = api_const.inpatient_facility_id log_file = dir_path + api_const.inpatient_log_file elif instance == api_const.ambulatory_instance: password = api_const.ambulatory_password facility_id = api_const.ambulatory_facility_id log_file = dir_path + api_const.ambulatory_log_file wkday = get_today_wkday() logging.basicConfig( filename = log_file, \ filemode = 'a', \ level = logging.INFO, \ format = "%(levelname)s %(name)s %(asctime)s %(lineno)s - %(message)s ") logger = logging.getLogger(__name__) username = api_const.username credentials = { "grant_type": "password", "username": username, "password": password, "instance": instance, "facilityID": facility_id } logger.info(wkday + ". " + instance + ". Starting authentication") mdstaff_auth_url = "https://api.mdstaff.com/webapi/api/tokens" auth_response = requests.post(mdstaff_auth_url, data=credentials) # Read token from auth response auth_response_json = auth_response.json() auth_token = auth_response_json["access_token"] auth_token_type = auth_response_json["token_type"] auth_expire = auth_response_json["expires_in"] current_time = get_current_time() if hostname == "hhssvninappt001" or hostname == "hhssvninappp001": pass else: print("Token:\n" + auth_token + "\n") print("Current time: " + current_time) logger.info(wkday + ". " + instance + ". Completing authentication") return auth_token
def get_providers(headers, instance): today_date = get_today_date_yyyymmdd() wkday = get_today_wkday() dir_path = os.path.dirname(os.path.realpath(__file__)) log_file = "" hostname = socket.gethostname().lower().strip() if instance == api_const.inpatient_instance: password = api_const.inpatient_password facility_id = api_const.inpatient_facility_id if hostname == "hhssvninappt001" or hostname == "hhssvninappp001": log_file = "C:\\inetpub\\wwwroot\\oscarhur\\" + api_const.inpatient_log_file else: log_file = dir_path + api_const.inpatient_log_file elif instance == api_const.ambulatory_instance: password = api_const.ambulatory_password facility_id = api_const.ambulatory_facility_id if hostname == "hhssvninappt001" or hostname == "hhssvninappp001": log_file = "C:\\inetpub\\wwwroot\\oscarhur\\" + api_const.ambulatory_log_file else: log_file = dir_path + api_const.ambulatory_log_file output_file = dir_path + "/data/providerid_list_" + instance + ".txt" log_file = dir_path + log_file logging.basicConfig( filename = log_file, \ filemode = 'a', \ level = logging.INFO, \ format = "%(levelname)s %(name)s %(asctime)s %(lineno)s - %(message)s ") logger = logging.getLogger(__name__) logger.info(wkday + ". " + instance + ". Starting Get Provider API. Getting a list of ProviderID.") # an array of mdstaff providerid providerid_list = [] api_url = api_const.api_url + instance api_url = api_url + "/providers/facility" logger.info(wkday + ". " + instance + ". API URL: " + api_url) response = requests.get(api_url, headers=headers) response_json = response.json() with open(output_file, "w", newline='') as output_fh: output_writer = csv.writer(output_fh, delimiter='|') for i in response_json: output_writer.writerow([i["ProviderID"], i["Name"]]) providerid_list.append(i["ProviderID"]) logger.info(wkday + ". " + instance + ". Return code: " + str(response.status_code)) return providerid_list
def get_lookups(headers, instance, lookup_type): dir_path = os.path.dirname(os.path.realpath(__file__)) today_date = get_today_date_yyyymmdd() wkday = get_today_wkday() output_file = dir_path + "/data/" + instance + "_" + \ lookup_type + "_" + ".txt" log_file = "" hostname = socket.gethostname().lower().strip() if instance == "inpatient": instance = api_const.inpatient_instance if hostname == "hhssvninappt001" or hostname == "hhssvninappp001": log_file = "C:\\inetpub\\wwwroot\\oscarhur\\" + api_const.inpatient_log_file else: log_file = dir_path + api_const.inpatient_log_file elif instance == "ambulatory": instance = api_const.ambulatory_instance if hostname == "hhssvninappt001" or hostname == "hhssvninappp001": log_file = "C:\\inetpub\\wwwroot\\oscarhur\\" + api_const.ambulatory_log_file else: log_file = dir_path + api_const.ambulatory_log_file table_hash = {} logging.basicConfig( filename = log_file, \ filemode = 'a', \ level = logging.INFO, \ format = "%(levelname)s %(name)s %(asctime)s %(lineno)s - %(message)s ") logger = logging.getLogger(__name__) logger.info(wkday + ". " + instance + ". Starting GET LOOKUPS API " + lookup_type) api_url = api_const.api_url + instance + "/lookups/" + lookup_type logger.info(wkday + ". " + instance + ". API URL: " + api_url) response = requests.get(api_url, headers=headers) response_json = response.json() with open(output_file, "w", newline="") as output_fh: csv_writer = csv.writer(output_fh, delimiter="|") for i in response_json: id_code = i["ID"] description = i["Description"] csv_writer.writerow([description, id_code]) table_hash[id_code] = description logger.info(wkday + ". " + instance + ". Completing lookup.") return table_hash
def get_credential(instance, providerid_list): # provider_list list should be passed as a list of mdstaff providerid hostname = socket.gethostname().lower().strip() dir_path = os.path.dirname(os.path.realpath(__file__)) today_date = get_today_date_yyyymmdd() begin_time_obj = datetime.datetime.now() output_file = dir_path + "/data/credential_licnums_" + instance + ".txt" log_file = "" wkday = get_today_wkday() if instance == "inpatient": instance = api_const.inpatient_instance if hostname == "hhssvninappt001" or hostname == "hhssvninappp001": log_file = "C:\\inetpub\\wwwroot\\oscarhur\\" + api_const.inpatient_log_file else: log_file = dir_path + api_const.inpatient_log_file elif instance == "ambulatory": instance = api_const.ambulatory_instance if hostname == "hhssvninappt001" or hostname == "hhssvninappp001": log_file = "C:\\inetpub\\wwwroot\\oscarhur\\" + api_const.ambulatory_log_file else: log_file = dir_path + api_const.ambulatory_log_file providerids_to_licnums_hash = {} providerids_to_licexps_hash = {} logging.basicConfig( filename = log_file, \ filemode = 'a', \ level = logging.info, \ format = "%(levelname)s %(name)s %(asctime)s %(lineno)s - %(message)s ") logger = logging.getLogger(__name__) logger.info(wkday + ". " + instance + ". Starting GET CREDENTIAL API") counter = 0 with open(output_file, "w", newline='') as output_fh: output_writer = csv.writer(output_fh, delimiter='|') output_writer.writerow(["credential_id", "provider_id", "lic_num", "issued_date", \ "expiration_date"]) for providerid in providerid_list: api_url = api_const.api_url + instance + "/providers/" + providerid + "/credential" logger.debug(wkday + ". " + api_url) counter = counter + 1 # API authentication expires in about 15 minutes # So after each 1700 iterations, we will get authentication again if (counter % 1700 == 0) or counter == 1: auth_token = get_auth_token(instance) headers = api_get_headers.get_headers(auth_token) try: response = requests.get(api_url, headers=headers, timeout=60) response.raise_for_status() except requests.exceptions.RequestException as err: logger.critical(wkday + ". " + instance + ". OOps: Something Else: " + str(err)) logger.critical(wkday + ". " + instance + ". " + api_url) continue except requests.exceptions.HTTPError as errh: logger.critical(wkday + ". " + instance + ". Http Error: " + errh) logger.critical(wkday + ". " + instance + ". " + api_url) continue except requests.exceptions.ConnectionError as errc: logger.critical(wkday + ". " + instance + ". Error Connecting: " + errc) logger.critical(wkday + ". " + instance + ". " + api_url) continue except requests.exceptions.Timeout as errt: logger.critical(wkday + ". " + instance + ". Timeout Error: " + errt) logger.critical(wkday + ". " + instance + ". " + api_url) continue logger.debug(wkday + ". Get_credential api return code: " + str(response.status_code)) response_json = response.json() try: credential = response_json[0]["CredentialID"] except: logger.critical(wkday + ". " + instance + ". This Provider ID credential not found: " + providerid) continue licnum = response_json[0]["LicenseNumber"] issued_date = response_json[0]["IssuedText"] licexp = response_json[0]["ExpiredText"] output_writer.writerow( [credential, providerid, licnum, issued_date, licexp]) providerids_to_licnums_hash[providerid] = licnum providerids_to_licexps_hash[providerid] = licexp end_time_obj = datetime.datetime.now() diff_time = (end_time_obj - begin_time_obj).total_seconds() diff_time = int(diff_time) logger.info(wkday + ". " + instance + ". Completing all staff licenses data") logger.info(wkday + ". " + instance + ". Creating the table: " + output_file) logger.info(wkday + ". " + instance + ". Run ends. It took " + str(diff_time) + " second(s) to complete") return (providerids_to_licnums_hash, providerids_to_licexps_hash)
def mdstaff_reports(instance): hostname = socket.gethostname().lower().strip() username = getpass.getuser() begin_time_obj = datetime.datetime.now() today_date = get_today_date_yyyymmdd() dir_path = os.path.dirname(os.path.realpath(__file__)) if instance == "inpatient": instance = api_const.inpatient_instance if hostname == "hhssvninappt001" or hostname == "hhssvninappp001": log_file = "C:\\inetpub\\wwwroot\\oscarhur\\" + api_const.inpatient_log_file else: log_file = dir_path + api_const.inpatient_log_file elif instance == "ambulatory": instance = api_const.ambulatory_instance if hostname == "hhssvninappt001" or hostname == "hhssvninappp001": log_file = "C:\\inetpub\\wwwroot\\oscarhur\\" + api_const.ambulatory_log_file else: log_file = dir_path + api_const.ambulatory_log_file logging.basicConfig( filename = log_file, \ filemode = 'a', \ level = logging.INFO, \ format = "%(levelname)s %(name)s %(asctime)s %(lineno)s - %(message)s ") logger = logging.getLogger(__name__) wkday = get_today_wkday() logger.info("=================") logger.info(wkday + ". " + instance + ". Starting MDStaff API. By " + username + " from " + hostname) auth_token = get_auth_token(instance) get_headers = api_get_headers.get_headers(auth_token) # to get a hash of providerid vs sccid # with 'get providers' and 'get demographic' providerid_list = get_providers(get_headers, instance) providerids_to_sccuids_hash = {} providerids_to_names_hash = {} providerids_to_apptids_hash = {} providerids_to_sccuids_hash, providerids_to_names_hash = \ get_demographic(instance, providerid_list) # get the lic data # get hashes of lic data (num and exp date) with SCCUID providerids_to_licnums_hash, providerids_to_licexps_hash = \ get_credential(instance, providerid_list) # get the appointment data # for department and division data of the staff providerids_to_apptids_hash, providerids_to_depts_hash, providerids_to_divs_hash = \ get_appointments(instance, providerid_list) # get department and division names from lookup table dept_hash = {} div_hash = {} auth_token = get_auth_token(instance) get_headers = api_get_headers.get_headers(auth_token) dept_hash = get_lookups(get_headers, instance, "department") if instance == api_const.inpatient_instance: # get the division data of VMC, OCH, SLRH div_hash = get_lookups(get_headers, instance, "division") # writing the reports through API # output_file = dir_path + "\\data\\mdstaff_report_" + instance + "_" + today_date + ".txt" logger.info(wkday + ". " + instance + ". Writing report " + output_file) with open (output_file, "w", newline = "") as output_file_fh: csv_writer =csv.writer(output_file_fh, delimiter = "|") csv_writer.writerow(["sccuid", "fullname", "licnum", "licexp", "division", "dept"]) for providerid, sccuid in providerids_to_sccuids_hash.items(): fullname = providerids_to_names_hash[providerid] div = "" if fullname is None: fullname = "" if sccuid is None: sccuid = "" try: licnum = providerids_to_licnums_hash[providerid] except: logger.critical(wkday + ". " + instance + ". Lic Num not found for " + \ fullname + " " + sccuid) try: licexp = providerids_to_licexps_hash[providerid] except: logger.critical(wkday + ". " + instance + ". Lic Expiration not found for " + \ fullname + " " + sccuid) if instance == api_const.inpatient_instance: try: div_code = providerids_to_divs_hash[providerid] div = div_hash[div_code] except: logger.critical(wkday + ". " + instance + ". Division not found for " + \ fullname + " " + sccuid) try: dept_code = providerids_to_depts_hash[providerid] dept = dept_hash[dept_code] except: dept = "" logger.critical(wkday + ". " + instance + ". Dept data not found for " + \ fullname) csv_writer.writerow([sccuid, fullname, licnum, licexp, div, dept]) if hostname == "hhssvninappt001" or hostname == "hhssvninappp001": # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! subprocess.call(r'net use h: /del') subprocess.call(r'net use h: \\hhsisiAOB\hipaa$') shared_drive = "H:\\FTP-root\\Virtual_Folders\\NURSING\\mdstaff\\data\\" shared_file = shared_drive + "mdstaff_report_" + instance + "_" + today_date + ".txt" copyfile(output_file, shared_file) logger.info(wkday + ". " + instance + ". Report copied to " + shared_drive + "\\" + output_file) end_time_obj = datetime.datetime.now() diff_time = (end_time_obj - begin_time_obj).total_seconds() diff_time_sec = int(diff_time) diff_time_min = diff_time_sec / 60 diff_time_min = str(round(diff_time_min, 2)) logger.info(wkday + ". " + instance + ". MDStaff License Report created " + output_file) logger.info(wkday + ". " + instance + ". Run ends. It took " + str(diff_time_sec) + \ " seconds or " + diff_time_min + " minutes to complete") logger.info("=================")
def get_demographic(instance, providerid_list): # provider_list is passed as a list of providerID from MDStaff providerids_to_names_hash = {} providerids_to_sccuids_hash = {} today_date = get_today_date_yyyymmdd() wkday = get_today_wkday() begin_time_obj = datetime.datetime.now() hostname = socket.gethostname().lower().strip() dir_path = os.path.dirname(os.path.realpath(__file__)) log_file = "" output_file = dir_path + "/data/demographic_providerids_" + instance + ".txt" if instance == "inpatient": instance = api_const.inpatient_instance if hostname == "hhssvninappt001" or hostname == "hhssvninappp001": log_file = "C:\\inetpub\\wwwroot\\oscarhur\\" + api_const.inpatient_log_file else: log_file = dir_path + api_const.inpatient_log_file elif instance == "ambulatory": instance = api_const.ambulatory_instance if hostname == "hhssvninappt001" or hostname == "hhssvninappp001": log_file = "C:\\inetpub\\wwwroot\\oscarhur\\" + api_const.ambulatory_log_file else: log_file = dir_path + api_const.ambulatory_log_file logging.basicConfig( filename = log_file, \ filemode = 'a', \ level = logging.info, \ format = "%(levelname)s %(name)s %(asctime)s %(lineno)s - %(message)s ") logger = logging.getLogger(__name__) logger.info(wkday + ". " + instance + ". Starting Get Demographic API.") with open(output_file, "w", newline='') as output_fh: output_writer = csv.writer(output_fh, delimiter='|') output_writer.writerow(["provider_id", "vmcid", "name", "email_addr"]) logger.info(wkday + ". " + instance + ". Getting each staff demographic data") counter = 0 for providerid in providerid_list: api_url = api_const.api_url + "/" + instance + "/demographic/" + providerid counter = counter + 1 logger.debug(wkday + ". " + instance + ". API URL: " + api_url) # API authentication expires in about 15 minutes # So after each 1700 iterations, we will get authentication again if (counter % 1700 == 0) or counter == 1: auth_token = get_auth_token(instance) headers = api_get_headers.get_headers(auth_token) try: response = requests.get(api_url, headers=headers, timeout=60) response.raise_for_status() except requests.exceptions.RequestException as err: logger.critical(wkday + ". " + instance + ". OOps: Something Else: " + str(err)) logger.critical(wkday + ". " + instance + ". " + api_url) continue except requests.exceptions.HTTPError as errh: logger.critical(wkday + ". " + instance + ". Http Error: " + errh) logger.critical(wkday + ". " + instance + ". " + api_url) continue except requests.exceptions.ConnectionError as errc: logger.critical(wkday + ". " + instance + ". Error Connecting: " + errc) logger.critical(wkday + ". " + instance + ". " + api_url) continue except requests.exceptions.Timeout as errt: logger.critical(wkday + ". " + instance + ". Timeout Error: " + errt) logger.critical(wkday + ". " + instance + ". " + api_url) continue response_json = response.json() name = response_json["FormattedName"] sccuid = response_json["OtherID"] email_addr = response_json["Email"] if sccuid == None: logger.critical(wkday + ". " + instance + ". " + name + " SCCUID is not found in MDStaff") else: sccuid = sccuid.strip() providerids_to_names_hash[providerid] = name providerids_to_sccuids_hash[providerid] = sccuid logger.debug(wkday + ". " + instance + ". Get demographics. " + ". " \ + providerids_to_names_hash[providerid]) output_writer.writerow([providerid, str(sccuid), name, email_addr]) logger.info(wkday + ". " + instance + ". Creating the table: " + output_file) end_time_obj = datetime.datetime.now() diff_time = (end_time_obj - begin_time_obj).total_seconds() diff_time = int(diff_time) logger.info(wkday + ". " + instance + ". Completing all staff demographic data") logger.info(wkday + ". " + instance + ". Run ends. It took " + str(diff_time) + " second(s) to complete") return providerids_to_sccuids_hash, providerids_to_names_hash
def put_appointment(put_headers, \ get_headers, \ instance, \ providerids_to_apptids_hash, \ providerids_to_sccids_hash, \ providerids_to_names_hash): # credential_data should be passed as a hash of providerID and credentialID, # with credentialID as the key # lic_data should be passed as a hash of data to be modified and credentialID # with credentialID as the key wkday = get_today_wkday() dir_path = os.path.dirname(os.path.realpath(__file__)) log_file = dir_path + api_const.log_file logging.basicConfig( filename = log_file, \ filemode = 'a', \ level = logging.INFO, \ format = "%(levelname)s %(name)s %(asctime)s %(lineno)s - %(message)s ") logger = logging.getLogger(__name__) logger.info(wkday + ". Starting PUT APPOINTMENT API") # for mdstaff nursing PUT appointment is to add dept and division data only sccids_to_depts_hash = {} dept = "" with open("SantaClara_66403_20200709-1002.txt", "r") as input_fh: csv_reader = csv.reader(input_fh, delimiter="|") for row in csv_reader: sccid = row[4] dept = str(row[5]) sccids_to_depts_hash[sccid] = dept deptcodes_to_deptids_hash = get_lookups(get_headers, instance, "department") divnames_to_divids_hash = get_lookups(get_headers, instance, "division") print(deptcodes_to_deptids_hash) for providerid, apptid in providerids_to_apptids_hash.items(): print(providerid, apptid) sccid = providerids_to_sccids_hash[providerid] staff_name = providerids_to_names_hash[providerid] try: dept = sccids_to_depts_hash[sccid] deptid = deptcodes_to_deptids_hash[dept] except: logger.critical(wkday + ". " + staff_name + " not found in PeopleSoft.") continue if int(dept) < 8400: divname = "VMC" elif int(dept) > 8400 and int(dept) < 8600: divname = "OCH" else: divname = "SLRH" logger.info(wkday + ". " + staff_name + ". " + divname + " & " + dept) division = divnames_to_divids_hash[divname] api_url = api_const.api_url + instance + "/providers/" + providerid \ + "/appointment/" + apptid logger.info(wkday + ". " + staff_name + ". " + api_url) data = '{"DepartmentID_1": "' + deptid + '","DivisionID":"' + division + '"}' try: response = requests.put(api_url, \ headers = put_headers, \ data = data, \ timeout = (250, 500)) response.raise_for_status() except requests.exceptions.RequestException as err: logging.critical(wkday + ". OOps: Something Else: " + str(err)) logging.critical(wkday + ". " + api_url) logging.critical(wkday + ". " + data) continue except requests.exceptions.HTTPError as errh: logging.critical(wkday + ". Http Error: " + str(errh)) logging.critical(wkday + ". " + api_url) logging.critical(wkday + ". " + data) continue except requests.exceptions.ConnectionError as errc: logging.critical(wkday + ". Error Connecting: " + str(errc)) logging.critical(wkday + ". " + api_url) logging.critical(wkday + ". " + data) continue except requests.exceptions.Timeout as errt: logging.critical(wkday + ". Timeout Error: " + str(errt)) logging.critical(wkday + ". " + api_url) logging.critical(wkday + ". " + data) contiue response_json = response.json() logger.info(wkday + ". " + staff_name + ". " + "Put Appointment return code: " + \ str(response.status_code))
def put_demographic(headers, instance): # mdstaff_id should be passed as a hash wkday = get_today_wkday() dir_path = os.path.dirname(os.path.realpath(__file__)) log_file = dir_path + api_const.log_file logging.basicConfig( filename = log_file, \ filemode = 'a', \ level = logging.info, \ format = "%(levelname)s %(name)s %(asctime)s %(lineno)s - %(message)s ") logger = logging.getLogger(__name__) logger.info(wkday + ". Starting PUT DEMOGRAPHIC API") with open("output_final2.txt", "r") as input_fh: csv_reader = csv.reader(input_fh, delimiter="|") for row in csv_reader: providerid = row[5] firstname = row[3].strip().title() middlename = row[4].strip().title() lastname = row[2].strip().title() api_url = api_const.api_url + instance + "/demographic/" + providerid logger.info(wkday + ". API URL: " + api_url) data = '{"FirstName":"' + firstname + '", "MiddleName":"' + middlename + '","LastName":"' + lastname + '"}' print(data) try: response = requests.put(api_url, \ headers = headers, \ data = data, \ timeout = (250, 500)) response.raise_for_status() except requests.exceptions.RequestException as err: logger.critical(wkday + ". OOps: Something Else: " + str(err)) logger.critical(wkday + ". " + api_url) logger.critical(wkday + ". " + data) continue except requests.exceptions.HTTPError as errh: logger.critical(wkday + ". Http Error: " + str(errh)) logger.critical(wkday + ". " + api_url) logger.critical(wkday + ". " + data) continue except requests.exceptions.ConnectionError as errc: logger.critical(wkday + ". Error Connecting: " + str(errc)) logger.critical(wkday + ". " + api_url) logger.critical(wkday + ". " + data) continue except requests.exceptions.Timeout as errt: logger.critical(wkday + ". Timeout Error: " + str(errt)) logger.critical(wkday + ". " + api_url) logger.critical(wkday + ". " + data) continue response_json = response.json() logger.info("Return code: " + str(response.status_code))
def get_appointments(instance, providerid_list): # mdstaff_id should be passed as a list today_date = get_today_date_yyyymmdd() wkday = get_today_wkday() dir_path = os.path.dirname(os.path.realpath(__file__)) providerids_to_apptids_hash = {} providerids_to_depts_hash = {} providerids_to_divs_hash = {} log_file = "" hostname = socket.gethostname().lower().strip() output_file = "appointment_" + instance + ".txt" output_file = dir_path + "/data/" + output_file if instance == "inpatient": instance = api_const.inpatient_instance if hostname == "hhssvninappt001" or hostname == "hhssvninappp001": log_file = "C:\\inetpub\\wwwroot\\oscarhur\\" + api_const.inpatient_log_file else: log_file = dir_path + api_const.inpatient_log_file elif instance == "ambulatory": instance = api_const.ambulatory_instance if hostname == "hhssvninappt001" or hostname == "hhssvninappp001": log_file = "C:\\inetpub\\wwwroot\\oscarhur\\" + api_const.ambulatory_log_file else: log_file = dir_path + api_const.ambulatory_log_file logging.basicConfig( filename = log_file, \ filemode = 'a', \ level = logging.INFO, \ format = "%(levelname)s %(name)s %(asctime)s %(lineno)s - %(message)s ") logger = logging.getLogger(__name__) logger.info(wkday + ". " + instance + ". Starting Get Appointment API.") with open(output_file, "w", newline='') as output_fh: output_writer = csv.writer(output_fh, delimiter='|') output_writer.writerow(["providerid", "apptid", "dept", "div"]) counter = 0 for providerid in providerid_list: api_url = api_const.api_url + instance + "/providers/" + providerid + "/appointment" logger.debug(wkday + ". " + api_url) counter = counter + 1 # API authentication expires in about 15 minutes # So after each 1700 iterations, we will get authentication again if (counter % 1700 == 0) or counter == 1: auth_token = get_auth_token(instance) headers = api_get_headers.get_headers(auth_token) try: response = requests.get(api_url, headers=headers, timeout=60) response.raise_for_status() except requests.exceptions.RequestException as err: logging.critical(wkday + ". " + instance + ". OOps: Something Else: " + str(err)) logging.critical(wkday + ". " + instance + ". " + api_url) continue except requests.exceptions.HTTPError as errh: logging.critical(wkday + ". " + instance + ". Http Error: " + errh) logging.critical(wkday + ". " + instance + ". " + api_url) continue except requests.exceptions.ConnectionError as errc: logging.critical(wkday + ". " + instance + ". Error Connecting: " + errc) logging.critical(wkday + ". " + instance + ". " + api_url) continue except requests.exceptions.Timeout as errt: logging.critical(wkday + ". " + instance + ". Timeout Error: " + errt) logging.critical(wkday + ". " + instance + ". " + api_url) continue response_json = response.json() for i in response_json: apptid = i["AppointmentID"] providerid = i["ProviderID"] dept = i["DepartmentID_1"] division = i["DivisionID"] providerids_to_apptids_hash[providerid] = apptid providerids_to_depts_hash[providerid] = dept providerids_to_divs_hash[providerid] = division output_writer.writerow([providerid, apptid, dept, division]) logger.info(wkday + ". " + instance + ". Completing all staff appointment data") logger.info(wkday + ". " + instance + ". Creating the table: " + output_file) return providerids_to_apptids_hash, providerids_to_depts_hash, providerids_to_divs_hash