From 57d05eab385091dbc783398f5bcbf611b9e6a8b1 Mon Sep 17 00:00:00 2001 From: Sudo-JHare Date: Wed, 13 Aug 2025 07:31:32 +1000 Subject: [PATCH] hotfix --- app.py | 16 +++++------ services.py | 3 +- templates/retrieve_split_data.html | 45 +++++++++++++++++------------- 3 files changed, 35 insertions(+), 29 deletions(-) diff --git a/app.py b/app.py index 5347cb8..6aff9f0 100644 --- a/app.py +++ b/app.py @@ -1907,22 +1907,22 @@ def proxy_hapi(subpath): return jsonify({'resourceType': 'OperationOutcome', 'issue': [{'severity': 'error', 'code': 'exception', 'diagnostics': 'An unexpected error occurred within the FHIR proxy.', 'details': {'text': str(e)}}]}), 500 -@app.route('/api/stream-retrieve', methods=['POST']) +@app.route('/api/stream-retrieve', methods=['GET']) def stream_retrieve_bundles(): """ Handles streaming FHIR bundle retrieval. This endpoint directly calls the service function, bypassing the proxy to avoid conflicts. It receives the target URL, - resources, and other parameters from the front-end via a JSON body. + resources, and other parameters from the front-end via URL query parameters. """ def generate_stream(): with app.app_context(): - # Extract parameters from JSON body - data = request.json - fhir_server_url = data.get('fhir_server_url') - resources = data.get('resources') - validate_references = data.get('validate_references', False) - fetch_reference_bundles = data.get('fetch_reference_bundles', False) + # Extract parameters from query string + fhir_server_url = request.args.get('fhir_server_url') + resources = request.args.getlist('resources') + validate_references = request.args.get('validate_references', 'false').lower() == 'true' + fetch_reference_bundles = request.args.get('fetch_reference_bundles', 'false').lower() == 'true' + # Extract authentication headers auth_token = request.headers.get('Authorization') auth_type = 'bearer' if auth_token and auth_token.lower().startswith('bearer') else 'basic' if auth_token and auth_token.lower().startswith('basic') else 'none' diff --git a/services.py b/services.py index 4361b4e..87818cf 100644 --- a/services.py +++ b/services.py @@ -4584,7 +4584,8 @@ def retrieve_bundles(fhir_server_url, resources, output_zip, validate_references yield json.dumps({"type": "info", "message": "Reference fetching OFF"}) + "\n" # Determine the final base URL for requests. - final_base_url = fhir_server_url.rstrip('/') if fhir_server_url and fhir_server_url != '/fhir' else app.config.get('HAPI_FHIR_URL', 'http://localhost:8080/fhir') + from flask import current_app + final_base_url = fhir_server_url.rstrip('/') if fhir_server_url and fhir_server_url != '/fhir' else current_app.config.get('HAPI_FHIR_URL', 'http://localhost:8080/fhir') headers = {'Accept': 'application/fhir+json, application/fhir+xml;q=0.9, */*;q=0.8'} is_custom_url = fhir_server_url != '/fhir' and fhir_server_url is not None and fhir_server_url.startswith('http') diff --git a/templates/retrieve_split_data.html b/templates/retrieve_split_data.html index c930322..3fe7ff4 100644 --- a/templates/retrieve_split_data.html +++ b/templates/retrieve_split_data.html @@ -27,7 +27,7 @@ {% endif %} -
+ {{ form.hidden_tag() }}
@@ -216,7 +216,6 @@ document.addEventListener('DOMContentLoaded', () => { toggleServerButton.classList.remove('disabled'); toggleServerButton.style.pointerEvents = 'auto'; toggleServerButton.removeAttribute('aria-disabled'); - toggleServerButton.title = "Toggle between Local HAPI and Custom URL"; toggleLabel.textContent = useLocalHapi ? 'Using Local HAPI' : 'Using Custom URL'; fhirServerUrlInput.style.display = useLocalHapi ? 'none' : 'block'; fhirServerUrlInput.placeholder = "e.g., https://hapi.fhir.org/baseR4"; @@ -300,10 +299,9 @@ document.addEventListener('DOMContentLoaded', () => { fetchMetadataButton.textContent = 'Fetching...'; try { - const fetchUrl = '/fhir/metadata'; + const fetchUrl = useLocalHapi ? '/fhir/metadata' : `${customUrl}/metadata`; const headers = { 'Accept': 'application/fhir+json' }; if (!useLocalHapi && customUrl) { - headers['X-Target-FHIR-Server'] = customUrl; if (authTypeSelect && authTypeSelect.value !== 'none') { const authType = authTypeSelect.value; if (authType === 'bearer' && bearerTokenInput && bearerTokenInput.value) { @@ -313,12 +311,12 @@ document.addEventListener('DOMContentLoaded', () => { headers['Authorization'] = `Basic ${credentials}`; } } - console.log(`Fetching metadata via proxy with X-Target-FHIR-Server: ${customUrl}`); + console.log(`Fetching metadata directly from: ${customUrl}`); } else { - console.log("Fetching metadata via proxy for local HAPI server"); + console.log("Fetching metadata from local HAPI server via proxy"); } - console.log(`Proxy Fetch URL: ${fetchUrl}`); - console.log(`Request Headers sent TO PROXY: ${JSON.stringify(headers)}`); + console.log(`Fetch URL: ${fetchUrl}`); + console.log(`Request Headers sent: ${JSON.stringify(headers)}`); const response = await fetch(fetchUrl, { method: 'GET', headers: headers }); @@ -383,11 +381,30 @@ document.addEventListener('DOMContentLoaded', () => { return; } + const currentFhirServerUrl = useLocalHapi ? null : fhirServerUrlInput.value.trim(); + if (!useLocalHapi && !currentFhirServerUrl) { + alert('Custom FHIR Server URL is required.'); + fhirServerUrlInput.classList.add('is-invalid'); + retrieveButton.disabled = false; + if (spinner) spinner.style.display = 'none'; + if (icon) icon.style.display = 'inline-block'; + return; + } + const formData = new FormData(); const csrfTokenInput = retrieveForm.querySelector('input[name="csrf_token"]'); if (csrfTokenInput) formData.append('csrf_token', csrfTokenInput.value); selectedResources.forEach(res => formData.append('resources', res)); + // --- FIX APPLIED HERE --- + if (!useLocalHapi && currentFhirServerUrl) { + formData.append('fhir_server_url', currentFhirServerUrl); + } else { + // Explicitly use the proxy URL if local HAPI is selected + formData.append('fhir_server_url', '/fhir'); + } + // --- END FIX --- + if (validateReferencesCheckbox) { formData.append('validate_references', validateReferencesCheckbox.checked ? 'true' : 'false'); } @@ -401,18 +418,6 @@ document.addEventListener('DOMContentLoaded', () => { formData.append('fetch_reference_bundles', 'false'); } - const currentFhirServerUrl = useLocalHapi ? '/fhir' : fhirServerUrlInput.value.trim(); - if (!useLocalHapi && !currentFhirServerUrl) { - alert('Custom FHIR Server URL is required.'); - fhirServerUrlInput.classList.add('is-invalid'); - retrieveButton.disabled = false; - if (spinner) spinner.style.display = 'none'; - if (icon) icon.style.display = 'inline-block'; - return; - } - formData.append('fhir_server_url', currentFhirServerUrl); - - // Add authentication fields for custom URL if (!useLocalHapi && authTypeSelect) { const authType = authTypeSelect.value; formData.append('auth_type', authType);