def get_all_records(): # Get URL for API request url = app.config['JHU_REQUEST_URL'] # Make request and retrieve records in json format r = requests.get(url) data = r.json() logging.info(data) status_code = r.status_code logging.info(status_code) # If status code is not 200, send error emails and read records in cached json layer if status_code != 200: # Log error and send error email body = "Results were not successfully retrieved from JHU API. " \ "Request returned status code {}.".format(status_code) logger.info(body) logger.info(f"API Response Info: {data}") request_info = {"url": url, "headers": None} send_api_error_email(body, data, request_info=request_info) # Read and return data from cached json data = read_from_json(app.config['JHU_CACHED_RESULTS_PATH']) return data, status_code
def get(self): try: # Check how long it has been since cached_results.json was last updated current_time = datetime.now() file_created_datetime =\ datetime.fromtimestamp(os.path.getmtime(app.config['JHU_CACHED_RESULTS_PATH'])) hour_diff = ( (current_time - file_created_datetime).total_seconds()) / 3600 # If longer than value of time diff param, generate a new cache generate_new_cache = True if hour_diff > app.config[ 'JHU_TIME_DIFF'] else False except FileNotFoundError: # If cached_results.json has never been created, generate a new cache generate_new_cache = True file_created_datetime = datetime.now() if generate_new_cache: # Get all records from using JHU API records, status_code = get_all_records() if status_code == 200: # Write new records to json only if the JHU API request was successful write_to_json(records, app.config['JHU_CACHED_RESULTS_PATH']) else: # Read records from json cache layer records = read_from_json(app.config['JHU_CACHED_RESULTS_PATH']) status_code = 200 result = { "jhu_request_status_code": status_code, "records": records, "updated_at": file_created_datetime } return jsonify(result)
def get(self): # Parse request parameters visualize_on_serotracker_filter = request.args.get( 'visualize_on_serotracker_filter', 1, type=int) airtable_fields_json_name = request.args.get( 'airtable_fields_json_name', 'dashboard') # Only check cache if records are being pulled for dashboard if airtable_fields_json_name == 'dashboard': try: # Check how long it has been since cached_results.json was last updated current_time = datetime.now() file_created_datetime =\ datetime.fromtimestamp(os.path.getmtime(app.config['AIRTABLE_CACHED_RESULTS_PATH'])) hour_diff = ((current_time - file_created_datetime).total_seconds()) / 3600 # If longer than value of time diff param, generate a new cache generate_new_cache = True if hour_diff > app.config[ 'AIRTABLE_TIME_DIFF'] else False except FileNotFoundError: # If cached_results.json has never been created, generate a new cache generate_new_cache = True file_created_datetime = datetime.now() if generate_new_cache: # Get all records from using airtable API records, status_code = get_all_records( visualize_on_serotracker_filter, airtable_fields_json_name) if status_code == 200: # Write new records to json only if the airtable API request was successful write_to_json(records, app.config['AIRTABLE_CACHED_RESULTS_PATH']) else: # Read records from json cache layer records = read_from_json( app.config['AIRTABLE_CACHED_RESULTS_PATH']) status_code = 200 # If records aren't being pulled for dashboard, make new request and don't write to cache else: # Get all records from using airtable API records, status_code = get_all_records( visualize_on_serotracker_filter, airtable_fields_json_name) file_created_datetime = datetime.now() result = { "airtable_request_status_code": status_code, "records": records, "updated_at": file_created_datetime } return jsonify(result)
def get_all_records(visualize_sero_filter, airtable_fields_json): # Get airtable API URL and add fields to be scraped to URL in HTML format url = app.config['AIRTABLE_REQUEST_URL'] url = _add_fields_to_url(url, airtable_fields_json) headers = { 'Authorization': 'Bearer {}'.format(app.config['AIRTABLE_API_KEY']) } params = app.config[ 'AIRTABLE_REQUEST_PARAMS'] if visualize_sero_filter == 1 else None # Make request and retrieve records in json format r = requests.get(url, headers=headers, params=params) data = r.json() # Try to get records from data if the request was successful try: # If offset was included in data, retrieve additional paginated records if 'offset' in list(data.keys()): parameters = params.copy() if params is not None else {} request_info = [url, headers, parameters] records = _get_paginated_records(data, request_info) else: records = data['records'] formatted_records = _get_formatted_json_records( records, airtable_fields_json) return formatted_records, 200 # If request was not successful, there will be no records field in response # Just return what is in cached layer and log an error except KeyError as e: body = "Results were not successfully retrieved from Airtable API." \ "Please check connection parameters in config.py and fields in airtable_fields_config.py." logger.error(body) logger.error(f"Error Info: {e}") logger.error(f"API Response Info: {data}") request_info = {"url": url, "headers": json.dumps(headers)} send_api_error_email(body, data, error=e, request_info=request_info) try: records = read_from_json( app.config['AIRTABLE_CACHED_RESULTS_PATH']) except FileNotFoundError: records = [] return records, 400