From 6b05225f4d981dfacd37d7d5a5161975ec53235d Mon Sep 17 00:00:00 2001 From: Colin Blackburn Date: Wed, 6 Mar 2024 13:28:49 +0000 Subject: [PATCH] Get request url based on environment --- app/routes.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index cca008c..3cbfca0 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,6 +1,7 @@ import tempfile import shutil import requests +import os from enum import StrEnum from pathlib import Path @@ -24,6 +25,9 @@ BOREHOLE_INDEX_URL = ("https://ogcapi.bgs.ac.uk/collections/agsboreholeindex/items?f=json" "&properties=bgs_loca_id&filter=INTERSECTS(shape,{polygon})&limit=10") +# Get AGS_API_ENV, defaults to DEVELOP if not set or not recognised. +AGS_API_ENV = os.getenv("AGS_API_ENV", "DEVELOP").upper() + router = APIRouter() log_responses = dict(error_responses) @@ -225,7 +229,7 @@ def prepare_validation_response(request, data): response_data = { 'msg': f'{len(data)} files validated', 'type': 'success', - 'self': str(request.url), + 'self': get_request_url(request), 'data': data, } return ValidationResponse(**response_data, media_type="application/json") @@ -472,7 +476,16 @@ def prepare_count_response(request, count): response_data = { 'msg': 'Borehole count', 'type': 'success', - 'self': str(request.url), + 'self': get_request_url(request), 'count': count } return BoreholeCountResponse(**response_data, media_type="application/json") + + +def get_request_url(request): + """ External calls need https to be returned, so check environment.""" + request_url = str(request.url) + if AGS_API_ENV == 'PRODUCTION' and request_url.startswith('http:'): + request_url = request_url.replace('http:', 'https:') + + return request_url