0
# Configuration Profiles
1
2
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.
3
4
## Capabilities
5
6
### Client Profile Configuration
7
8
Main configuration class that combines HTTP settings, signature methods, language preferences, and resilience features.
9
10
```python { .api }
11
class ClientProfile:
12
def __init__(self, signMethod: str = None, httpProfile: HttpProfile = None,
13
language: str = "zh-CN", disable_region_breaker: bool = True,
14
region_breaker_profile = None, request_client: str = None,
15
retryer = None):
16
"""
17
Create client profile configuration.
18
19
Args:
20
signMethod (str, optional): Signature method ("HmacSHA1", "HmacSHA256", "TC3-HMAC-SHA256")
21
Default: "TC3-HMAC-SHA256"
22
httpProfile (HttpProfile, optional): HTTP configuration
23
language (str): API response language ("zh-CN", "en-US"). Default: "zh-CN"
24
disable_region_breaker (bool): Disable circuit breaker. Default: True
25
region_breaker_profile: Circuit breaker configuration
26
request_client (str, optional): Custom request client identifier (max 128 chars)
27
retryer: Custom retry configuration
28
29
Raises:
30
TencentCloudSDKException: If language is not supported
31
"""
32
```
33
34
### HTTP Profile Configuration
35
36
HTTP-specific configuration including protocols, endpoints, timeouts, and connection settings.
37
38
```python { .api }
39
class HttpProfile:
40
def __init__(self, protocol: str = None, endpoint: str = None,
41
reqMethod: str = "POST", reqTimeout: int = 60,
42
keepAlive: bool = False, proxy: str = None,
43
rootDomain: str = None, certification = None):
44
"""
45
Create HTTP profile configuration.
46
47
Args:
48
protocol (str, optional): Request schema ("http" or "https"). Default: "https"
49
endpoint (str, optional): Target domain (e.g., "cvm.tencentcloudapi.com")
50
reqMethod (str): HTTP method ("GET" or "POST"). Default: "POST"
51
reqTimeout (int): Request timeout in seconds. Default: 60
52
keepAlive (bool): Enable keep-alive connections. Default: False
53
proxy (str, optional): Proxy URL (e.g., "http://user:pass@host:port")
54
rootDomain (str, optional): Root domain. Default: "tencentcloudapi.com"
55
certification: Certificate path, True for default, False to disable verification
56
"""
57
```
58
59
### Region Breaker Profile
60
61
Circuit breaker configuration for regional failover to handle service outages gracefully.
62
63
```python { .api }
64
class RegionBreakerProfile:
65
def __init__(self, backup_endpoint: str = "ap-guangzhou.tencentcloudapi.com",
66
max_fail_num: int = 5, max_fail_percent: float = 0.75,
67
window_interval: int = 300, timeout: int = 60,
68
max_requests: int = 5):
69
"""
70
Create region breaker profile.
71
72
Args:
73
backup_endpoint (str): Backup endpoint for failover
74
max_fail_num (int): Max failures to trigger breaker. Default: 5
75
max_fail_percent (float): Max failure percentage (0-1). Default: 0.75
76
window_interval (int): Reset counter interval in seconds. Default: 300 (5 minutes)
77
timeout (int): Open to half-open transition timeout. Default: 60 seconds
78
max_requests (int): Successful requests to close breaker. Default: 5
79
80
Raises:
81
TencentCloudSDKException: If endpoint format is invalid or fail percent out of range
82
"""
83
84
def check_endpoint(self) -> bool:
85
"""
86
Validate endpoint format.
87
88
Returns:
89
bool: True if endpoint format is valid
90
"""
91
```
92
93
## Usage Examples
94
95
### Basic Configuration
96
97
```python
98
from tencentcloud.common.profile.client_profile import ClientProfile
99
from tencentcloud.common.profile.http_profile import HttpProfile
100
101
# Create HTTP profile
102
http_profile = HttpProfile()
103
http_profile.endpoint = "cvm.tencentcloudapi.com"
104
http_profile.reqTimeout = 30
105
http_profile.keepAlive = True
106
107
# Create client profile
108
client_profile = ClientProfile()
109
client_profile.httpProfile = http_profile
110
client_profile.signMethod = "TC3-HMAC-SHA256"
111
client_profile.language = "en-US"
112
```
113
114
### Custom Endpoint and Protocol
115
116
```python
117
from tencentcloud.common.profile.http_profile import HttpProfile
118
119
# Custom endpoint with HTTP
120
http_profile = HttpProfile()
121
http_profile.protocol = "http"
122
http_profile.endpoint = "custom-endpoint.example.com"
123
http_profile.reqMethod = "GET"
124
```
125
126
### Proxy Configuration
127
128
```python
129
from tencentcloud.common.profile.http_profile import HttpProfile
130
131
# HTTP profile with proxy
132
http_profile = HttpProfile()
133
http_profile.proxy = "http://proxy.example.com:8080"
134
135
# Proxy with authentication
136
http_profile = HttpProfile()
137
http_profile.proxy = "http://username:password@proxy.example.com:8080"
138
```
139
140
### Certificate Configuration
141
142
```python
143
from tencentcloud.common.profile.http_profile import HttpProfile
144
145
# Custom certificate bundle
146
http_profile = HttpProfile()
147
http_profile.certification = "/path/to/custom/ca-bundle.crt"
148
149
# Disable certificate verification (not recommended for production)
150
http_profile = HttpProfile()
151
http_profile.certification = False
152
```
153
154
### Circuit Breaker Configuration
155
156
```python
157
from tencentcloud.common.profile.client_profile import ClientProfile, RegionBreakerProfile
158
159
# Create region breaker profile
160
breaker_profile = RegionBreakerProfile(
161
backup_endpoint="ap-shanghai.tencentcloudapi.com",
162
max_fail_num=3,
163
max_fail_percent=0.5,
164
window_interval=180, # 3 minutes
165
timeout=30,
166
max_requests=3
167
)
168
169
# Create client profile with circuit breaker
170
client_profile = ClientProfile()
171
client_profile.disable_region_breaker = False
172
client_profile.region_breaker_profile = breaker_profile
173
```
174
175
### Custom Request Client
176
177
```python
178
from tencentcloud.common.profile.client_profile import ClientProfile
179
180
# Set custom request client identifier
181
client_profile = ClientProfile()
182
client_profile.request_client = "MyApp/1.0.0"
183
```
184
185
### Retry Configuration
186
187
```python
188
from tencentcloud.common.profile.client_profile import ClientProfile
189
from tencentcloud.common.retry import StandardRetryer
190
import logging
191
192
# Create custom retryer
193
logger = logging.getLogger("my_app")
194
retryer = StandardRetryer(
195
max_attempts=5,
196
backoff_fn=lambda n: min(2 ** n, 60), # Exponential backoff with max 60s
197
logger=logger
198
)
199
200
# Create client profile with custom retry
201
client_profile = ClientProfile()
202
client_profile.retryer = retryer
203
```
204
205
### Complete Configuration Example
206
207
```python
208
from tencentcloud.common.credential import Credential
209
from tencentcloud.common.common_client import CommonClient
210
from tencentcloud.common.profile.client_profile import ClientProfile, RegionBreakerProfile
211
from tencentcloud.common.profile.http_profile import HttpProfile
212
from tencentcloud.common.retry import StandardRetryer
213
import logging
214
215
# Create credentials
216
cred = Credential("your-secret-id", "your-secret-key")
217
218
# Configure HTTP profile
219
http_profile = HttpProfile()
220
http_profile.endpoint = "cvm.tencentcloudapi.com"
221
http_profile.reqTimeout = 30
222
http_profile.keepAlive = True
223
http_profile.proxy = "http://proxy.example.com:8080"
224
225
# Configure circuit breaker
226
breaker_profile = RegionBreakerProfile(
227
backup_endpoint="ap-shanghai.tencentcloudapi.com",
228
max_fail_num=3,
229
max_fail_percent=0.6
230
)
231
232
# Configure retry
233
retryer = StandardRetryer(max_attempts=3)
234
235
# Create client profile
236
client_profile = ClientProfile()
237
client_profile.httpProfile = http_profile
238
client_profile.signMethod = "TC3-HMAC-SHA256"
239
client_profile.language = "en-US"
240
client_profile.disable_region_breaker = False
241
client_profile.region_breaker_profile = breaker_profile
242
client_profile.request_client = "MyApp/1.0.0"
243
client_profile.retryer = retryer
244
245
# Create client with full configuration
246
client = CommonClient("cvm", "2017-03-12", cred, "ap-guangzhou", client_profile)
247
248
# Make API call with all configured features
249
try:
250
response = client.call_json("DescribeInstances", {"Limit": 10})
251
print("Success:", response)
252
except Exception as e:
253
print("Error:", e)
254
```
255
256
### Environment-Specific Configurations
257
258
```python
259
import os
260
from tencentcloud.common.profile.http_profile import HttpProfile
261
262
# Development configuration
263
if os.getenv("ENV") == "development":
264
http_profile = HttpProfile()
265
http_profile.protocol = "http"
266
http_profile.endpoint = "dev-api.example.com"
267
http_profile.certification = False
268
269
# Production configuration
270
else:
271
http_profile = HttpProfile()
272
http_profile.protocol = "https"
273
http_profile.endpoint = "cvm.tencentcloudapi.com"
274
http_profile.reqTimeout = 60
275
http_profile.keepAlive = True
276
```