From f494f7cccb591877466a824c8f7a57085773869a Mon Sep 17 00:00:00 2001 From: Francia Csaba Date: Thu, 8 Feb 2024 13:07:51 +0100 Subject: [PATCH] setup function and router --- amarillo/plugins/gtfs_export/__init__.py | 1 + .../plugins/gtfs_export/gtfs_generator.py | 10 ++- amarillo/plugins/gtfs_export/router.py | 61 +++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 amarillo/plugins/gtfs_export/router.py diff --git a/amarillo/plugins/gtfs_export/__init__.py b/amarillo/plugins/gtfs_export/__init__.py index e69de29..3aa6b3b 100644 --- a/amarillo/plugins/gtfs_export/__init__.py +++ b/amarillo/plugins/gtfs_export/__init__.py @@ -0,0 +1 @@ +from .gtfs_generator import setup \ No newline at end of file diff --git a/amarillo/plugins/gtfs_export/gtfs_generator.py b/amarillo/plugins/gtfs_export/gtfs_generator.py index 591e42c..456088d 100644 --- a/amarillo/plugins/gtfs_export/gtfs_generator.py +++ b/amarillo/plugins/gtfs_export/gtfs_generator.py @@ -1,14 +1,16 @@ +from fastapi import FastAPI + from amarillo.models.Carpool import Region from amarillo.plugins.gtfs_export.gtfs_export import GtfsExport, GtfsFeedInfo, GtfsAgency from amarillo.plugins.gtfs_export.gtfs import GtfsRtProducer from amarillo.utils.container import container +from amarillo.plugins.gtfs_export.router import router from glob import glob import json import schedule import threading import time import logging -from datetime import date, timedelta logger = logging.getLogger(__name__) @@ -68,4 +70,8 @@ def start_schedule(): # Create all feeds once at startup schedule.run_all() job_thread = threading.Thread(target=run_schedule, daemon=True) - job_thread.start() \ No newline at end of file + job_thread.start() + +def setup(app : FastAPI): + app.include_router(router) + start_schedule() \ No newline at end of file diff --git a/amarillo/plugins/gtfs_export/router.py b/amarillo/plugins/gtfs_export/router.py new file mode 100644 index 0000000..48de9d5 --- /dev/null +++ b/amarillo/plugins/gtfs_export/router.py @@ -0,0 +1,61 @@ +import logging + +from fastapi import APIRouter, HTTPException, status, Depends + +from amarillo.models.Carpool import Region +from amarillo.routers.agencyconf import verify_admin_api_key +from amarillo.services.regions import RegionService +from amarillo.utils.container import container +from fastapi.responses import FileResponse + +logger = logging.getLogger(__name__) + +router = APIRouter( + prefix="/region", + tags=["region"] +) + +#TODO: move to amarillo/utils? +def _assert_region_exists(region_id: str) -> Region: + regions: RegionService = container['regions'] + region = regions.get_region(region_id) + region_exists = region is not None + + if not region_exists: + message = f"Region with id {region_id} does not exist." + logger.error(message) + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=message) + + return region + + +@router.get("/{region_id}/gtfs", + summary="Return GTFS Feed for this region", + response_description="GTFS-Feed (zip-file)", + response_class=FileResponse, + responses={ + status.HTTP_404_NOT_FOUND: {"description": "Region not found"}, + } + ) +async def get_file(region_id: str, user: str = Depends(verify_admin_api_key)): + _assert_region_exists(region_id) + return FileResponse(f'data/gtfs/amarillo.{region_id}.gtfs.zip') + +@router.get("/{region_id}/gtfs-rt", + summary="Return GTFS-RT Feed for this region", + response_description="GTFS-RT-Feed", + response_class=FileResponse, + responses={ + status.HTTP_404_NOT_FOUND: {"description": "Region not found"}, + status.HTTP_400_BAD_REQUEST: {"description": "Bad request, e.g. because format is not supported, i.e. neither protobuf nor json."} + } + ) +async def get_file(region_id: str, format: str = 'protobuf', user: str = Depends(verify_admin_api_key)): + _assert_region_exists(region_id) + if format == 'json': + return FileResponse(f'data/gtfs/amarillo.{region_id}.gtfsrt.json') + elif format == 'protobuf': + return FileResponse(f'data/gtfs/amarillo.{region_id}.gtfsrt.pbf') + else: + message = "Specified format is not supported, i.e. neither protobuf nor json." + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=message) \ No newline at end of file