diff --git a/app/main.py b/app/main.py index 57eec28..029dcc3 100644 --- a/app/main.py +++ b/app/main.py @@ -112,7 +112,7 @@ def custom_openapi(): @app.middleware("http") async def log_requests(request: Request, call_next): - if not request.client.host.startswith('10.'): + if request.client and not request.client.host.startswith('10.'): logger = logging.getLogger('request') logger.info(f"called by {request.client.host}") req_id = shortuuid.ShortUUID().random(length=8) @@ -122,7 +122,7 @@ async def log_requests(request: Request, call_next): response = await call_next(request) - if not request.client.host.startswith('10.'): + if request.client and not request.client.host.startswith('10.'): call_time = int((time.time() - start_time) * 1000) logger.info(f"Request: id: {req_id} status: {response.status_code}, time: {call_time} ms") logger.debug(f"Request: id: {req_id} response headers: {response.headers}") diff --git a/app/routes.py b/app/routes.py index 7c813fe..7d34654 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") @@ -471,7 +475,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 diff --git a/requirements.in b/requirements.in index 230af10..9e41242 100644 --- a/requirements.in +++ b/requirements.in @@ -11,9 +11,9 @@ python-ags4==0.5.0 requests shortuuid # These libraries are already in FastAPI container but need updated -fastapi==0.88.0 +fastapi==0.110.0 h11==0.14.0 pydantic==1.10.14 python-multipart==0.0.9 -starlette==0.22.0 +starlette==0.36.3 uvicorn==0.20.0 \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 07569c7..8d5fd86 100644 --- a/requirements.txt +++ b/requirements.txt @@ -35,7 +35,7 @@ defusedxml==0.7.1 # via python-ags4 et-xmlfile==1.1.0 # via openpyxl -fastapi==0.88.0 +fastapi==0.110.0 # via -r requirements.in fiona==1.9.5 # via @@ -109,12 +109,14 @@ six==1.16.0 # python-dateutil sniffio==1.3.1 # via anyio -starlette==0.22.0 +starlette==0.36.3 # via # -r requirements.in # fastapi typing-extensions==4.10.0 - # via pydantic + # via + # fastapi + # pydantic tzdata==2024.1 # via pandas urllib3==2.2.1