From 708325846bf0221267205c7ad88e07505e199859 Mon Sep 17 00:00:00 2001 From: Colin Blackburn Date: Wed, 6 Mar 2024 11:26:36 +0000 Subject: [PATCH 1/3] Up-pin fastapi and starlette --- requirements.in | 4 ++-- requirements.txt | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/requirements.in b/requirements.in index 28d70c2..6d22932 100644 --- a/requirements.in +++ b/requirements.in @@ -10,9 +10,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 2a2f35c..3a052f4 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 @@ -106,12 +106,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 From 2953dedff69edcb8a5b5a39142a99ffcd21f8210 Mon Sep 17 00:00:00 2001 From: Colin Blackburn Date: Wed, 6 Mar 2024 11:27:27 +0000 Subject: [PATCH 2/3] Check for None client --- app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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}") From 09cc2d4b4d1bac54dead12343421a4fb998d8268 Mon Sep 17 00:00:00 2001 From: Colin Blackburn Date: Wed, 6 Mar 2024 13:28:49 +0000 Subject: [PATCH 3/3] 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 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