Skip to content

Commit 7b50119

Browse files
author
liyan.90210
committed
feat auto update sdk
1 parent 752e642 commit 7b50119

16 files changed

Lines changed: 520 additions & 80 deletions

Changelog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
### Change log
22

3+
2026-03-12 Bumped to version v1.0.217
4+
- Updated apis for tls/visual
5+
36
2026-02-05 Bumped to version v1.0.216
47
- Updated apis for content_security
58

volcengine/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# coding:utf-8
2-
VERSION='v1.0.216'
2+
VERSION='v1.0.217'
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# coding:utf-8
2+
from __future__ import print_function
3+
4+
from volcengine.visual.VisualService import VisualService
5+
6+
if __name__ == '__main__':
7+
visual_service = VisualService()
8+
9+
# call below method if you don't set ak and sk in $HOME/.volc/config
10+
visual_service.set_ak('Ak')
11+
visual_service.set_sk('Sk')
12+
13+
form = {
14+
"req_key": "xxx",
15+
"task_id": "123456"
16+
}
17+
resp = visual_service.cv_cancel_task(form)
18+
print(resp)

volcengine/tls/TLSService.py

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
from __future__ import print_function
55

66
import hashlib
7-
import threading
8-
import random
97
import time
108

119
from volcengine.ApiInfo import ApiInfo
@@ -19,6 +17,7 @@
1917
from volcengine.tls.tls_responses import DescribeETLTaskResponse, ModifyAlarmContentTemplateResponse
2018
from volcengine.tls.tls_responses import ModifyTraceInstanceResponse
2119
from volcengine.tls.tls_exception import TLSException
20+
from volcengine.tls.retry_policy import RetryPolicy
2221
from volcengine.tls.const import DELETE_TRACE_INSTANCE, DESCRIBE_TRACE_INSTANCE, DESCRIBE_TRACE, SEARCH_TRACES, \
2322
CREATE_ALARM_CONTENT_TEMPLATE, CREATE_ALARM_WEBHOOK_INTEGRATION, DELETE_ALARM_WEBHOOK_INTEGRATION
2423
from volcengine.tls.util import get_logger
@@ -161,38 +160,6 @@
161160

162161

163162
class TLSService(Service):
164-
_instance_lock = threading.Lock()
165-
_default_retry_interval_ms = 100
166-
_default_retry_counter = 0
167-
_default_retry_counter_maximum = 50
168-
169-
@staticmethod
170-
def set_default_retry_counter_maximum(v):
171-
TLSService._default_retry_counter_maximum = v
172-
173-
@staticmethod
174-
def increase_retry_counter_by_one():
175-
with TLSService._instance_lock:
176-
if TLSService._default_retry_counter < TLSService._default_retry_counter_maximum:
177-
TLSService._default_retry_counter += 1
178-
179-
@staticmethod
180-
def decrease_retry_counter_by_one():
181-
with TLSService._instance_lock:
182-
if TLSService._default_retry_counter < TLSService._default_retry_counter_maximum:
183-
TLSService._default_retry_counter -= 1
184-
185-
@staticmethod
186-
def calc_backoff_ms(expected_quit_timestamp_ms):
187-
current_timestamp_ms = int(time.time() * 1000)
188-
counter = TLSService._default_retry_counter
189-
sleep_ms = random.random() * counter * TLSService._default_retry_interval_ms
190-
if current_timestamp_ms + sleep_ms > expected_quit_timestamp_ms:
191-
sleep_ms = expected_quit_timestamp_ms - current_timestamp_ms
192-
if sleep_ms < 0:
193-
sleep_ms = 0
194-
return sleep_ms
195-
196163
def __init__(self, endpoint: str, access_key_id: str, access_key_secret: str, region: str,
197164
security_token: str = None, scheme: str = "https", timeout: int = 60,
198165
api_version=API_VERSION_V_0_3_0):
@@ -204,6 +171,7 @@ def __init__(self, endpoint: str, access_key_id: str, access_key_secret: str, re
204171
self.__scheme = scheme
205172
self.__timeout = timeout
206173
self.__api_version = api_version
174+
self.__retry_policy = None
207175

208176
self.check_scheme_and_endpoint()
209177

@@ -278,8 +246,10 @@ def __request(self, api: str, params: dict = None, body: dict = None, request_he
278246
method = self.api_info[api].method
279247
url = request.build()
280248

281-
expected_quit_timestamp = int(time.time() * 1000 + self.__timeout * 1500)
249+
policy = self.get_retry_policy()
250+
deadline = time.monotonic() + policy.total_timeout
282251
try_count = 0
252+
retry_index = 0
283253
while True:
284254
try_count += 1
285255
try:
@@ -288,27 +258,25 @@ def __request(self, api: str, params: dict = None, body: dict = None, request_he
288258
response = self.session.request(method, url, headers=request.headers, data=request.body,
289259
timeout=self.__timeout)
290260
except Exception as e:
291-
TLSService.increase_retry_counter_by_one()
292-
sleep_ms = TLSService.calc_backoff_ms(expected_quit_timestamp)
293-
if try_count < 5 and sleep_ms > 0:
294-
# HTTP请求未响应, 尝试重试
295-
time.sleep(sleep_ms / 1000)
296-
else:
297-
# 已超出重试上限, 退出
261+
if self.__can_retry(try_count, policy) is False or self.__should_retry_exception(e) is False:
298262
raise TLSException(error_code=e.__class__.__name__, error_message=e.__str__())
263+
sleep_seconds = self.__calc_backoff_seconds(policy, retry_index + 1, deadline)
264+
if sleep_seconds <= 0:
265+
raise TLSException(error_code="RequestTimeout",
266+
error_message="Request exceeded retry total timeout")
267+
time.sleep(sleep_seconds)
268+
retry_index += 1
299269
else:
300270
if response.status_code == 200:
301271
# self.__logger.info("TLS client successfully got the response for requesting {}".format(api))
302-
TLSService.decrease_retry_counter_by_one()
303272
return response
304-
elif try_count < 5 and response.status_code in [429, 500, 502, 503]:
305-
TLSService.increase_retry_counter_by_one()
306-
sleep_ms = TLSService.calc_backoff_ms(expected_quit_timestamp)
307-
if sleep_ms > 0:
308-
# HTTP请求未响应, 尝试重试
309-
time.sleep(sleep_ms / 1000)
310-
else:
311-
raise TLSException(response)
273+
elif response.status_code in [429, 500, 502, 503] and self.__can_retry(try_count, policy):
274+
sleep_seconds = self.__calc_backoff_seconds(policy, retry_index + 1, deadline)
275+
if sleep_seconds > 0:
276+
time.sleep(sleep_seconds)
277+
retry_index += 1
278+
continue
279+
raise TLSException(response)
312280
else:
313281
raise TLSException(response)
314282

@@ -322,6 +290,40 @@ def set_timeout(self, timeout: int):
322290
self.__timeout = timeout
323291
self.service_info = self.get_service_info()
324292

293+
def set_retry_policy(self, policy):
294+
if policy is None:
295+
self.__retry_policy = None
296+
return
297+
self.__retry_policy = policy.normalize()
298+
299+
def get_retry_policy(self):
300+
if self.__retry_policy is None:
301+
return RetryPolicy.default_policy()
302+
return self.__retry_policy.normalize()
303+
304+
def __can_retry(self, attempts, policy):
305+
if policy.max_attempts <= 0:
306+
return True
307+
return attempts < policy.max_attempts
308+
309+
def __calc_backoff_seconds(self, policy, retry_index, deadline):
310+
remaining = deadline - time.monotonic()
311+
if remaining <= 0:
312+
return 0
313+
sleep_seconds = policy.backoff_seconds(retry_index)
314+
if sleep_seconds > remaining:
315+
sleep_seconds = remaining
316+
return sleep_seconds
317+
318+
def __should_retry_exception(self, e):
319+
try:
320+
from requests import exceptions as req_exc
321+
except Exception:
322+
return True
323+
324+
return isinstance(e, (req_exc.Timeout, req_exc.ConnectionError, req_exc.ChunkedEncodingError,
325+
req_exc.ContentDecodingError))
326+
325327
def create_project(self, create_project_request: CreateProjectRequest) -> CreateProjectResponse:
326328
if create_project_request.check_validation() is False:
327329
raise TLSException(error_code="InvalidArgument", error_message="Invalid request, please check it")

volcengine/tls/const.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,14 @@
216216
COLD_TTL = "ColdTtl"
217217
ARCHIVE_TTL = "ArchiveTtl"
218218
ENCRYPT_CONF = "EncryptConf"
219+
BIND_PROCESSOR = "BindProcessor"
220+
PROCESSOR_ID = "ProcessorID"
221+
PROCESSOR_NAME = "ProcessorName"
222+
METERING_MODE = "MeteringMode"
223+
TLS_VERSION = "TLSVersion"
224+
REGIONS = "Regions"
225+
FUZZY_SEARCH_KEY = "FuzzySearchKey"
226+
ORDER_BY_PROJECT = "OrderByProject"
219227

220228
FULL_TEXT = "FullText"
221229
KEY_VALUE = "KeyValue"
@@ -449,6 +457,7 @@
449457
JSON_INFO = "JsonInfo"
450458
CSV_INFO = "CsvInfo"
451459
TRN = "trn"
460+
FROM_TLS = "from_tls"
452461
ENABLE_ENCRYPT_CONF = "enable"
453462
ENCRYPT_TYPE = "encrypt_type"
454463
REGION_ID = "region_id"

volcengine/tls/data.py

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,25 +145,30 @@ def get_tags(self):
145145

146146

147147
class EncryptUserCmkConf(TLSData):
148-
def __init__(self, user_cmk_id: str, trn: str, region_id: str):
148+
def __init__(self, user_cmk_id: str, trn: str, region_id: str, from_tls: bool = None):
149149
self.user_cmk_id = user_cmk_id
150150
self.trn = trn
151151
self.region_id = region_id
152+
self.from_tls = from_tls
152153

153154
@classmethod
154155
def set_attributes(cls, data):
155156
user_cmk_id = data.get(USER_CMK_ID)
156157
trn = data.get(TRN)
157158
region_id = data.get(REGION_ID)
159+
from_tls = data.get(FROM_TLS)
158160

159-
return cls(user_cmk_id, trn, region_id)
161+
return cls(user_cmk_id, trn, region_id, from_tls)
160162

161163
def json(self):
162-
return {
164+
res = {
163165
USER_CMK_ID: self.user_cmk_id,
164166
TRN: self.trn,
165167
REGION_ID: self.region_id,
166168
}
169+
if self.from_tls is not None:
170+
res[FROM_TLS] = self.from_tls
171+
return res
167172

168173

169174
class EncryptConf(TLSData):
@@ -176,7 +181,10 @@ def __init__(self, enable: bool = False, encrypt_type: str = "default", user_cmk
176181
def set_attributes(cls, data):
177182
enable = data.get(ENABLE_ENCRYPT_CONF)
178183
encrypt_type = data.get(ENCRYPT_TYPE)
179-
user_cmk_info = data.get(USER_CMK_INFO)
184+
user_cmk_info = None
185+
user_cmk_info_data = data.get(USER_CMK_INFO)
186+
if user_cmk_info_data is not None and isinstance(user_cmk_info_data, dict):
187+
user_cmk_info = EncryptUserCmkConf.set_attributes(data=user_cmk_info_data)
180188

181189
return cls(enable, encrypt_type, user_cmk_info)
182190

@@ -188,13 +196,32 @@ def json(self):
188196
}
189197

190198

199+
class BindProcessor(TLSData):
200+
def __init__(self, processor_id: str = None, processor_name: str = None):
201+
self.processor_id = processor_id
202+
self.processor_name = processor_name
203+
204+
@classmethod
205+
def set_attributes(cls, data: dict):
206+
processor_id = data.get(PROCESSOR_ID)
207+
processor_name = data.get(PROCESSOR_NAME)
208+
return cls(processor_id, processor_name)
209+
210+
def json(self):
211+
return {
212+
PROCESSOR_ID: self.processor_id,
213+
PROCESSOR_NAME: self.processor_name,
214+
}
215+
216+
191217
class TopicInfo(TLSData):
192218
def __init__(self, topic_name: str = None, topic_id: str = None, project_id: str = None, ttl: int = None,
193219
create_time: str = None, modify_time: str = None, shard_count: int = None, description: str = None,
194220
auto_split: bool = None, max_split_shard: int = None, enable_tracking: bool = None,
195221
time_key: str = None, time_format: str = None, tags: List[TagInfo] = None, log_public_ip: bool = None,
196222
enable_hot_ttl: bool = None, hot_ttl: int = None, cold_ttl: int = None, archive_ttl: int = None,
197-
encrypt_conf: EncryptConf = None):
223+
encrypt_conf: EncryptConf = None, bind_processor=None, metering_mode: str = None,
224+
project_name: str = None, region: str = None, tls_version: str = None):
198225
self.topic_name = topic_name
199226
self.topic_id = topic_id
200227
self.project_id = project_id
@@ -215,12 +242,25 @@ def __init__(self, topic_name: str = None, topic_id: str = None, project_id: str
215242
self.cold_ttl = cold_ttl
216243
self.archive_ttl = archive_ttl
217244
self.encrypt_conf = encrypt_conf
245+
self.bind_processor = bind_processor
246+
self.metering_mode = metering_mode
247+
self.project_name = project_name
248+
self.region = region
249+
self.tls_version = tls_version
218250

219251
@classmethod
220252
def set_attributes(cls, data: dict):
253+
bind_processor = None
254+
bind_processor_data = data.get(BIND_PROCESSOR)
255+
if bind_processor_data is not None and isinstance(bind_processor_data, dict):
256+
bind_processor = BindProcessor.set_attributes(data=bind_processor_data)
257+
metering_mode = data.get(METERING_MODE)
221258
topic_name = data.get(TOPIC_NAME)
222259
topic_id = data.get(TOPIC_ID)
223260
project_id = data.get(PROJECT_ID)
261+
project_name = data.get(PROJECT_NAME)
262+
region = data.get(REGION)
263+
tls_version = data.get(TLS_VERSION)
224264
ttl = data.get(TTL)
225265
create_time = data.get(CREATE_TIME)
226266
modify_time = data.get(MODIFY_TIME)
@@ -237,7 +277,10 @@ def set_attributes(cls, data: dict):
237277
hot_ttl = data.get(HOT_TTL)
238278
cold_ttl = data.get(COLD_TTL)
239279
archive_ttl = data.get(ARCHIVE_TTL)
240-
encrypt_conf = data.get(ENCRYPT_CONF)
280+
encrypt_conf = None
281+
encrypt_conf_data = data.get(ENCRYPT_CONF)
282+
if encrypt_conf_data is not None and isinstance(encrypt_conf_data, dict):
283+
encrypt_conf = EncryptConf.set_attributes(data=encrypt_conf_data)
241284

242285
tags = data.get(TAGS)
243286
if tags is not None:
@@ -248,7 +291,8 @@ def set_attributes(cls, data: dict):
248291

249292
return cls(topic_name, topic_id, project_id, ttl, create_time, modify_time, shard_count, description,
250293
auto_split, max_split_shard, enable_tracking, time_key, time_format, topic_tags, log_public_ip,
251-
enable_hot_ttl, hot_ttl, cold_ttl, archive_ttl, encrypt_conf)
294+
enable_hot_ttl, hot_ttl, cold_ttl, archive_ttl, encrypt_conf, bind_processor, metering_mode,
295+
project_name, region, tls_version)
252296

253297
def get_create_time(self):
254298
"""

0 commit comments

Comments
 (0)