feat: add energy, weather and consumption endpoints with models and error handling

This commit is contained in:
Esa Kataja
2025-08-04 19:09:07 +03:00
parent f1b8097f7a
commit 8fba6cfa1e
7 changed files with 257 additions and 1 deletions
+26
View File
@@ -0,0 +1,26 @@
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