Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query should handle 401 unauthorized exceptions from Katsu gracefully #56

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions query_server/query_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,22 @@ def fix_dicts(to_fix):
else:
return to_fix

# Helper function to format the response from a query into a format the data portal understands
def format_query_response(donors, genomic_query, summary_stats, page, page_size):
# Determine which part of the filtered donors to send back
full_data = {
'results': donors[(page*page_size):((page+1)*page_size)],
'genomic': genomic_query,
'count': len(donors),
'summary': summary_stats
}
# full_data['genomic_query_info'] = genomic_query_info

# Add prev and next parameters to the repsonse, appending a session ID.
# Essentially we want to go session ID -> list of donors
# and then paginate the list of donors, calling donors_with_clinical_data on each before returning
return fix_dicts(full_data), 200

@app.route('/query')
def query(treatment="", primary_site="", drug_name="", systemic_therapy_type="", chrom="", gene="", page=0, page_size=10, assembly="hg38", exclude_cohorts=[], session_id=""):
# Add a service token to the headers so that other services will know this is from the query service:
Expand Down Expand Up @@ -233,7 +249,17 @@ def query(treatment="", primary_site="", drug_name="", systemic_therapy_type="",
params[param[1]] = param[0]

full_url = f"{url}?{urllib.parse.urlencode(params, doseq=True)}"
donors = safe_get_request_json(requests.get(full_url, headers=headers), 'Katsu explorer donors')['items']
donors_req = requests.get(full_url, headers=headers)
if not donors_req.ok:
if donors_req.status_code == 401:
# 401 Unauthorized: the user is not allowed to view any donors at this site
# The rest of the code should just pass quietly
return format_query_response([], [], get_summary_stats([], {}, {}), page, page_size)
else:
err_msg = f"Could not got Katsu donors response: {donors_req.status_code} {donors_req.text}"
logger.error(err_msg)
raise Exception(err_msg)
donors = donors_req.json()['items']

# Filter on excluded cohorts
donors = [donor for donor in donors if donor['program_id'] not in exclude_cohorts]
Expand Down Expand Up @@ -317,19 +343,7 @@ def query(treatment="", primary_site="", drug_name="", systemic_therapy_type="",
# TODO: Cache the above list of donor IDs and summary statistics
summary_stats = get_summary_stats(donors, summary_info['primary_site'], summary_info['treatment_type'])

# Determine which part of the filtered donors to send back
full_data = {
'results': [donor for donor in donors[(page*page_size):((page+1)*page_size)]],
'genomic': genomic_query,
'count': len(donors),
'summary': summary_stats
}
# full_data['genomic_query_info'] = genomic_query_info

# Add prev and next parameters to the repsonse, appending a session ID.
# Essentially we want to go session ID -> list of donors
# and then paginate the list of donors, calling donors_with_clinical_data on each before returning
return fix_dicts(full_data), 200
return format_query_response(donors, genomic_query, summary_stats, page, page_size)

@app.route('/genomic_completeness')
def genomic_completeness():
Expand Down