Only cache 200 responses
Some checks failed
Amarillo/amarillo-gitea/amarillo-gtfs-export/pipeline/head There was a failure building this commit

This commit is contained in:
Csaba 2024-05-23 13:55:09 +02:00
parent eba7967c90
commit a7311b9152
2 changed files with 9 additions and 7 deletions

View file

@ -62,8 +62,9 @@ async def get_file(region_id: str, requesting_user: User = Depends(get_current_u
# logger.info("Returning new response") # logger.info("Returning new response")
response = requests.get(f"{config.generator_url}/region/{region_id}/gtfs/") response = requests.get(f"{config.generator_url}/region/{region_id}/gtfs/")
# cache response # cache response
with open(file_path, "wb") as file: if response.status_code == 200:
file.write(response.content) with open(file_path, "wb") as file:
file.write(response.content)
return Response(content=response.content, media_type="application/zip") return Response(content=response.content, media_type="application/zip")
@ -88,13 +89,14 @@ async def get_file(region_id: str, format: str = 'protobuf', requesting_user: Us
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=message) raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=message)
if is_cached_1m(file_path): if is_cached_1m(file_path):
logger.info("Returning cached response") # logger.info("Returning cached response")
return FileResponse(file_path) return FileResponse(file_path)
logger.info("Returning new response") # logger.info("Returning new response")
response = requests.get(f"{config.generator_url}/region/{region_id}/gtfs-rt/?format={format}") response = requests.get(f"{config.generator_url}/region/{region_id}/gtfs-rt/?format={format}")
# cache response # cache response
with open(file_path, "wb") as file: if response.status_code == 200:
file.write(response.content) with open(file_path, "wb") as file:
file.write(response.content)
return Response(content=response.content) return Response(content=response.content)

View file

@ -1,6 +1,6 @@
[project] [project]
name = "amarillo-gtfs-exporter" name = "amarillo-gtfs-exporter"
version = "0.0.2" version = "0.0.4"
dependencies = [ dependencies = [
"amarillo" "amarillo"
] ]