Skip to content

Commit

Permalink
auth and cache adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
chandra-tacc committed Nov 22, 2023
1 parent 54372ef commit cb9737e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
17 changes: 15 additions & 2 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import flask
import traceback


import logging
import requests
import json
import pandas as pd
Expand Down Expand Up @@ -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:

Expand Down
27 changes: 13 additions & 14 deletions src/datastore_loading.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit cb9737e

Please sign in to comment.