Automated Action 2681892abd Add Weather Data API with OpenWeatherMap integration
- Set up FastAPI project structure
- Create database models for locations and weather data
- Implement OpenWeatherMap API integration
- Create API endpoints for current weather and history
- Add health endpoint
- Set up database migrations with Alembic
- Update README with documentation

generated with BackendIM... (backend.im)
2025-05-12 13:35:49 +00:00

60 lines
1.9 KiB
Python

from fastapi import APIRouter, Depends, Query, HTTPException
from typing import Optional
from sqlalchemy.orm import Session
from app.core.database import get_db
from app.services.weather import WeatherService, get_weather_service
from app.schemas.weather import WeatherRequest, WeatherResponse, WeatherHistoryResponse
router = APIRouter()
@router.get("/current", response_model=WeatherResponse)
async def get_current_weather(
request: WeatherRequest = Depends(),
weather_service: WeatherService = Depends(get_weather_service)
):
"""
Get current weather for a location.
You can provide either city name or coordinates.
- **city**: City name (optional)
- **country**: Country code (optional, used with city)
- **lat**: Latitude (optional)
- **lon**: Longitude (optional)
"""
if request.city:
location, weather = await weather_service.get_weather_by_city(request.city, request.country)
elif request.lat is not None and request.lon is not None:
location, weather = await weather_service.get_weather_by_coordinates(request.lat, request.lon)
else:
raise HTTPException(
status_code=400,
detail="Either city or lat/lon coordinates must be provided"
)
return {
"location": location,
"current_weather": weather
}
@router.get("/history/{location_id}", response_model=WeatherHistoryResponse)
def get_weather_history(
location_id: int,
limit: int = Query(10, ge=1, le=100),
weather_service: WeatherService = Depends(get_weather_service)
):
"""
Get weather history for a location.
- **location_id**: ID of the location
- **limit**: Number of records to return (default: 10, max: 100)
"""
location, history = weather_service.get_weather_history(location_id, limit)
return {
"location": location,
"history": history
}