from typing import Annotated, Optional from fastapi import APIRouter, Header, HTTPException from src.api.config import API_FLIGHTS, API_MESSAGES from src.api.routes.auth import status as checkAuth from src.api.schemas.flight import Flight, FlightCreate, FlightStatusUpdate from src.api.utils.network import request router = APIRouter() @router.get("/{id}", response_model=Flight) async def get_flight_by_id(id: int): (response, status, _) = await request(f"{API_FLIGHTS}/{id}", "GET") if status < 200 or status > 204: raise HTTPException(status_code=status, detail=response) return response @router.post("", response_model=Flight) async def create_flight( flight: FlightCreate, authorization: Annotated[str | None, Header()] = None ): await checkAuth(authorization) (response, status, _) = await request( f"{API_FLIGHTS}", "POST", json=flight.model_dump() ) if status < 200 or status > 204: raise HTTPException(status_code=status, detail=response) return response @router.patch("/{id}", response_model=Flight) async def update_flight( id: int, status_update: FlightStatusUpdate, authorization: Annotated[str | None, Header()] = None, ): await checkAuth(authorization) (response, status, _) = await request( f"{API_FLIGHTS}/{id}", "PATCH", json=status_update.model_dump() ) if status < 200 or status > 204: raise HTTPException(status_code=status, detail=response) # TODO: move to flights-domain msg = response msg["id"] = id (response, status, _) = await request( f"{API_MESSAGES}", "POST", json=msg ) if status < 200 or status > 204: raise HTTPException(status_code=status, detail=response) return response @router.get("", response_model=list[Flight]) async def get_flights(origin: Optional[str] = None, lastUpdated: Optional[str] = None): query = {} if origin: query["origin"] = origin if lastUpdated: query["lastUpdated"] = lastUpdated (response, status, _) = await request(f"{API_FLIGHTS}", "GET", query=query) if status < 200 or status > 204: raise HTTPException(status_code=status, detail=response) return response