35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
"""Weather API: manual refresh + cached read (PLAN.md 4c: no auto-refetch)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, HTTPException, Query
|
|
|
|
from app.deps import get_store
|
|
from app.services.weather import fetch_route_weather
|
|
|
|
router = APIRouter(prefix="/api/routes", tags=["weather"])
|
|
|
|
|
|
@router.post("/{route_id}/refresh-weather")
|
|
def refresh_weather(route_id: str, days: int = Query(7)):
|
|
store = get_store()
|
|
route = store.get_route(route_id)
|
|
if route is None:
|
|
raise HTTPException(404, "route not found")
|
|
if days not in (7, 10):
|
|
raise HTTPException(400, "days must be 7 or 10")
|
|
try:
|
|
weather = fetch_route_weather(route, days)
|
|
except Exception as e:
|
|
raise HTTPException(502, f"weather fetch failed: {e}")
|
|
store.save_weather(route_id, weather)
|
|
return store.get_weather(route_id)
|
|
|
|
|
|
@router.get("/{route_id}/weather")
|
|
def get_weather(route_id: str):
|
|
weather = get_store().get_weather(route_id)
|
|
if weather is None:
|
|
raise HTTPException(404, "no cached weather; refresh first")
|
|
return weather
|