From 0707942a6e58c211b77505befaf83fcc3ae2eec8 Mon Sep 17 00:00:00 2001 From: Parissa Jamali Date: Fri, 9 Aug 2024 15:52:20 +0100 Subject: [PATCH] Fix access to WEATHER_API_URL and API_KEY in fetch_weather_data --- app/weather.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/weather.py b/app/weather.py index c7e8819..206378f 100644 --- a/app/weather.py +++ b/app/weather.py @@ -9,6 +9,7 @@ logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) +# Load environment variables load_dotenv() class Config: @@ -20,6 +21,8 @@ def validate(): if not Config.API_KEY: raise ValueError("Weather API key is missing. Please set WEATHER_API_KEY in the environment variables.") +# Validate configuration +Config.validate() async def fetch_weather_data(city: str, date: str) -> dict: """ @@ -38,7 +41,10 @@ async def fetch_weather_data(city: str, date: str) -> dict: """ async with httpx.AsyncClient() as client: try: - response = await client.get(WEATHER_API_URL, params={"key": API_KEY, "q": city, "dt": date}) + response = await client.get( + Config.WEATHER_API_URL, + params={"key": Config.API_KEY, "q": city, "dt": date} + ) response.raise_for_status() data = response.json() @@ -65,7 +71,7 @@ async def get_weather(db: Session, city: str, date: str) -> schemas.WeatherRespo date (str): The date to get weather data for, in 'YYYY-MM-DD' format. Returns: - schemas.Weather: The weather data object. + schemas.WeatherResponse: The weather data object. Raises: Exception: If there is an error fetching or storing weather data.