|
""" |
|
API configuration module. |
|
""" |
|
import os |
|
import logging |
|
import yaml |
|
from typing import Dict, Any, Optional |
|
import base64 |
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
_config_cache = None |
|
|
|
|
|
_supported_base_models = [] |
|
|
|
def get_api_config() -> Dict[str, Any]: |
|
""" |
|
Returns the API configuration. |
|
|
|
Returns: |
|
Dict[str, Any]: API configuration |
|
""" |
|
global _config_cache |
|
|
|
|
|
if _config_cache is not None: |
|
return _config_cache |
|
|
|
|
|
config_path = os.environ.get("MEZURA_CONFIG_PATH", "config/api_config.yaml") |
|
if os.path.exists(config_path): |
|
try: |
|
with open(config_path, 'r') as f: |
|
config = yaml.safe_load(f) |
|
_config_cache = config |
|
logger.info(f"Loaded API configuration from {config_path}") |
|
return config |
|
except Exception as e: |
|
logger.error(f"Error loading config from {config_path}: {e}") |
|
raise RuntimeError(f"Failed to load configuration from {config_path}: {e}") |
|
else: |
|
|
|
error_msg = f"Configuration file not found at {config_path}" |
|
logger.error(error_msg) |
|
raise FileNotFoundError(error_msg) |
|
|
|
def get_api_config_for_type(evaluation_type: str) -> Dict[str, Any]: |
|
""" |
|
Get API configuration for a specific evaluation type. |
|
|
|
Args: |
|
evaluation_type: Evaluation type (e.g., "evalmix") |
|
|
|
Returns: |
|
Dict[str, Any]: API configuration for the specified type |
|
""" |
|
config = get_api_config() |
|
|
|
|
|
api_type = evaluation_type.lower().replace("-", "_") |
|
|
|
|
|
if "apis" in config and api_type in config["apis"]: |
|
type_config = config["apis"][api_type] |
|
logger.debug(f"Using config for {evaluation_type}: {type_config}") |
|
return type_config |
|
|
|
|
|
if "default" in config: |
|
logger.warning(f"No configuration found for {evaluation_type}, using default") |
|
return config["default"] |
|
|
|
|
|
logger.warning(f"No configuration found for {evaluation_type} and no default config") |
|
return {} |
|
|
|
def get_airflow_config() -> Dict[str, Any]: |
|
""" |
|
Get Airflow API configuration. |
|
|
|
Returns: |
|
Dict[str, Any]: Airflow API configuration |
|
""" |
|
config = get_api_config() |
|
|
|
|
|
if "apis" in config and "airflow" in config["apis"]: |
|
airflow_config = config["apis"]["airflow"] |
|
logger.debug(f"Using Airflow config from YAML: {airflow_config}") |
|
|
|
|
|
env_base_url = os.environ.get("AIRFLOW_URL") |
|
if env_base_url: |
|
airflow_config["base_url"] = env_base_url |
|
logger.info(f"Loaded Airflow base_url from environment variable AIRFLOW_URL: {env_base_url}") |
|
else: |
|
logger.info(f"Using Airflow base_url from YAML config: {airflow_config.get('base_url')}") |
|
|
|
|
|
|
|
auth_config = airflow_config.get("auth", {}) |
|
if auth_config.get("use_env", False): |
|
|
|
username_env = auth_config.get("env_username", "MEZURA_API_USERNAME") |
|
password_env = auth_config.get("env_password", "MEZURA_API_PASSWORD") |
|
|
|
|
|
logger.info(f"Looking for credentials in environment variables: {username_env}, {password_env}") |
|
|
|
|
|
username = os.environ.get(username_env) |
|
password = os.environ.get(password_env) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger.info(f"Username variable '{username_env}' found: {username is not None}") |
|
logger.info(f"Password variable '{password_env}' found: {password is not None}") |
|
|
|
|
|
if username and password: |
|
auth_config["username"] = username |
|
auth_config["password"] = password |
|
|
|
airflow_config["auth"] = auth_config |
|
|
|
return airflow_config |
|
|
|
|
|
logger.warning("Airflow configuration not found in config file") |
|
return {} |
|
|
|
def update_base_model_list(models): |
|
""" |
|
Updates the list of supported base models. |
|
|
|
Args: |
|
models (list): List of supported model names |
|
""" |
|
global _supported_base_models |
|
_supported_base_models = models |
|
|
|
def get_base_model_list(): |
|
""" |
|
Returns the current list of supported base models. |
|
|
|
Returns: |
|
list: List of supported model names |
|
""" |
|
return _supported_base_models |