Skip to content

Configuration

The Config class manages client configuration from environment variables, files, and programmatic input.

Config dataclass

Configuration for MicroDC client.

Can be loaded from environment variables, config file, or passed directly.

Attributes:

Name Type Description
api_key str

MicroDC API key

base_url str

Base URL for API

timeout int

Request timeout in seconds

verify_ssl bool

Whether to verify SSL certificates

auto_start_polling bool

Automatically start polling thread

poll_interval float

Polling interval in seconds

max_retries int

Maximum retry attempts

retry_backoff float

Exponential backoff multiplier

default_model Optional[str]

Default model to use

default_priority str

Default job priority

Source code in microdc/core/config.py
@dataclass
class Config:
    """
    Configuration for MicroDC client.

    Can be loaded from environment variables, config file, or passed directly.

    Attributes:
        api_key: MicroDC API key
        base_url: Base URL for API
        timeout: Request timeout in seconds
        verify_ssl: Whether to verify SSL certificates
        auto_start_polling: Automatically start polling thread
        poll_interval: Polling interval in seconds
        max_retries: Maximum retry attempts
        retry_backoff: Exponential backoff multiplier
        default_model: Default model to use
        default_priority: Default job priority
    """

    api_key: str = ""
    base_url: str = "https://api.microdc.ai"
    timeout: int = 30
    verify_ssl: bool = True
    auto_start_polling: bool = True
    poll_interval: float = 2.0
    max_retries: int = 3
    retry_backoff: float = 2.0
    default_model: Optional[str] = None
    default_priority: str = "standard"

    @classmethod
    def from_env(cls) -> "Config":
        """
        Load configuration from environment variables.

        Environment variables:
            MICRODC_API_KEY: API key
            MICRODC_BASE_URL: Base URL
            MICRODC_TIMEOUT: Timeout in seconds
            MICRODC_VERIFY_SSL: Verify SSL (true/false)
            MICRODC_AUTO_POLLING: Auto start polling (true/false)
            MICRODC_POLL_INTERVAL: Poll interval in seconds
        """
        return cls(
            api_key=os.getenv("MICRODC_API_KEY", ""),
            base_url=os.getenv("MICRODC_BASE_URL", "https://api.microdc.ai"),
            timeout=int(os.getenv("MICRODC_TIMEOUT", "30")),
            verify_ssl=os.getenv("MICRODC_VERIFY_SSL", "true").lower() == "true",
            auto_start_polling=os.getenv("MICRODC_AUTO_POLLING", "true").lower() == "true",
            poll_interval=float(os.getenv("MICRODC_POLL_INTERVAL", "2.0")),
        )

    @classmethod
    def from_file(cls, config_path: str = ".microdc.json") -> "Config":
        """
        Load configuration from JSON file.

        Args:
            config_path: Path to config file (default: .microdc.json)

        Returns:
            Config object

        Raises:
            FileNotFoundError: If config file doesn't exist
            json.JSONDecodeError: If config file is invalid JSON
        """
        path = Path(config_path)
        if not path.exists():
            raise FileNotFoundError(f"Config file not found: {config_path}")

        with path.open("r") as f:
            data = json.load(f)

        return cls(**data)

    @classmethod
    def load(cls, config_path: Optional[str] = None) -> "Config":
        """
        Load configuration from file, environment, or defaults.

        Priority:
            1. Config file (if specified)
            2. Environment variables
            3. Defaults

        Args:
            config_path: Optional path to config file

        Returns:
            Config object
        """
        # Try config file first
        if config_path:
            return cls.from_file(config_path)

        # Try default config file
        default_path = Path(".microdc.json")
        if default_path.exists():
            return cls.from_file(str(default_path))

        # Fall back to environment variables
        return cls.from_env()

    def validate(self) -> None:
        """
        Validate configuration.

        Raises:
            ValueError: If configuration is invalid
        """
        if not self.api_key:
            raise ValueError("API key is required")

        if not self.api_key.startswith("mDC_"):
            raise ValueError("API key must start with 'mDC_'")

        if self.timeout <= 0:
            raise ValueError("Timeout must be positive")

        if self.poll_interval <= 0:
            raise ValueError("Poll interval must be positive")

        if self.max_retries < 0:
            raise ValueError("Max retries must be non-negative")

from_env() classmethod

Load configuration from environment variables.

Environment variables

MICRODC_API_KEY: API key MICRODC_BASE_URL: Base URL MICRODC_TIMEOUT: Timeout in seconds MICRODC_VERIFY_SSL: Verify SSL (true/false) MICRODC_AUTO_POLLING: Auto start polling (true/false) MICRODC_POLL_INTERVAL: Poll interval in seconds

Source code in microdc/core/config.py
@classmethod
def from_env(cls) -> "Config":
    """
    Load configuration from environment variables.

    Environment variables:
        MICRODC_API_KEY: API key
        MICRODC_BASE_URL: Base URL
        MICRODC_TIMEOUT: Timeout in seconds
        MICRODC_VERIFY_SSL: Verify SSL (true/false)
        MICRODC_AUTO_POLLING: Auto start polling (true/false)
        MICRODC_POLL_INTERVAL: Poll interval in seconds
    """
    return cls(
        api_key=os.getenv("MICRODC_API_KEY", ""),
        base_url=os.getenv("MICRODC_BASE_URL", "https://api.microdc.ai"),
        timeout=int(os.getenv("MICRODC_TIMEOUT", "30")),
        verify_ssl=os.getenv("MICRODC_VERIFY_SSL", "true").lower() == "true",
        auto_start_polling=os.getenv("MICRODC_AUTO_POLLING", "true").lower() == "true",
        poll_interval=float(os.getenv("MICRODC_POLL_INTERVAL", "2.0")),
    )

from_file(config_path='.microdc.json') classmethod

Load configuration from JSON file.

Parameters:

Name Type Description Default
config_path str

Path to config file (default: .microdc.json)

'.microdc.json'

Returns:

Type Description
Config

Config object

Raises:

Type Description
FileNotFoundError

If config file doesn't exist

JSONDecodeError

If config file is invalid JSON

Source code in microdc/core/config.py
@classmethod
def from_file(cls, config_path: str = ".microdc.json") -> "Config":
    """
    Load configuration from JSON file.

    Args:
        config_path: Path to config file (default: .microdc.json)

    Returns:
        Config object

    Raises:
        FileNotFoundError: If config file doesn't exist
        json.JSONDecodeError: If config file is invalid JSON
    """
    path = Path(config_path)
    if not path.exists():
        raise FileNotFoundError(f"Config file not found: {config_path}")

    with path.open("r") as f:
        data = json.load(f)

    return cls(**data)

load(config_path=None) classmethod

Load configuration from file, environment, or defaults.

Priority
  1. Config file (if specified)
  2. Environment variables
  3. Defaults

Parameters:

Name Type Description Default
config_path Optional[str]

Optional path to config file

None

Returns:

Type Description
Config

Config object

Source code in microdc/core/config.py
@classmethod
def load(cls, config_path: Optional[str] = None) -> "Config":
    """
    Load configuration from file, environment, or defaults.

    Priority:
        1. Config file (if specified)
        2. Environment variables
        3. Defaults

    Args:
        config_path: Optional path to config file

    Returns:
        Config object
    """
    # Try config file first
    if config_path:
        return cls.from_file(config_path)

    # Try default config file
    default_path = Path(".microdc.json")
    if default_path.exists():
        return cls.from_file(str(default_path))

    # Fall back to environment variables
    return cls.from_env()

validate()

Validate configuration.

Raises:

Type Description
ValueError

If configuration is invalid

Source code in microdc/core/config.py
def validate(self) -> None:
    """
    Validate configuration.

    Raises:
        ValueError: If configuration is invalid
    """
    if not self.api_key:
        raise ValueError("API key is required")

    if not self.api_key.startswith("mDC_"):
        raise ValueError("API key must start with 'mDC_'")

    if self.timeout <= 0:
        raise ValueError("Timeout must be positive")

    if self.poll_interval <= 0:
        raise ValueError("Poll interval must be positive")

    if self.max_retries < 0:
        raise ValueError("Max retries must be non-negative")