File size: 1,410 Bytes
d2a1db5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import os
import httpx
from fastapi import HTTPException
API_KEY = os.getenv("TOMORROW_IO_API_KEY")
BASE_URL = "https://api.tomorrow.io/v4"
async def get_tomorrow_weather(location: str, fields: str = "temperature,weatherCode", timesteps: str = "1h", units: str = "metric"):
if not API_KEY:
raise HTTPException(status_code=500, detail="Tomorrow.io API key not configured")
params = {
"location": location,
"fields": fields.split(','),
"timesteps": timesteps.split(','),
"units": units,
"apikey": API_KEY,
}
async with httpx.AsyncClient() as client:
response = await client.get(f"{BASE_URL}/weather/forecast", params=params)
if response.status_code != 200:
raise HTTPException(status_code=response.status_code, detail="Error fetching data from Tomorrow.io")
return response.json()
async def get_weather_alerts(location: str):
if not API_KEY:
raise HTTPException(status_code=500, detail="Tomorrow.io API key not configured")
params = {
"location": location,
"apikey": API_KEY,
}
async with httpx.AsyncClient() as client:
response = await client.get(f"{BASE_URL}/weather/alerts", params=params)
if response.status_code != 200:
raise HTTPException(status_code=response.status_code, detail="Error fetching data from Tomorrow.io")
return response.json()
|