diff --git a/src/app.py b/src/app.py index 378c0d2..ea5fef7 100644 --- a/src/app.py +++ b/src/app.py @@ -3,7 +3,7 @@ import flask import traceback - +import logging import requests import json import pandas as pd @@ -140,8 +140,21 @@ def serve_layout(): # Get data from API api_address = DATASTORE_URL + 'subjects' + app.logger.info('Requesting data from api {0}'.format(api_address)) api_json = get_api_data(api_address) - print(api_json) + + if 'error' in api_json: + app.logger.info('Error response from datastore: {0}'.format(api_json)) + if 'error_code' in api_json: + error_code = api_json['error_code'] + if error_code in ('MISSING_SESSION_ID', 'INVALID_TAPIS_TOKEN'): + app.logger.warn('Auth error from datastore, asking user to authenticate') + return html.Div([ html.H4('Please login and authenticate on the portal to access the report.')]) + + # ignore cache and request again. + if 'data' not in api_json: + app.logger.info('Requesting data from api {0} to ignore cache.'.format(api_address)) + api_json = get_api_data(api_address, True) try: diff --git a/src/datastore_loading.py b/src/datastore_loading.py index 7680b8f..2ec5d1b 100644 --- a/src/datastore_loading.py +++ b/src/datastore_loading.py @@ -1,33 +1,32 @@ import os import flask import requests -import json -import traceback +import logging # --------------------------------- # MOVE THIS TO REFERENCE FROM ENV # --------------------------------- DATASTORE_URL = os.environ.get("DATASTORE_URL","url not found") DATASTORE_URL = os.path.join(DATASTORE_URL, "api/") +logger = logging.getLogger("imaging_app") # --------------------------------- # Get Data From datastore # --------------------------------- -def get_api_data(api_address): +class PortalAuthException(Exception): + '''Custom Exception for issues with Authentication''' + +def get_api_data(api_address, ignore_cache=False): api_json = {} try: - try: - response = requests.get(api_address, cookies=flask.request.cookies) - except Exception as e: - return('error: {}'.format(e)) - request_status = response.status_code - if request_status == 200: - api_json = response.json() - return api_json - else: - return request_status + params = {} + if ignore_cache: + params = {'ignore_cache':True} + response = requests.get(api_address, params=params, cookies=flask.request.cookies) + response.raise_for_status() + return response.json() except Exception as e: - traceback.print_exc() + logger.warn(e) api_json['json'] = 'error: {}'.format(e) return api_json