106 lines
3.8 KiB
Python
106 lines
3.8 KiB
Python
# shared/clients/__init__.py
|
|
"""
|
|
Service Client Factory and Convenient Imports
|
|
Provides easy access to all service clients
|
|
"""
|
|
|
|
from .base_service_client import BaseServiceClient, ServiceAuthenticator
|
|
from .training_client import TrainingServiceClient
|
|
from .data_client import DataServiceClient
|
|
from .forecast_client import ForecastServiceClient
|
|
|
|
# Import config
|
|
from shared.config.base import BaseServiceSettings
|
|
|
|
# Cache clients to avoid recreating them
|
|
_client_cache = {}
|
|
|
|
def get_training_client(config: BaseServiceSettings = None, service_name: str = "unknown") -> TrainingServiceClient:
|
|
"""Get or create a training service client"""
|
|
if config is None:
|
|
from app.core.config import settings as config
|
|
|
|
cache_key = f"training_{service_name}"
|
|
if cache_key not in _client_cache:
|
|
_client_cache[cache_key] = TrainingServiceClient(config, service_name)
|
|
return _client_cache[cache_key]
|
|
|
|
def get_data_client(config: BaseServiceSettings = None, service_name: str = "unknown") -> DataServiceClient:
|
|
"""Get or create a data service client"""
|
|
if config is None:
|
|
from app.core.config import settings as config
|
|
|
|
cache_key = f"data_{service_name}"
|
|
if cache_key not in _client_cache:
|
|
_client_cache[cache_key] = DataServiceClient(config, service_name)
|
|
return _client_cache[cache_key]
|
|
|
|
def get_forecast_client(config: BaseServiceSettings = None, service_name: str = "unknown") -> ForecastServiceClient:
|
|
"""Get or create a forecast service client"""
|
|
if config is None:
|
|
from app.core.config import settings as config
|
|
|
|
cache_key = f"forecast_{service_name}"
|
|
if cache_key not in _client_cache:
|
|
_client_cache[cache_key] = ForecastServiceClient(config, service_name)
|
|
return _client_cache[cache_key]
|
|
|
|
class ServiceClients:
|
|
"""Convenient wrapper for all service clients"""
|
|
|
|
def __init__(self, config: BaseServiceSettings = None, service_name: str = "unknown"):
|
|
self.service_name = service_name
|
|
self.config = config or self._get_default_config()
|
|
|
|
# Initialize clients lazily
|
|
self._training_client = None
|
|
self._data_client = None
|
|
self._forecast_client = None
|
|
|
|
def _get_default_config(self):
|
|
"""Get default config from app settings"""
|
|
try:
|
|
from app.core.config import settings
|
|
return settings
|
|
except ImportError:
|
|
raise ImportError("Could not import app config. Please provide config explicitly.")
|
|
|
|
@property
|
|
def training(self) -> TrainingServiceClient:
|
|
"""Get training service client"""
|
|
if self._training_client is None:
|
|
self._training_client = get_training_client(self.config, self.service_name)
|
|
return self._training_client
|
|
|
|
@property
|
|
def data(self) -> DataServiceClient:
|
|
"""Get data service client"""
|
|
if self._data_client is None:
|
|
self._data_client = get_data_client(self.config, self.service_name)
|
|
return self._data_client
|
|
|
|
@property
|
|
def forecast(self) -> ForecastServiceClient:
|
|
"""Get forecast service client"""
|
|
if self._forecast_client is None:
|
|
self._forecast_client = get_forecast_client(self.config, self.service_name)
|
|
return self._forecast_client
|
|
|
|
# Convenience function to get all clients
|
|
def get_service_clients(config: BaseServiceSettings = None, service_name: str = "unknown") -> ServiceClients:
|
|
"""Get a wrapper with all service clients"""
|
|
return ServiceClients(config, service_name)
|
|
|
|
# Export all classes for direct import
|
|
__all__ = [
|
|
'BaseServiceClient',
|
|
'ServiceAuthenticator',
|
|
'TrainingServiceClient',
|
|
'DataServiceClient',
|
|
'ForecastServiceClient',
|
|
'ServiceClients',
|
|
'get_training_client',
|
|
'get_data_client',
|
|
'get_forecast_client',
|
|
'get_service_clients'
|
|
] |