Skip to content

Commit

Permalink
Fix access to WEATHER_API_URL and API_KEY in fetch_weather_data
Browse files Browse the repository at this point in the history
  • Loading branch information
Parissai committed Aug 9, 2024
1 parent 151149a commit 0707942
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions app/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Load environment variables
load_dotenv()

class Config:
Expand All @@ -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:
"""
Expand All @@ -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()

Expand All @@ -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.
Expand Down

0 comments on commit 0707942

Please sign in to comment.