from fastapi import APIRouter from httpx import AsyncClient from lib.exceptions import FetchException, ParseException from models.weather import Weather from lib import logger router = APIRouter(prefix="/weather", tags=["Weather"]) @router.get("/") async def weather(): url = "https://www.ilmatieteenlaitos.fi/api/weather/observations?fmisid=101237&observations=true&radar=false&daily=false" async with AsyncClient() as client: response = await client.get(url) if response.status_code != 200: raise FetchException(response.url, response.status_code) data = response.json() if not data: raise FetchException(response.url, response.status_code) try: weather = [Weather(**d) for d in data["observations"] if d["t2m"] is not None] except Exception as e: logger.error(f"Error parsing weather: {str(e)}") raise ParseException() return weather