Configuration API

The WeexClient uses a comprehensive configuration system designed for flexibility and security.

class weex_client.config.WeexConfig(api_key, secret_key, passphrase, environment='development', connection=ConnectionConfig(base_url='https://api-contract.weex.com', websocket_url='wss://api-contract.weex.com/ws/v2/streams', timeout=30.0, max_connections=20, max_keepalive_connections=10), retry=RetryConfig(max_retries=3, backoff_base=0.5, backoff_max=8.0, jitter=True), rate_limit=RateLimitConfig(requests_per_second=10, requests_per_minute=600, burst_size=20), logging=LoggingConfig(level='INFO', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', structured=False), version='0.2.0')[source]

Bases: object

Main configuration class.

Simplified version for testing without pydantic.

api_key: str
secret_key: str
passphrase: str
environment: weex_client.config.TypeAliasType
connection: weex_client.config.ConnectionConfig
retry: weex_client.config.RetryConfig
rate_limit: weex_client.config.RateLimitConfig
logging: weex_client.config.LoggingConfig
version: str
get_auth_headers()[source]

Get authentication headers for API requests.

is_production()[source]

Check if running in production environment.

get_base_url()[source]

Get appropriate base URL for environment.

get_websocket_url()[source]

Get WebSocket URL for environment.

__init__(api_key, secret_key, passphrase, environment='development', connection=ConnectionConfig(base_url='https://api-contract.weex.com', websocket_url='wss://api-contract.weex.com/ws/v2/streams', timeout=30.0, max_connections=20, max_keepalive_connections=10), retry=RetryConfig(max_retries=3, backoff_base=0.5, backoff_max=8.0, jitter=True), rate_limit=RateLimitConfig(requests_per_second=10, requests_per_minute=600, burst_size=20), logging=LoggingConfig(level='INFO', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', structured=False), version='0.2.0')

Connection Configuration

class weex_client.config.ConnectionConfig(base_url='https://api-contract.weex.com', websocket_url='wss://api-contract.weex.com/ws/v2/streams', timeout=30.0, max_connections=20, max_keepalive_connections=10)[source]

Connection configuration with immutable behavior.

base_url: weex_client.config.TypeAliasType
websocket_url: str
timeout: weex_client.config.TypeAliasType
max_connections: int
max_keepalive_connections: int
__init__(base_url='https://api-contract.weex.com', websocket_url='wss://api-contract.weex.com/ws/v2/streams', timeout=30.0, max_connections=20, max_keepalive_connections=10)

Retry Configuration

class weex_client.config.RetryConfig(max_retries=3, backoff_base=0.5, backoff_max=8.0, jitter=True)[source]

Retry configuration with exponential backoff.

max_retries: int
backoff_base: float
backoff_max: float
jitter: bool
__init__(max_retries=3, backoff_base=0.5, backoff_max=8.0, jitter=True)

Rate Limit Configuration

class weex_client.config.RateLimitConfig(requests_per_second=10, requests_per_minute=600, burst_size=20)[source]

Rate limiting configuration to prevent API abuse.

requests_per_second: int
requests_per_minute: int
burst_size: int
__init__(requests_per_second=10, requests_per_minute=600, burst_size=20)

Logging Configuration

class weex_client.config.LoggingConfig(level='INFO', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', structured=False)[source]

Logging configuration with validation.

level: weex_client.config.TypeAliasType
format: str
structured: bool
__init__(level='INFO', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', structured=False)

Environment Variables

The configuration system supports loading from environment variables:

WEEX_API_KEY

Your Weex API key. Starts with “weex_”.

WEEX_SECRET_KEY

Your Weex secret key for HMAC signature.

WEEX_PASSPHRASE

Your API passphrase.

WEEX_ENVIRONMENT

Target environment: development, staging, or production. Default: development

WEEX_TIMEOUT

Request timeout in seconds. Default: 30

WEEX_MAX_RETRIES

Maximum number of retry attempts. Default: 3

WEEX_LOG_LEVEL

Logging level: DEBUG, INFO, WARNING, ERROR. Default: INFO

Configuration Examples

Basic Configuration

from weex_client.config import WeexConfig

config = WeexConfig(
    api_key="your_api_key",
    secret_key="your_secret_key",
    passphrase="your_passphrase",
    environment="development"
)

From Environment

from weex_client.config import WeexConfig

# Load from environment variables
config = WeexConfig.from_env()

Advanced Configuration

from weex_client.config import WeexConfig, ConnectionConfig, RetryConfig

config = WeexConfig(
    api_key="your_api_key",
    secret_key="your_secret_key",
    passphrase="your_passphrase",
    connection=ConnectionConfig(
        timeout=60,
        max_connections=20
    ),
    retry=RetryConfig(
        max_attempts=5,
        backoff_factor=2.0
    ),
    environment="staging"
)

Trading Safety Configuration

from weex_client.config import WeexConfig

# For paper trading and testing
config = WeexConfig(
    api_key="paper_trading_key",
    secret_key="paper_trading_secret",
    passphrase="paper_trading_pass",
    environment="development"  # Always use development for testing
)

URL Resolution

The configuration automatically resolves API endpoints based on environment:

Environment URLs

Environment

Base URL

WebSocket URL

development

https://development-api.weex.com

wss://development-ws.weex.com

staging

https://staging-api.weex.com

wss://staging-ws.weex.com

production

https://api.weex.com

wss://ws.weex.com

Configuration Validation

The configuration system includes built-in validation:

try:
    config = WeexConfig.from_env()
    print(f"✅ Configuration valid for {config.environment}")
except ValueError as e:
    print(f"❌ Configuration error: {e}")

Best Practices

Trading Safety

  1. Always use development environment for testing and paper trading

  2. Never commit API credentials to version control

  3. Use separate API keys for development and production

  4. Enable IP whitelisting when possible

Configuration Management

  1. Use environment variables for sensitive data

  2. Store non-sensitive defaults in code

  3. Validate configuration before API calls

  4. Log configuration status (without secrets)

# Safe configuration logging
config = WeexConfig.from_env()
logger.info(f"Config loaded: environment={config.environment}, timeout={config.connection.timeout}")
# Never log API keys or secrets!