Skip to content

Commit

Permalink
Merge pull request #134 from BritishGeologicalSurvey/self-https
Browse files Browse the repository at this point in the history
Update fastapi & starlette; return https in external responses
  • Loading branch information
volcan01010 authored Mar 8, 2024
2 parents f2c4ef3 + 09cc2d4 commit ef28b3b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}")
Expand Down
17 changes: 15 additions & 2 deletions app/routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import tempfile
import shutil
import requests
import os

from enum import StrEnum
from pathlib import Path
Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 5 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ef28b3b

Please sign in to comment.