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:
objectMain configuration class.
Simplified version for testing without pydantic.
-
environment:
weex_client.config.TypeAliasType
-
connection:
weex_client.config.ConnectionConfig
-
rate_limit:
weex_client.config.RateLimitConfig
-
logging:
weex_client.config.LoggingConfig
- __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')
-
environment:
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
-
timeout:
weex_client.config.TypeAliasType
- __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)
-
base_url:
Retry Configuration
Rate Limit Configuration
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
- __init__(level='INFO', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', structured=False)
-
level:
Environment Variables
The configuration system supports loading from environment variables:
- WEEX_SECRET_KEY
Your Weex secret key for HMAC signature.
- WEEX_PASSPHRASE
Your API passphrase.
- WEEX_ENVIRONMENT
Target environment:
development,staging, orproduction. 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 |
Base URL |
WebSocket URL |
|---|---|---|
development |
wss://development-ws.weex.com |
|
staging |
wss://staging-ws.weex.com |
|
production |
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
Always use development environment for testing and paper trading
Never commit API credentials to version control
Use separate API keys for development and production
Enable IP whitelisting when possible
Configuration Management
Use environment variables for sensitive data
Store non-sensitive defaults in code
Validate configuration before API calls
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!