Common utilities and core components for Tencent Cloud SDK for Python, providing credential management, HTTP clients, authentication, error handling, and foundational classes required by all Tencent Cloud service SDKs
Flexible configuration system for HTTP settings, signature methods, language preferences, and regional failover. Supports customization of endpoints, timeouts, retry policies, and circuit breaker settings for robust API communication.
Main configuration class that combines HTTP settings, signature methods, language preferences, and resilience features.
class ClientProfile:
def __init__(self, signMethod: str = None, httpProfile: HttpProfile = None,
language: str = "zh-CN", disable_region_breaker: bool = True,
region_breaker_profile = None, request_client: str = None,
retryer = None):
"""
Create client profile configuration.
Args:
signMethod (str, optional): Signature method ("HmacSHA1", "HmacSHA256", "TC3-HMAC-SHA256")
Default: "TC3-HMAC-SHA256"
httpProfile (HttpProfile, optional): HTTP configuration
language (str): API response language ("zh-CN", "en-US"). Default: "zh-CN"
disable_region_breaker (bool): Disable circuit breaker. Default: True
region_breaker_profile: Circuit breaker configuration
request_client (str, optional): Custom request client identifier (max 128 chars)
retryer: Custom retry configuration
Raises:
TencentCloudSDKException: If language is not supported
"""HTTP-specific configuration including protocols, endpoints, timeouts, and connection settings.
class HttpProfile:
def __init__(self, protocol: str = None, endpoint: str = None,
reqMethod: str = "POST", reqTimeout: int = 60,
keepAlive: bool = False, proxy: str = None,
rootDomain: str = None, certification = None):
"""
Create HTTP profile configuration.
Args:
protocol (str, optional): Request schema ("http" or "https"). Default: "https"
endpoint (str, optional): Target domain (e.g., "cvm.tencentcloudapi.com")
reqMethod (str): HTTP method ("GET" or "POST"). Default: "POST"
reqTimeout (int): Request timeout in seconds. Default: 60
keepAlive (bool): Enable keep-alive connections. Default: False
proxy (str, optional): Proxy URL (e.g., "http://user:pass@host:port")
rootDomain (str, optional): Root domain. Default: "tencentcloudapi.com"
certification: Certificate path, True for default, False to disable verification
"""Circuit breaker configuration for regional failover to handle service outages gracefully.
class RegionBreakerProfile:
def __init__(self, backup_endpoint: str = "ap-guangzhou.tencentcloudapi.com",
max_fail_num: int = 5, max_fail_percent: float = 0.75,
window_interval: int = 300, timeout: int = 60,
max_requests: int = 5):
"""
Create region breaker profile.
Args:
backup_endpoint (str): Backup endpoint for failover
max_fail_num (int): Max failures to trigger breaker. Default: 5
max_fail_percent (float): Max failure percentage (0-1). Default: 0.75
window_interval (int): Reset counter interval in seconds. Default: 300 (5 minutes)
timeout (int): Open to half-open transition timeout. Default: 60 seconds
max_requests (int): Successful requests to close breaker. Default: 5
Raises:
TencentCloudSDKException: If endpoint format is invalid or fail percent out of range
"""
def check_endpoint(self) -> bool:
"""
Validate endpoint format.
Returns:
bool: True if endpoint format is valid
"""from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
# Create HTTP profile
http_profile = HttpProfile()
http_profile.endpoint = "cvm.tencentcloudapi.com"
http_profile.reqTimeout = 30
http_profile.keepAlive = True
# Create client profile
client_profile = ClientProfile()
client_profile.httpProfile = http_profile
client_profile.signMethod = "TC3-HMAC-SHA256"
client_profile.language = "en-US"from tencentcloud.common.profile.http_profile import HttpProfile
# Custom endpoint with HTTP
http_profile = HttpProfile()
http_profile.protocol = "http"
http_profile.endpoint = "custom-endpoint.example.com"
http_profile.reqMethod = "GET"from tencentcloud.common.profile.http_profile import HttpProfile
# HTTP profile with proxy
http_profile = HttpProfile()
http_profile.proxy = "http://proxy.example.com:8080"
# Proxy with authentication
http_profile = HttpProfile()
http_profile.proxy = "http://username:password@proxy.example.com:8080"from tencentcloud.common.profile.http_profile import HttpProfile
# Custom certificate bundle
http_profile = HttpProfile()
http_profile.certification = "/path/to/custom/ca-bundle.crt"
# Disable certificate verification (not recommended for production)
http_profile = HttpProfile()
http_profile.certification = Falsefrom tencentcloud.common.profile.client_profile import ClientProfile, RegionBreakerProfile
# Create region breaker profile
breaker_profile = RegionBreakerProfile(
backup_endpoint="ap-shanghai.tencentcloudapi.com",
max_fail_num=3,
max_fail_percent=0.5,
window_interval=180, # 3 minutes
timeout=30,
max_requests=3
)
# Create client profile with circuit breaker
client_profile = ClientProfile()
client_profile.disable_region_breaker = False
client_profile.region_breaker_profile = breaker_profilefrom tencentcloud.common.profile.client_profile import ClientProfile
# Set custom request client identifier
client_profile = ClientProfile()
client_profile.request_client = "MyApp/1.0.0"from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.retry import StandardRetryer
import logging
# Create custom retryer
logger = logging.getLogger("my_app")
retryer = StandardRetryer(
max_attempts=5,
backoff_fn=lambda n: min(2 ** n, 60), # Exponential backoff with max 60s
logger=logger
)
# Create client profile with custom retry
client_profile = ClientProfile()
client_profile.retryer = retryerfrom tencentcloud.common.credential import Credential
from tencentcloud.common.common_client import CommonClient
from tencentcloud.common.profile.client_profile import ClientProfile, RegionBreakerProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.retry import StandardRetryer
import logging
# Create credentials
cred = Credential("your-secret-id", "your-secret-key")
# Configure HTTP profile
http_profile = HttpProfile()
http_profile.endpoint = "cvm.tencentcloudapi.com"
http_profile.reqTimeout = 30
http_profile.keepAlive = True
http_profile.proxy = "http://proxy.example.com:8080"
# Configure circuit breaker
breaker_profile = RegionBreakerProfile(
backup_endpoint="ap-shanghai.tencentcloudapi.com",
max_fail_num=3,
max_fail_percent=0.6
)
# Configure retry
retryer = StandardRetryer(max_attempts=3)
# Create client profile
client_profile = ClientProfile()
client_profile.httpProfile = http_profile
client_profile.signMethod = "TC3-HMAC-SHA256"
client_profile.language = "en-US"
client_profile.disable_region_breaker = False
client_profile.region_breaker_profile = breaker_profile
client_profile.request_client = "MyApp/1.0.0"
client_profile.retryer = retryer
# Create client with full configuration
client = CommonClient("cvm", "2017-03-12", cred, "ap-guangzhou", client_profile)
# Make API call with all configured features
try:
response = client.call_json("DescribeInstances", {"Limit": 10})
print("Success:", response)
except Exception as e:
print("Error:", e)import os
from tencentcloud.common.profile.http_profile import HttpProfile
# Development configuration
if os.getenv("ENV") == "development":
http_profile = HttpProfile()
http_profile.protocol = "http"
http_profile.endpoint = "dev-api.example.com"
http_profile.certification = False
# Production configuration
else:
http_profile = HttpProfile()
http_profile.protocol = "https"
http_profile.endpoint = "cvm.tencentcloudapi.com"
http_profile.reqTimeout = 60
http_profile.keepAlive = TrueInstall with Tessl CLI
npx tessl i tessl/pypi-tencentcloud-sdk-python-common