Files
bakery-ia/shared/clients/external_client.py

231 lines
7.6 KiB
Python
Raw Normal View History

2025-08-12 18:17:30 +02:00
# shared/clients/external_client.py
"""
External Service Client
Handles all API calls to the external service (weather and traffic data)
"""
import httpx
import structlog
from typing import Dict, Any, Optional, List
from .base_service_client import BaseServiceClient
from shared.config.base import BaseServiceSettings
logger = structlog.get_logger()
class ExternalServiceClient(BaseServiceClient):
"""Client for communicating with the external service"""
def __init__(self, config: BaseServiceSettings, calling_service_name: str = "unknown"):
super().__init__(calling_service_name, config)
self.service_url = config.EXTERNAL_SERVICE_URL
def get_service_base_path(self) -> str:
return "/api/v1"
# ================================================================
# WEATHER DATA
# ================================================================
async def get_weather_historical(
self,
tenant_id: str,
start_date: str,
end_date: str,
latitude: Optional[float] = None,
longitude: Optional[float] = None
) -> Optional[List[Dict[str, Any]]]:
"""
Get historical weather data using NEW v2.0 optimized city-based endpoint
This uses pre-loaded data from the database with Redis caching for <100ms response times
2025-08-12 18:17:30 +02:00
"""
# Prepare query parameters
params = {
2025-08-12 18:17:30 +02:00
"latitude": latitude or 40.4168, # Default Madrid coordinates
"longitude": longitude or -3.7038,
"start_date": start_date, # ISO format datetime
"end_date": end_date # ISO format datetime
2025-08-12 18:17:30 +02:00
}
logger.info(f"Weather request (v2.0 optimized): {params}", tenant_id=tenant_id)
# Use GET request to new optimized endpoint with short timeout (data is cached)
2025-08-12 18:17:30 +02:00
result = await self._make_request(
"GET",
"external/operations/historical-weather-optimized",
2025-08-12 18:17:30 +02:00
tenant_id=tenant_id,
params=params,
timeout=10.0 # Much shorter - data is pre-loaded and cached
2025-08-12 18:17:30 +02:00
)
2025-08-12 18:17:30 +02:00
if result:
logger.info(f"Successfully fetched {len(result)} weather records from v2.0 endpoint")
2025-08-12 18:17:30 +02:00
return result
else:
logger.warning("No weather data returned from v2.0 endpoint")
2025-08-12 18:17:30 +02:00
return []
async def get_current_weather(
self,
tenant_id: str,
latitude: Optional[float] = None,
longitude: Optional[float] = None
) -> Optional[Dict[str, Any]]:
"""
Get current weather for a location (real-time data)
Uses new v2.0 endpoint
"""
params = {
"latitude": latitude or 40.4168,
"longitude": longitude or -3.7038
}
logger.info(f"Current weather request (v2.0): {params}", tenant_id=tenant_id)
result = await self._make_request(
"GET",
"external/operations/weather/current",
tenant_id=tenant_id,
params=params,
timeout=10.0
)
if result:
logger.info("Successfully fetched current weather")
return result
else:
logger.warning("No current weather data available")
return None
2025-08-12 18:17:30 +02:00
async def get_weather_forecast(
self,
tenant_id: str,
days: int = 7,
2025-08-12 18:17:30 +02:00
latitude: Optional[float] = None,
longitude: Optional[float] = None
) -> Optional[List[Dict[str, Any]]]:
"""
Get weather forecast for location (from AEMET)
Uses new v2.0 endpoint
2025-08-12 18:17:30 +02:00
"""
params = {
"latitude": latitude or 40.4168,
2025-08-12 18:17:30 +02:00
"longitude": longitude or -3.7038,
"days": days
}
logger.info(f"Weather forecast request (v2.0): {params}", tenant_id=tenant_id)
2025-08-12 18:17:30 +02:00
result = await self._make_request(
"GET",
"external/operations/weather/forecast",
2025-08-12 18:17:30 +02:00
tenant_id=tenant_id,
params=params,
timeout=10.0
2025-08-12 18:17:30 +02:00
)
2025-08-12 18:17:30 +02:00
if result:
logger.info(f"Successfully fetched weather forecast for {days} days")
return result
else:
logger.warning("No forecast data available")
2025-08-12 18:17:30 +02:00
return []
2025-08-12 18:17:30 +02:00
# ================================================================
# TRAFFIC DATA
# ================================================================
async def get_traffic_data(
self,
tenant_id: str,
start_date: str,
end_date: str,
latitude: Optional[float] = None,
longitude: Optional[float] = None
) -> Optional[List[Dict[str, Any]]]:
"""
Get historical traffic data using NEW v2.0 optimized city-based endpoint
This uses pre-loaded data from the database with Redis caching for <100ms response times
2025-08-12 18:17:30 +02:00
"""
# Prepare query parameters
params = {
2025-08-12 18:17:30 +02:00
"latitude": latitude or 40.4168, # Default Madrid coordinates
"longitude": longitude or -3.7038,
"start_date": start_date, # ISO format datetime
"end_date": end_date # ISO format datetime
2025-08-12 18:17:30 +02:00
}
logger.info(f"Traffic request (v2.0 optimized): {params}", tenant_id=tenant_id)
# Use GET request to new optimized endpoint with short timeout (data is cached)
2025-08-12 18:17:30 +02:00
result = await self._make_request(
"GET",
"external/operations/historical-traffic-optimized",
2025-08-12 18:17:30 +02:00
tenant_id=tenant_id,
params=params,
timeout=10.0 # Much shorter - data is pre-loaded and cached
2025-08-12 18:17:30 +02:00
)
2025-08-12 18:17:30 +02:00
if result:
logger.info(f"Successfully fetched {len(result)} traffic records from v2.0 endpoint")
2025-08-12 18:17:30 +02:00
return result
else:
logger.warning("No traffic data returned from v2.0 endpoint")
return []
2025-08-12 18:17:30 +02:00
async def get_stored_traffic_data_for_training(
self,
tenant_id: str,
start_date: str,
end_date: str,
latitude: Optional[float] = None,
longitude: Optional[float] = None
) -> Optional[List[Dict[str, Any]]]:
"""
Get stored traffic data for model training/re-training
In v2.0, this uses the same optimized endpoint as get_traffic_data
since all data is pre-loaded and cached
2025-08-12 18:17:30 +02:00
"""
logger.info("Training traffic data request - delegating to optimized endpoint", tenant_id=tenant_id)
# Delegate to the same optimized endpoint
return await self.get_traffic_data(
tenant_id=tenant_id,
start_date=start_date,
end_date=end_date,
latitude=latitude,
longitude=longitude
2025-08-12 18:17:30 +02:00
)
async def get_current_traffic(
self,
tenant_id: str,
latitude: Optional[float] = None,
longitude: Optional[float] = None
) -> Optional[Dict[str, Any]]:
"""
Get current traffic conditions for a location (real-time data)
Uses new v2.0 endpoint
"""
params = {
"latitude": latitude or 40.4168,
"longitude": longitude or -3.7038
}
logger.info(f"Current traffic request (v2.0): {params}", tenant_id=tenant_id)
2025-08-12 18:17:30 +02:00
result = await self._make_request(
"GET",
"external/operations/traffic/current",
2025-08-12 18:17:30 +02:00
tenant_id=tenant_id,
params=params,
timeout=10.0
2025-08-12 18:17:30 +02:00
)
2025-08-12 18:17:30 +02:00
if result:
logger.info("Successfully fetched current traffic")
2025-08-12 18:17:30 +02:00
return result
else:
logger.warning("No current traffic data available")
2025-08-12 18:17:30 +02:00
return None