
- Created FastAPI application with SQLite database integration - Implemented OpenWeatherMap client with caching - Added endpoints for current weather, forecasts, and history - Included comprehensive error handling and validation - Set up Alembic migrations - Created detailed README with usage examples generated with BackendIM... (backend.im)
78 lines
2.3 KiB
Python
78 lines
2.3 KiB
Python
from pydantic import BaseModel, Field
|
|
from datetime import datetime
|
|
from typing import Optional, List
|
|
|
|
# City schemas
|
|
class CityBase(BaseModel):
|
|
name: str
|
|
country: str
|
|
latitude: float
|
|
longitude: float
|
|
|
|
class CityCreate(CityBase):
|
|
pass
|
|
|
|
class CityUpdate(CityBase):
|
|
name: Optional[str] = None
|
|
country: Optional[str] = None
|
|
latitude: Optional[float] = None
|
|
longitude: Optional[float] = None
|
|
|
|
class CityInDB(CityBase):
|
|
id: int
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
class City(CityInDB):
|
|
pass
|
|
|
|
# Weather record schemas
|
|
class WeatherRecordBase(BaseModel):
|
|
temperature: float = Field(..., description="Temperature in Kelvin")
|
|
feels_like: float = Field(..., description="Feels like temperature in Kelvin")
|
|
humidity: int = Field(..., description="Humidity in percentage")
|
|
pressure: int = Field(..., description="Atmospheric pressure in hPa")
|
|
wind_speed: float = Field(..., description="Wind speed in meter/sec")
|
|
wind_direction: int = Field(..., description="Wind direction in degrees")
|
|
weather_condition: str = Field(..., description="Main weather condition")
|
|
weather_description: str = Field(..., description="Weather condition description")
|
|
clouds: int = Field(..., description="Cloudiness in percentage")
|
|
rain_1h: Optional[float] = Field(None, description="Rain volume for last hour in mm")
|
|
snow_1h: Optional[float] = Field(None, description="Snow volume for last hour in mm")
|
|
timestamp: datetime = Field(..., description="Timestamp of the weather data")
|
|
|
|
class WeatherRecordCreate(WeatherRecordBase):
|
|
city_id: int
|
|
|
|
class WeatherRecordInDB(WeatherRecordBase):
|
|
id: int
|
|
city_id: int
|
|
created_at: datetime
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
class WeatherRecord(WeatherRecordInDB):
|
|
pass
|
|
|
|
# Weather response schemas for API
|
|
class CurrentWeatherResponse(BaseModel):
|
|
city: City
|
|
weather: WeatherRecord
|
|
|
|
class ForecastItem(WeatherRecordBase):
|
|
pass
|
|
|
|
class ForecastResponse(BaseModel):
|
|
city: City
|
|
forecast: List[ForecastItem]
|
|
|
|
# Search schemas
|
|
class WeatherSearchParams(BaseModel):
|
|
city: Optional[str] = None
|
|
country: Optional[str] = None
|
|
lat: Optional[float] = None
|
|
lon: Optional[float] = None |