Skip to content

HuwiseConfig

Type-safe configuration management for Huwise API access.

Overview

HuwiseConfig is a Pydantic-based configuration class that provides:

  • Validation: Ensures required fields are present
  • Type safety: Full type hints and runtime validation
  • Dependency injection: Pass configuration to components
  • Secure logging: API keys are masked in string representations

Usage

From Environment Variables

from huwise_utils_py import HuwiseConfig

# Loads HUWISE_API_KEY (required by default),
# HUWISE_DOMAIN (optional, defaults to data.bs.ch), and HUWISE_API_TYPE (optional)
config = HuwiseConfig.from_env()

# Explicitly allow missing HUWISE_API_KEY (advanced/unauthenticated scenarios):
unauth_config = HuwiseConfig.from_env(require_api_key=False)

Programmatic

config = HuwiseConfig(
    api_key="your-api-key",
    domain="data.bs.ch",
    api_type="automation/v1.0",  # Optional, defaults to this
)

Properties

# Get the base URL for API requests
print(config.base_url)
# Output: https://data.bs.ch/api/automation/v1.0

# Get authorization headers
print(config.headers)
# Output with key: {"Authorization": "apikey your-api-key"}
# Output without key: {}

Dependency Injection

from huwise_utils_py import HuwiseConfig, HuwiseDataset

# Create a custom config for testing
test_config = HuwiseConfig(
    api_key="test-key",
    domain="test.example.com",
)

# Inject into components
dataset = HuwiseDataset.from_id("100123", config=test_config)

API Reference

HuwiseConfig

Bases: BaseModel

Configuration for Huwise API access.

Provides type-safe configuration management with environment variable loading and supports dependency injection patterns.

ATTRIBUTE DESCRIPTION
api_key

API key for Huwise authentication.

TYPE: str | None

domain

Huwise domain (e.g., data.bs.ch).

TYPE: str

api_type

API version/type (defaults to automation/v1.0).

TYPE: str

Example
config = HuwiseConfig.from_env()
print(config.base_url)
# Output: 'https://data.bs.ch/api/automation/v1.0'

api_key: str | None = Field(default=None, description='API key for Huwise authentication') class-attribute instance-attribute

domain: str = Field(default=DEFAULT_HUWISE_DOMAIN, description='Huwise domain (e.g., data.bs.ch)') class-attribute instance-attribute

api_type: str = Field(default='automation/v1.0', description='API version/type') class-attribute instance-attribute

base_url: str property

Construct the base API URL.

RETURNS DESCRIPTION
str

The full base URL for API requests without trailing slash.

headers: dict[str, str] property

Get authorization headers for API requests.

RETURNS DESCRIPTION
dict[str, str]

Dictionary containing the Authorization header when api_key exists.

from_env(*, require_api_key: bool = True) -> HuwiseConfig classmethod

Load configuration from environment variables.

For Automation API usage, API key authentication is required by default. The domain defaults to data.bs.ch when not set. Set require_api_key=False only for explicit unauthenticated scenarios.

RETURNS DESCRIPTION
HuwiseConfig

HuwiseConfig instance populated from environment.

RAISES DESCRIPTION
AppConfigError

If required environment variables are missing.

Source code in src/huwise_utils_py/config.py
@classmethod
def from_env(cls, *, require_api_key: bool = True) -> "HuwiseConfig":
    """Load configuration from environment variables.

    For Automation API usage, API key authentication is required by default.
    The domain defaults to ``data.bs.ch`` when not set.
    Set ``require_api_key=False`` only for explicit unauthenticated scenarios.

    Returns:
        HuwiseConfig instance populated from environment.

    Raises:
        AppConfigError: If required environment variables are missing.
    """
    api_key = get_env_or_throw("HUWISE_API_KEY") if require_api_key else os.getenv("HUWISE_API_KEY")
    return cls(
        api_key=api_key,
        domain=os.getenv("HUWISE_DOMAIN", DEFAULT_HUWISE_DOMAIN),
        api_type=os.getenv("HUWISE_API_TYPE", "automation/v1.0"),
    )