Add dedicated weather alerts endpoint
Browse files- app/main.py +26 -1
- app/utils/weather_api.py +13 -0
app/main.py
CHANGED
|
@@ -16,7 +16,7 @@ from app.tasks.rag_updater import schedule_updates
|
|
| 16 |
from app.utils import config
|
| 17 |
from app.agents.crew_pipeline import run_pipeline
|
| 18 |
from app.agents.climate_agent import advise_climate_resilient
|
| 19 |
-
from app.utils.weather_api import fetch_forecast
|
| 20 |
|
| 21 |
logging.basicConfig(
|
| 22 |
format="%(asctime)s [%(levelname)s] %(message)s",
|
|
@@ -85,6 +85,31 @@ def weather_test(
|
|
| 85 |
except Exception as e:
|
| 86 |
raise HTTPException(status_code=502, detail=f"WeatherAPI request failed: {e}")
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
@app.post("/ask")
|
| 89 |
def ask_farmbot(
|
| 90 |
query: str = Body(..., embed=True),
|
|
|
|
| 16 |
from app.utils import config
|
| 17 |
from app.agents.crew_pipeline import run_pipeline
|
| 18 |
from app.agents.climate_agent import advise_climate_resilient
|
| 19 |
+
from app.utils.weather_api import fetch_forecast, alerts_for_q
|
| 20 |
|
| 21 |
logging.basicConfig(
|
| 22 |
format="%(asctime)s [%(levelname)s] %(message)s",
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
raise HTTPException(status_code=502, detail=f"WeatherAPI request failed: {e}")
|
| 87 |
|
| 88 |
+
|
| 89 |
+
@app.get("/weather-alerts")
|
| 90 |
+
def weather_alerts(
|
| 91 |
+
state: Optional[str] = None,
|
| 92 |
+
lat: Optional[float] = None,
|
| 93 |
+
lon: Optional[float] = None,
|
| 94 |
+
):
|
| 95 |
+
if not config.WEATHER_API_KEY:
|
| 96 |
+
raise HTTPException(status_code=500, detail="WEATHER_API_KEY is not configured")
|
| 97 |
+
|
| 98 |
+
if lat is not None and lon is not None:
|
| 99 |
+
q = f"{lat},{lon}"
|
| 100 |
+
elif state:
|
| 101 |
+
q = f"{state}, Nigeria"
|
| 102 |
+
else:
|
| 103 |
+
raise HTTPException(
|
| 104 |
+
status_code=400,
|
| 105 |
+
detail="Provide either state or both lat and lon",
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
try:
|
| 109 |
+
return alerts_for_q(q)
|
| 110 |
+
except Exception as e:
|
| 111 |
+
raise HTTPException(status_code=502, detail=f"WeatherAPI request failed: {e}")
|
| 112 |
+
|
| 113 |
@app.post("/ask")
|
| 114 |
def ask_farmbot(
|
| 115 |
query: str = Body(..., embed=True),
|
app/utils/weather_api.py
CHANGED
|
@@ -108,6 +108,19 @@ def forecast_summary_for_state(state: str) -> str:
|
|
| 108 |
return summarize_forecast(data, include_alerts=True)
|
| 109 |
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
def forecast_summary_for_gps(lat: float, lon: float) -> str:
|
| 112 |
q = f"{lat},{lon}"
|
| 113 |
data = fetch_forecast(
|
|
|
|
| 108 |
return summarize_forecast(data, include_alerts=True)
|
| 109 |
|
| 110 |
|
| 111 |
+
def alerts_for_q(q: str) -> Dict[str, Any]:
|
| 112 |
+
data = fetch_forecast(
|
| 113 |
+
q=q,
|
| 114 |
+
days=getattr(config, "WEATHER_FORECAST_DAYS", 3),
|
| 115 |
+
alerts=getattr(config, "WEATHER_ALERTS", "yes"),
|
| 116 |
+
aqi=getattr(config, "WEATHER_AQI", "yes"),
|
| 117 |
+
)
|
| 118 |
+
return {
|
| 119 |
+
"location": data.get("location"),
|
| 120 |
+
"alerts": data.get("alerts") or {},
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
|
| 124 |
def forecast_summary_for_gps(lat: float, lon: float) -> str:
|
| 125 |
q = f"{lat},{lon}"
|
| 126 |
data = fetch_forecast(
|