byteplus-sdk 1.0.62__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- byteplus_sdk/ApiInfo.py +13 -0
- byteplus_sdk/Credentials.py +15 -0
- byteplus_sdk/Policy.py +91 -0
- byteplus_sdk/ServiceInfo.py +11 -0
- byteplus_sdk/ServiceInfoHttps.py +11 -0
- byteplus_sdk/__init__.py +2 -0
- byteplus_sdk/auth/MetaData.py +29 -0
- byteplus_sdk/auth/SignerV4.py +145 -0
- byteplus_sdk/auth/__init__.py +0 -0
- byteplus_sdk/base/Request.py +51 -0
- byteplus_sdk/base/Service.py +227 -0
- byteplus_sdk/base/__init__.py +0 -0
- byteplus_sdk/base/models/__init__.py +0 -0
- byteplus_sdk/base/models/base/__init__.py +0 -0
- byteplus_sdk/base/models/base/base_pb2.py +46 -0
- byteplus_sdk/cdn/__init__.py +0 -0
- byteplus_sdk/cdn/service.py +1203 -0
- byteplus_sdk/const/Const.py +66 -0
- byteplus_sdk/const/__init__.py +0 -0
- byteplus_sdk/example/__init__.py +1 -0
- byteplus_sdk/example/cdn/__init__.py +3 -0
- byteplus_sdk/example/cdn/add_cdn_certificate.py +27 -0
- byteplus_sdk/example/cdn/add_cdn_domain.py +35 -0
- byteplus_sdk/example/cdn/add_template_domain.py +26 -0
- byteplus_sdk/example/cdn/create_cipher_template.py +25 -0
- byteplus_sdk/example/cdn/create_rule_engine_template.py +161 -0
- byteplus_sdk/example/cdn/create_service_template.py +41 -0
- byteplus_sdk/example/cdn/delete_cdn_domain.py +19 -0
- byteplus_sdk/example/cdn/delete_template.py +20 -0
- byteplus_sdk/example/cdn/describe_accounting_data.py +24 -0
- byteplus_sdk/example/cdn/describe_cdn_access_log.py +23 -0
- byteplus_sdk/example/cdn/describe_cdn_config.py +19 -0
- byteplus_sdk/example/cdn/describe_cdn_data.py +29 -0
- byteplus_sdk/example/cdn/describe_cdn_data_detail.py +25 -0
- byteplus_sdk/example/cdn/describe_cdn_detail.py +26 -0
- byteplus_sdk/example/cdn/describe_cdn_origin_data.py +24 -0
- byteplus_sdk/example/cdn/describe_cdn_region_and_isp.py +19 -0
- byteplus_sdk/example/cdn/describe_cdn_service.py +15 -0
- byteplus_sdk/example/cdn/describe_cdn_upper_ip.py +19 -0
- byteplus_sdk/example/cdn/describe_cipher_template.py +20 -0
- byteplus_sdk/example/cdn/describe_content_block_tasks.py +23 -0
- byteplus_sdk/example/cdn/describe_content_quota.py +14 -0
- byteplus_sdk/example/cdn/describe_content_tasks.py +21 -0
- byteplus_sdk/example/cdn/describe_edge_nrt_data_summary.py +23 -0
- byteplus_sdk/example/cdn/describe_edge_statistical_data.py +24 -0
- byteplus_sdk/example/cdn/describe_edge_top_nrt_data.py +25 -0
- byteplus_sdk/example/cdn/describe_edge_top_statistical_data.py +25 -0
- byteplus_sdk/example/cdn/describe_edge_top_status_code.py +22 -0
- byteplus_sdk/example/cdn/describe_ip_info.py +19 -0
- byteplus_sdk/example/cdn/describe_ip_list_info.py +20 -0
- byteplus_sdk/example/cdn/describe_origin_nrt_data_summary.py +23 -0
- byteplus_sdk/example/cdn/describe_origin_top_nrt_data.py +24 -0
- byteplus_sdk/example/cdn/describe_origin_top_status_code.py +22 -0
- byteplus_sdk/example/cdn/describe_rule_engine_template.py +23 -0
- byteplus_sdk/example/cdn/describe_service_template.py +20 -0
- byteplus_sdk/example/cdn/describe_template_domains.py +22 -0
- byteplus_sdk/example/cdn/describe_templates.py +30 -0
- byteplus_sdk/example/cdn/duplicate_template.py +23 -0
- byteplus_sdk/example/cdn/list_cdn_cert_info.py +19 -0
- byteplus_sdk/example/cdn/list_cdn_domains.py +19 -0
- byteplus_sdk/example/cdn/list_cert_info.py +20 -0
- byteplus_sdk/example/cdn/lock_template.py +20 -0
- byteplus_sdk/example/cdn/release_template.py +23 -0
- byteplus_sdk/example/cdn/start_cdn_domain.py +19 -0
- byteplus_sdk/example/cdn/stop_cdn_domain.py +19 -0
- byteplus_sdk/example/cdn/submit_block_task.py +18 -0
- byteplus_sdk/example/cdn/submit_preload_task.py +18 -0
- byteplus_sdk/example/cdn/submit_refresh_task.py +19 -0
- byteplus_sdk/example/cdn/submit_unblock_task.py +18 -0
- byteplus_sdk/example/cdn/update_cdn_config.py +34 -0
- byteplus_sdk/example/cdn/update_cipher_template.py +48 -0
- byteplus_sdk/example/cdn/update_rule_engine_template.py +62 -0
- byteplus_sdk/example/cdn/update_service_template.py +42 -0
- byteplus_sdk/example/cdn/update_template_domain.py +29 -0
- byteplus_sdk/example/live/__init__.py +0 -0
- byteplus_sdk/example/rtc/RtcService.py +53 -0
- byteplus_sdk/example/rtc/__init__.py +0 -0
- byteplus_sdk/example/rtc/example_get_record_task.py +25 -0
- byteplus_sdk/example/rtc/example_start_record.py +86 -0
- byteplus_sdk/example/visual/__init__.py +0 -0
- byteplus_sdk/example/visual/cv_cancel_task.py +19 -0
- byteplus_sdk/example/visual/cv_get_result.py +19 -0
- byteplus_sdk/example/visual/cv_process.py +20 -0
- byteplus_sdk/example/visual/cv_submit_task.py +19 -0
- byteplus_sdk/example/visual/cv_sync2async_get_result.py +20 -0
- byteplus_sdk/example/visual/cv_sync2async_submit_task.py +20 -0
- byteplus_sdk/example/visual/example_common.py +32 -0
- byteplus_sdk/iam/IamService.py +42 -0
- byteplus_sdk/iam/__init__.py +0 -0
- byteplus_sdk/live/LiveService.py +571 -0
- byteplus_sdk/live/__init__.py +1 -0
- byteplus_sdk/live/v20200801/__init__.py +1 -0
- byteplus_sdk/live/v20200801/live_config.py +18 -0
- byteplus_sdk/live/v20200801/live_service.py +12 -0
- byteplus_sdk/live/v20200801/live_trait.py +62 -0
- byteplus_sdk/live/v20230101/__init__.py +1 -0
- byteplus_sdk/live/v20230101/live_config.py +171 -0
- byteplus_sdk/live/v20230101/live_service.py +12 -0
- byteplus_sdk/live/v20230101/live_trait.py +1133 -0
- byteplus_sdk/sms/SmsService.py +158 -0
- byteplus_sdk/sms/__init__.py +0 -0
- byteplus_sdk/util/Functions.py +37 -0
- byteplus_sdk/util/Util.py +116 -0
- byteplus_sdk/util/__init__.py +0 -0
- byteplus_sdk/visual/VisualService.py +142 -0
- byteplus_sdk/visual/__init__.py +0 -0
- byteplus_sdk/vod/VodService.py +2941 -0
- byteplus_sdk/vod/VodServiceConfig.py +134 -0
- byteplus_sdk/vod/__init__.py +0 -0
- byteplus_sdk/vod/models/__init__.py +0 -0
- byteplus_sdk/vod/models/business/__init__.py +0 -0
- byteplus_sdk/vod/models/business/vod_apps_manage_pb2.py +35 -0
- byteplus_sdk/vod/models/business/vod_callback_pb2.py +32 -0
- byteplus_sdk/vod/models/business/vod_cdn_pb2.py +434 -0
- byteplus_sdk/vod/models/business/vod_common_pb2.py +342 -0
- byteplus_sdk/vod/models/business/vod_edit_pb2.py +55 -0
- byteplus_sdk/vod/models/business/vod_measure_pb2.py +395 -0
- byteplus_sdk/vod/models/business/vod_media_pb2.py +547 -0
- byteplus_sdk/vod/models/business/vod_play_pb2.py +95 -0
- byteplus_sdk/vod/models/business/vod_project_pb2.py +45 -0
- byteplus_sdk/vod/models/business/vod_quality_platform_pb2.py +46 -0
- byteplus_sdk/vod/models/business/vod_smart_strategy_pb2.py +53 -0
- byteplus_sdk/vod/models/business/vod_space_pb2.py +63 -0
- byteplus_sdk/vod/models/business/vod_upload_pb2.py +344 -0
- byteplus_sdk/vod/models/business/vod_workflow_pb2.py +389 -0
- byteplus_sdk/vod/models/request/__init__.py +0 -0
- byteplus_sdk/vod/models/request/request_vod_pb2.py +1250 -0
- byteplus_sdk/vod/models/response/__init__.py +0 -0
- byteplus_sdk/vod/models/response/response_vod_pb2.py +1199 -0
- byteplus_sdk-1.0.62.dist-info/METADATA +24 -0
- byteplus_sdk-1.0.62.dist-info/RECORD +134 -0
- byteplus_sdk-1.0.62.dist-info/WHEEL +5 -0
- byteplus_sdk-1.0.62.dist-info/licenses/LICENSE +201 -0
- byteplus_sdk-1.0.62.dist-info/top_level.txt +1 -0
byteplus_sdk/ApiInfo.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# coding:utf-8
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class ApiInfo(object):
|
|
5
|
+
def __init__(self, method, path, query, form, header):
|
|
6
|
+
self.method = method
|
|
7
|
+
self.path = path
|
|
8
|
+
self.query = query
|
|
9
|
+
self.form = form
|
|
10
|
+
self.header = header
|
|
11
|
+
|
|
12
|
+
def __str__(self):
|
|
13
|
+
return 'method: ' + self.method + ', path: ' + self.path
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# coding:utf-8
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Credentials(object):
|
|
5
|
+
def __init__(self, ak, sk, service, region):
|
|
6
|
+
self.ak = ak
|
|
7
|
+
self.sk = sk
|
|
8
|
+
self.service = service
|
|
9
|
+
self.region = region
|
|
10
|
+
|
|
11
|
+
def set_ak(self, ak):
|
|
12
|
+
self.ak = ak
|
|
13
|
+
|
|
14
|
+
def set_sk(self, sk):
|
|
15
|
+
self.sk = sk
|
byteplus_sdk/Policy.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# coding:utf-8
|
|
2
|
+
import json
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class ComplexEncoder(json.JSONEncoder):
|
|
6
|
+
def default(self, o):
|
|
7
|
+
if isinstance(o, Statement):
|
|
8
|
+
return {'Effect': o.effect,
|
|
9
|
+
'Action': o.action,
|
|
10
|
+
'Resource': o.resource}
|
|
11
|
+
if isinstance(o, InnerToken):
|
|
12
|
+
return {
|
|
13
|
+
'LTAccessKeyId': o.lt_access_key_id,
|
|
14
|
+
'AccessKeyId': o.access_key_id,
|
|
15
|
+
'SignedSecretAccessKey': o.signed_secret_access_key,
|
|
16
|
+
'ExpiredTime': o.expired_time,
|
|
17
|
+
'PolicyString': o.policy_string,
|
|
18
|
+
'Signature': o.signature
|
|
19
|
+
}
|
|
20
|
+
if isinstance(o, Policy):
|
|
21
|
+
return {
|
|
22
|
+
'Statement': [item for item in o.statements]
|
|
23
|
+
}
|
|
24
|
+
return json.JSONEncoder.default(self, o)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class Policy(object):
|
|
28
|
+
def __init__(self, statements):
|
|
29
|
+
self.statements = statements
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class Statement(object):
|
|
33
|
+
def __init__(self):
|
|
34
|
+
self.effect = ''
|
|
35
|
+
self.action = []
|
|
36
|
+
self.resource = []
|
|
37
|
+
self.condition = ''
|
|
38
|
+
|
|
39
|
+
@staticmethod
|
|
40
|
+
def new_allow_statement(actions, resources):
|
|
41
|
+
s = Statement()
|
|
42
|
+
s.effect = "Allow"
|
|
43
|
+
s.action = actions
|
|
44
|
+
s.resource = resources
|
|
45
|
+
return s
|
|
46
|
+
|
|
47
|
+
@staticmethod
|
|
48
|
+
def new_deny_statement(actions, resources):
|
|
49
|
+
s = Statement()
|
|
50
|
+
s.effect = "Deny"
|
|
51
|
+
s.action = actions
|
|
52
|
+
s.resource = resources
|
|
53
|
+
return s
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class SecurityToken2(object):
|
|
57
|
+
def __init__(self):
|
|
58
|
+
self.access_key_id = ''
|
|
59
|
+
self.secret_access_key = ''
|
|
60
|
+
self.session_token = ''
|
|
61
|
+
self.expired_time = ''
|
|
62
|
+
self.current_time = ''
|
|
63
|
+
|
|
64
|
+
def __str__(self):
|
|
65
|
+
return json.dumps({
|
|
66
|
+
'AccessKeyID': self.access_key_id,
|
|
67
|
+
'SecretAccessKey': self.secret_access_key,
|
|
68
|
+
'SessionToken': self.session_token,
|
|
69
|
+
'ExpiredTime': self.expired_time,
|
|
70
|
+
'CurrentTime': self.current_time
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class InnerToken(object):
|
|
75
|
+
def __init__(self):
|
|
76
|
+
self.lt_access_key_id = ''
|
|
77
|
+
self.access_key_id = ''
|
|
78
|
+
self.signed_secret_access_key = ''
|
|
79
|
+
self.expired_time = 0
|
|
80
|
+
self.policy_string = ''
|
|
81
|
+
self.signature = ''
|
|
82
|
+
|
|
83
|
+
def __str__(self):
|
|
84
|
+
return json.dumps({
|
|
85
|
+
'LTAccessKeyId': self.lt_access_key_id,
|
|
86
|
+
'AccessKeyId': self.access_key_id,
|
|
87
|
+
'SignedSecretAccessKey': self.signed_secret_access_key,
|
|
88
|
+
'ExpiredTime': self.expired_time,
|
|
89
|
+
'PolicyString': self.policy_string,
|
|
90
|
+
'Signature': self.signature
|
|
91
|
+
})
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class ServiceInfo(object):
|
|
5
|
+
def __init__(self, host, header, credentials, connection_timeout, socket_timeout, scheme='http'):
|
|
6
|
+
self.host = host
|
|
7
|
+
self.header = header
|
|
8
|
+
self.credentials = credentials
|
|
9
|
+
self.connection_timeout = connection_timeout
|
|
10
|
+
self.socket_timeout = socket_timeout
|
|
11
|
+
self.scheme = scheme
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class ServiceInfoHttps(object):
|
|
5
|
+
def __init__(self, host, header, credentials, connection_timeout, socket_timeout, scheme='https'):
|
|
6
|
+
self.host = host
|
|
7
|
+
self.header = header
|
|
8
|
+
self.credentials = credentials
|
|
9
|
+
self.connection_timeout = connection_timeout
|
|
10
|
+
self.socket_timeout = socket_timeout
|
|
11
|
+
self.scheme = scheme
|
byteplus_sdk/__init__.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# coding:utf-8
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class MetaData(object):
|
|
5
|
+
def __init__(self):
|
|
6
|
+
self.algorithm = ''
|
|
7
|
+
self.credential_scope = ''
|
|
8
|
+
self.signed_headers = ''
|
|
9
|
+
self.date = ''
|
|
10
|
+
self.region = ''
|
|
11
|
+
self.service = ''
|
|
12
|
+
|
|
13
|
+
def set_date(self, date):
|
|
14
|
+
self.date = date
|
|
15
|
+
|
|
16
|
+
def set_service(self, service):
|
|
17
|
+
self.service = service
|
|
18
|
+
|
|
19
|
+
def set_region(self, region):
|
|
20
|
+
self.region = region
|
|
21
|
+
|
|
22
|
+
def set_algorithm(self, algorithm):
|
|
23
|
+
self.algorithm = algorithm
|
|
24
|
+
|
|
25
|
+
def set_credential_scope(self, credential_scope):
|
|
26
|
+
self.credential_scope = credential_scope
|
|
27
|
+
|
|
28
|
+
def set_signed_headers(self, signed_headers):
|
|
29
|
+
self.signed_headers = signed_headers
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
import datetime
|
|
3
|
+
import sys
|
|
4
|
+
import json
|
|
5
|
+
import pytz
|
|
6
|
+
|
|
7
|
+
try:
|
|
8
|
+
from urllib import urlencode
|
|
9
|
+
except:
|
|
10
|
+
from urllib.parse import urlencode
|
|
11
|
+
|
|
12
|
+
from byteplus_sdk.auth.MetaData import MetaData
|
|
13
|
+
from byteplus_sdk.util.Util import Util
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class SignerV4(object):
|
|
17
|
+
@staticmethod
|
|
18
|
+
def sign(request, credentials):
|
|
19
|
+
if request.path == '':
|
|
20
|
+
request.path = '/'
|
|
21
|
+
if request.method != 'GET' and not ('Content-Type' in request.headers):
|
|
22
|
+
request.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'
|
|
23
|
+
|
|
24
|
+
format_date = SignerV4.get_current_format_date()
|
|
25
|
+
request.headers['X-Date'] = format_date
|
|
26
|
+
|
|
27
|
+
md = MetaData()
|
|
28
|
+
md.set_algorithm('HMAC-SHA256')
|
|
29
|
+
md.set_service(credentials.service)
|
|
30
|
+
md.set_region(credentials.region)
|
|
31
|
+
md.set_date(format_date[:8])
|
|
32
|
+
|
|
33
|
+
hashed_canon_req = SignerV4.hashed_canonical_request_v4(request, md)
|
|
34
|
+
md.set_credential_scope('/'.join([md.date, md.region, md.service, 'request']))
|
|
35
|
+
|
|
36
|
+
signing_str = '\n'.join([md.algorithm, format_date, md.credential_scope, hashed_canon_req])
|
|
37
|
+
signing_key = SignerV4.get_signing_secret_key_v4(credentials.sk, md.date, md.region, md.service)
|
|
38
|
+
sign = Util.to_hex(Util.hmac_sha256(signing_key, signing_str))
|
|
39
|
+
request.headers['Authorization'] = SignerV4.build_auth_header_v4(sign, md, credentials)
|
|
40
|
+
return
|
|
41
|
+
|
|
42
|
+
@staticmethod
|
|
43
|
+
def sign_url(request, credentials):
|
|
44
|
+
format_date = SignerV4.get_current_format_date()
|
|
45
|
+
date = format_date[:8]
|
|
46
|
+
|
|
47
|
+
md = MetaData()
|
|
48
|
+
md.set_date(date)
|
|
49
|
+
md.set_service(credentials.service)
|
|
50
|
+
md.set_region(credentials.region)
|
|
51
|
+
md.set_signed_headers('')
|
|
52
|
+
md.set_algorithm('HMAC-SHA256')
|
|
53
|
+
md.set_credential_scope('/'.join([md.date, md.region, md.service, 'request']))
|
|
54
|
+
|
|
55
|
+
query = request.query
|
|
56
|
+
query['X-Date'] = format_date
|
|
57
|
+
query['X-NotSignBody'] = ''
|
|
58
|
+
query['X-Credential'] = credentials.ak + '/' + md.credential_scope
|
|
59
|
+
query['X-Algorithm'] = md.algorithm
|
|
60
|
+
query['X-SignedHeaders'] = md.signed_headers
|
|
61
|
+
query['X-SignedQueries'] = ''
|
|
62
|
+
query['X-SignedQueries'] = ';'.join(sorted(query.keys()))
|
|
63
|
+
|
|
64
|
+
hashed_canon_req = SignerV4.hashed_simple_canonical_request_v4(request, md)
|
|
65
|
+
signing_str = '\n'.join([md.algorithm, format_date, md.credential_scope, hashed_canon_req])
|
|
66
|
+
signing_key = SignerV4.get_signing_secret_key_v4(credentials.sk, md.date, md.region, md.service)
|
|
67
|
+
sign = SignerV4.signature_v4(signing_key, signing_str)
|
|
68
|
+
|
|
69
|
+
query['X-Signature'] = sign
|
|
70
|
+
return urlencode(query)
|
|
71
|
+
|
|
72
|
+
@staticmethod
|
|
73
|
+
def hashed_simple_canonical_request_v4(request, meta):
|
|
74
|
+
body = bytes()
|
|
75
|
+
if sys.version_info[0] == 3:
|
|
76
|
+
body_hash = Util.sha256(body.decode('utf-8'))
|
|
77
|
+
else:
|
|
78
|
+
body_hash = Util.sha256(body)
|
|
79
|
+
|
|
80
|
+
if request.path == '':
|
|
81
|
+
request.path = '/'
|
|
82
|
+
|
|
83
|
+
canoncial_request = '\n'.join(
|
|
84
|
+
[request.method, Util.norm_uri(request.path), Util.norm_query(request.query), '\n',
|
|
85
|
+
meta.signed_headers, body_hash])
|
|
86
|
+
return Util.sha256(canoncial_request)
|
|
87
|
+
|
|
88
|
+
@staticmethod
|
|
89
|
+
def hashed_canonical_request_v4(request, meta):
|
|
90
|
+
# 当content_type=json时request.body内部是个dict,
|
|
91
|
+
# 为了进行正确签名需要将其转成json进行hash
|
|
92
|
+
if type(request.body) == dict:
|
|
93
|
+
body_hash = Util.sha256(json.dumps(request.body))
|
|
94
|
+
else:
|
|
95
|
+
body_hash = Util.sha256(request.body)
|
|
96
|
+
|
|
97
|
+
request.headers['X-Content-Sha256'] = body_hash
|
|
98
|
+
|
|
99
|
+
signed_headers = dict()
|
|
100
|
+
for key in request.headers:
|
|
101
|
+
if key in ['Content-Type', 'Content-Md5', 'Host'] or key.startswith('X-'):
|
|
102
|
+
signed_headers[key.lower()] = request.headers[key]
|
|
103
|
+
|
|
104
|
+
if 'host' in signed_headers:
|
|
105
|
+
v = signed_headers['host']
|
|
106
|
+
if v.find(':') != -1:
|
|
107
|
+
split = v.split(':')
|
|
108
|
+
port = split[1]
|
|
109
|
+
if str(port) == '80' or str(port) == '443':
|
|
110
|
+
signed_headers['host'] = split[0]
|
|
111
|
+
|
|
112
|
+
signed_str = ''
|
|
113
|
+
for key in sorted(signed_headers.keys()):
|
|
114
|
+
signed_str += key + ':' + signed_headers[key] + '\n'
|
|
115
|
+
|
|
116
|
+
meta.set_signed_headers(';'.join(sorted(signed_headers.keys())))
|
|
117
|
+
|
|
118
|
+
canoncial_request = '\n'.join(
|
|
119
|
+
[request.method, Util.norm_uri(request.path), Util.norm_query(request.query), signed_str,
|
|
120
|
+
meta.signed_headers, body_hash])
|
|
121
|
+
|
|
122
|
+
return Util.sha256(canoncial_request)
|
|
123
|
+
|
|
124
|
+
@staticmethod
|
|
125
|
+
def signature_v4(signing_key, signing_str):
|
|
126
|
+
return Util.to_hex(Util.hmac_sha256(signing_key, signing_str))
|
|
127
|
+
|
|
128
|
+
@staticmethod
|
|
129
|
+
def get_signing_secret_key_v4(sk, date, region, service):
|
|
130
|
+
if sys.version_info[0] == 3:
|
|
131
|
+
kdate = Util.hmac_sha256(bytes(sk, encoding='utf-8'), date)
|
|
132
|
+
else:
|
|
133
|
+
kdate = Util.hmac_sha256(sk, date)
|
|
134
|
+
kregion = Util.hmac_sha256(kdate, region)
|
|
135
|
+
kservice = Util.hmac_sha256(kregion, service)
|
|
136
|
+
return Util.hmac_sha256(kservice, 'request')
|
|
137
|
+
|
|
138
|
+
@staticmethod
|
|
139
|
+
def build_auth_header_v4(signature, meta, credentials):
|
|
140
|
+
credential = credentials.ak + '/' + meta.credential_scope
|
|
141
|
+
return meta.algorithm + ' Credential=' + credential + ', SignedHeaders=' + meta.signed_headers + ', Signature=' + signature
|
|
142
|
+
|
|
143
|
+
@staticmethod
|
|
144
|
+
def get_current_format_date():
|
|
145
|
+
return datetime.datetime.now(tz=pytz.timezone('UTC')).strftime("%Y%m%dT%H%M%SZ")
|
|
File without changes
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
from collections import OrderedDict
|
|
3
|
+
|
|
4
|
+
try:
|
|
5
|
+
from urllib import urlencode
|
|
6
|
+
except:
|
|
7
|
+
from urllib.parse import urlencode
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Request(object):
|
|
11
|
+
def __init__(self):
|
|
12
|
+
self.schema = ''
|
|
13
|
+
self.method = ''
|
|
14
|
+
self.host = ''
|
|
15
|
+
self.path = ''
|
|
16
|
+
self.headers = OrderedDict()
|
|
17
|
+
self.query = OrderedDict()
|
|
18
|
+
self.body = ''
|
|
19
|
+
self.form = dict()
|
|
20
|
+
self.connection_timeout = 0
|
|
21
|
+
self.socket_timeout = 0
|
|
22
|
+
|
|
23
|
+
def set_shema(self, schema):
|
|
24
|
+
self.schema = schema
|
|
25
|
+
|
|
26
|
+
def set_method(self, method):
|
|
27
|
+
self.method = method
|
|
28
|
+
|
|
29
|
+
def set_host(self, host):
|
|
30
|
+
self.host = host
|
|
31
|
+
|
|
32
|
+
def set_path(self, path):
|
|
33
|
+
self.path = path
|
|
34
|
+
|
|
35
|
+
def set_headers(self, headers):
|
|
36
|
+
self.headers = headers
|
|
37
|
+
|
|
38
|
+
def set_query(self, query):
|
|
39
|
+
self.query = query
|
|
40
|
+
|
|
41
|
+
def set_body(self, body):
|
|
42
|
+
self.body = body
|
|
43
|
+
|
|
44
|
+
def set_connection_timeout(self, connection_timeout):
|
|
45
|
+
self.connection_timeout = connection_timeout
|
|
46
|
+
|
|
47
|
+
def set_socket_timeout(self, socket_timeout):
|
|
48
|
+
self.socket_timeout = socket_timeout
|
|
49
|
+
|
|
50
|
+
def build(self, doseq=0):
|
|
51
|
+
return self.schema + '://' + self.host + self.path + '?' + urlencode(self.query, doseq)
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
import json
|
|
3
|
+
import os
|
|
4
|
+
import time
|
|
5
|
+
from collections import OrderedDict
|
|
6
|
+
|
|
7
|
+
try:
|
|
8
|
+
from urllib.parse import urlencode
|
|
9
|
+
except ImportError:
|
|
10
|
+
from urllib import urlencode
|
|
11
|
+
|
|
12
|
+
import requests
|
|
13
|
+
|
|
14
|
+
from byteplus_sdk.Policy import SecurityToken2, InnerToken, ComplexEncoder
|
|
15
|
+
from byteplus_sdk.auth.SignerV4 import SignerV4
|
|
16
|
+
from byteplus_sdk.base.Request import Request
|
|
17
|
+
from byteplus_sdk.util.Util import *
|
|
18
|
+
from byteplus_sdk import VERSION
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class Service(object):
|
|
22
|
+
def __init__(self, service_info, api_info):
|
|
23
|
+
self.service_info = service_info
|
|
24
|
+
self.api_info = api_info
|
|
25
|
+
self.session = requests.session()
|
|
26
|
+
self.init()
|
|
27
|
+
|
|
28
|
+
def init(self):
|
|
29
|
+
if 'BYTEPLUS_ACCESSKEY' in os.environ and 'BYTEPLUS_SECRETKEY' in os.environ:
|
|
30
|
+
self.service_info.credentials.set_ak(os.environ['BYTEPLUS_ACCESSKEY'])
|
|
31
|
+
self.service_info.credentials.set_sk(os.environ['BYTEPLUS_SECRETKEY'])
|
|
32
|
+
else:
|
|
33
|
+
if os.environ.get('HOME', None) is None:
|
|
34
|
+
return
|
|
35
|
+
|
|
36
|
+
path = os.environ['HOME'] + '/.byteplus/config'
|
|
37
|
+
if os.path.isfile(path):
|
|
38
|
+
with open(path, 'r') as f:
|
|
39
|
+
j = json.load(f)
|
|
40
|
+
if 'ak' in j:
|
|
41
|
+
self.service_info.credentials.set_ak(j['ak'])
|
|
42
|
+
if 'sk' in j:
|
|
43
|
+
self.service_info.credentials.set_sk(j['sk'])
|
|
44
|
+
|
|
45
|
+
def set_ak(self, ak):
|
|
46
|
+
self.service_info.credentials.set_ak(ak)
|
|
47
|
+
|
|
48
|
+
def set_sk(self, sk):
|
|
49
|
+
self.service_info.credentials.set_sk(sk)
|
|
50
|
+
|
|
51
|
+
def set_host(self, host):
|
|
52
|
+
self.service_info.host = host
|
|
53
|
+
|
|
54
|
+
def set_scheme(self, scheme):
|
|
55
|
+
self.service_info.scheme = scheme
|
|
56
|
+
|
|
57
|
+
def get_sign_url(self, api, params):
|
|
58
|
+
if not (api in self.api_info):
|
|
59
|
+
raise Exception("no such api")
|
|
60
|
+
api_info = self.api_info[api]
|
|
61
|
+
|
|
62
|
+
mquery = self.merge(api_info.query, params)
|
|
63
|
+
r = Request()
|
|
64
|
+
r.set_shema(self.service_info.scheme)
|
|
65
|
+
r.set_method(api_info.method)
|
|
66
|
+
r.set_path(api_info.path)
|
|
67
|
+
r.set_query(mquery)
|
|
68
|
+
|
|
69
|
+
return SignerV4.sign_url(r, self.service_info.credentials)
|
|
70
|
+
|
|
71
|
+
def get(self, api, params, doseq=0):
|
|
72
|
+
if not (api in self.api_info):
|
|
73
|
+
raise Exception("no such api")
|
|
74
|
+
api_info = self.api_info[api]
|
|
75
|
+
|
|
76
|
+
r = self.prepare_request(api_info, params, doseq)
|
|
77
|
+
|
|
78
|
+
SignerV4.sign(r, self.service_info.credentials)
|
|
79
|
+
|
|
80
|
+
url = r.build(doseq)
|
|
81
|
+
resp = self.session.get(url, headers=r.headers,
|
|
82
|
+
timeout=(self.service_info.connection_timeout, self.service_info.socket_timeout))
|
|
83
|
+
if resp.status_code == 200:
|
|
84
|
+
return resp.text
|
|
85
|
+
else:
|
|
86
|
+
raise Exception(resp.text)
|
|
87
|
+
|
|
88
|
+
def post(self, api, params, form):
|
|
89
|
+
if not (api in self.api_info):
|
|
90
|
+
raise Exception("no such api")
|
|
91
|
+
api_info = self.api_info[api]
|
|
92
|
+
r = self.prepare_request(api_info, params)
|
|
93
|
+
r.headers['Content-Type'] = 'application/x-www-form-urlencoded'
|
|
94
|
+
r.form = self.merge(api_info.form, form)
|
|
95
|
+
r.body = urlencode(r.form, True)
|
|
96
|
+
SignerV4.sign(r, self.service_info.credentials)
|
|
97
|
+
|
|
98
|
+
url = r.build()
|
|
99
|
+
|
|
100
|
+
resp = self.session.post(url, headers=r.headers, data=r.form,
|
|
101
|
+
timeout=(self.service_info.connection_timeout, self.service_info.socket_timeout))
|
|
102
|
+
if resp.status_code == 200:
|
|
103
|
+
return resp.text
|
|
104
|
+
else:
|
|
105
|
+
raise Exception(resp.text)
|
|
106
|
+
|
|
107
|
+
def json(self, api, params, body):
|
|
108
|
+
if not (api in self.api_info):
|
|
109
|
+
raise Exception("no such api")
|
|
110
|
+
api_info = self.api_info[api]
|
|
111
|
+
r = self.prepare_request(api_info, params)
|
|
112
|
+
r.headers['Content-Type'] = 'application/json'
|
|
113
|
+
r.body = body
|
|
114
|
+
|
|
115
|
+
SignerV4.sign(r, self.service_info.credentials)
|
|
116
|
+
|
|
117
|
+
url = r.build()
|
|
118
|
+
# 发送content-type为json的http body
|
|
119
|
+
if api_info.method == "GET":
|
|
120
|
+
resp = self.session.get(url, headers=r.headers, json=r.body,
|
|
121
|
+
timeout=(self.service_info.connection_timeout, self.service_info.socket_timeout))
|
|
122
|
+
else:
|
|
123
|
+
resp = self.session.post(url, headers=r.headers, json=r.body,
|
|
124
|
+
timeout=(self.service_info.connection_timeout, self.service_info.socket_timeout))
|
|
125
|
+
if resp.status_code == 200:
|
|
126
|
+
return json.dumps(resp.json())
|
|
127
|
+
else:
|
|
128
|
+
raise Exception(resp.text)
|
|
129
|
+
|
|
130
|
+
def put(self, url, file_path, headers):
|
|
131
|
+
with open(file_path, 'rb') as f:
|
|
132
|
+
resp = self.session.put(url, headers=headers, data=f)
|
|
133
|
+
if resp.status_code == 200:
|
|
134
|
+
return True, resp.text
|
|
135
|
+
else:
|
|
136
|
+
return False, resp.text
|
|
137
|
+
|
|
138
|
+
def put_data(self, url, data, headers):
|
|
139
|
+
resp = self.session.put(url, headers=headers, data=data)
|
|
140
|
+
if resp.status_code == 200:
|
|
141
|
+
return True, resp.text
|
|
142
|
+
else:
|
|
143
|
+
return False, resp.text
|
|
144
|
+
|
|
145
|
+
def prepare_request(self, api_info, params, doseq=0):
|
|
146
|
+
for key in params:
|
|
147
|
+
if type(params[key]) == int or type(params[key]) == float:
|
|
148
|
+
params[key] = str(params[key])
|
|
149
|
+
elif type(params[key]) == list:
|
|
150
|
+
if not doseq:
|
|
151
|
+
params[key] = ','.join(params[key])
|
|
152
|
+
|
|
153
|
+
connection_timeout = self.service_info.connection_timeout
|
|
154
|
+
socket_timeout = self.service_info.socket_timeout
|
|
155
|
+
|
|
156
|
+
r = Request()
|
|
157
|
+
r.set_shema(self.service_info.scheme)
|
|
158
|
+
r.set_method(api_info.method)
|
|
159
|
+
r.set_connection_timeout(connection_timeout)
|
|
160
|
+
r.set_socket_timeout(socket_timeout)
|
|
161
|
+
|
|
162
|
+
mheaders = self.merge(api_info.header, self.service_info.header)
|
|
163
|
+
mheaders['Host'] = self.service_info.host
|
|
164
|
+
mheaders['User-Agent'] = 'byteplus-sdk-python/' + VERSION
|
|
165
|
+
r.set_headers(mheaders)
|
|
166
|
+
|
|
167
|
+
mquery = self.merge(api_info.query, params)
|
|
168
|
+
r.set_query(mquery)
|
|
169
|
+
|
|
170
|
+
r.set_host(self.service_info.host)
|
|
171
|
+
r.set_path(api_info.path)
|
|
172
|
+
|
|
173
|
+
return r
|
|
174
|
+
|
|
175
|
+
def merge(self, param1, param2):
|
|
176
|
+
od = OrderedDict()
|
|
177
|
+
for key in param1:
|
|
178
|
+
od[key] = param1[key]
|
|
179
|
+
|
|
180
|
+
for key in param2:
|
|
181
|
+
od[key] = param2[key]
|
|
182
|
+
|
|
183
|
+
return od
|
|
184
|
+
|
|
185
|
+
def sign_sts2(self, policy, expire):
|
|
186
|
+
sk = self.service_info.credentials.sk
|
|
187
|
+
key = hashlib.md5(sk.encode('utf-8')).digest()
|
|
188
|
+
|
|
189
|
+
sts = SecurityToken2()
|
|
190
|
+
sts.access_key_id = Util.generate_access_key_id('AKTP')
|
|
191
|
+
sts.secret_access_key = Util.generate_secret_key()
|
|
192
|
+
now = int(time.time())
|
|
193
|
+
sts.current_time = Service.to_rfc3339(now)
|
|
194
|
+
|
|
195
|
+
if expire < 60:
|
|
196
|
+
expire = 60
|
|
197
|
+
expire = now + expire
|
|
198
|
+
sts.expired_time = Service.to_rfc3339(expire)
|
|
199
|
+
|
|
200
|
+
inner_token = InnerToken()
|
|
201
|
+
inner_token.lt_access_key_id = self.service_info.credentials.ak
|
|
202
|
+
inner_token.access_key_id = sts.access_key_id
|
|
203
|
+
if policy is None:
|
|
204
|
+
inner_token.policy_string = ''
|
|
205
|
+
else:
|
|
206
|
+
inner_token.policy_string = json.dumps(
|
|
207
|
+
policy, cls=ComplexEncoder, sort_keys=True).replace(' ', '')
|
|
208
|
+
inner_token.signed_secret_access_key = Util.aes_encrypt_cbc_with_base64(
|
|
209
|
+
sts.secret_access_key, key)
|
|
210
|
+
inner_token.expired_time = expire
|
|
211
|
+
|
|
212
|
+
sign_str = '{}|{}|{}|{}|{}'.format(inner_token.lt_access_key_id, inner_token.access_key_id,
|
|
213
|
+
inner_token.expired_time, inner_token.signed_secret_access_key,
|
|
214
|
+
inner_token.policy_string)
|
|
215
|
+
inner_token.signature = Util.to_hex(Util.hmac_sha256(key, sign_str))
|
|
216
|
+
|
|
217
|
+
sts.session_token = 'STS2' + base64.b64encode(
|
|
218
|
+
json.dumps(inner_token, cls=ComplexEncoder, sort_keys=True).replace(' ', '').encode('utf-8')).decode()
|
|
219
|
+
return sts
|
|
220
|
+
|
|
221
|
+
@staticmethod
|
|
222
|
+
def to_rfc3339(t):
|
|
223
|
+
format_time = time.strftime('%Y-%m-%dT%H:%M:%S%z', time.localtime(t))
|
|
224
|
+
pos = format_time.find('+')
|
|
225
|
+
if pos == -1:
|
|
226
|
+
pos = format_time.find('-')
|
|
227
|
+
return format_time[:pos + 3] + ':' + format_time[pos + 3:pos + 5]
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
|
3
|
+
# source: byteplus/base/base.proto
|
|
4
|
+
"""Generated protocol buffer code."""
|
|
5
|
+
from google.protobuf import descriptor as _descriptor
|
|
6
|
+
from google.protobuf import descriptor_pool as _descriptor_pool
|
|
7
|
+
from google.protobuf import message as _message
|
|
8
|
+
from google.protobuf import reflection as _reflection
|
|
9
|
+
from google.protobuf import symbol_database as _symbol_database
|
|
10
|
+
# @@protoc_insertion_point(imports)
|
|
11
|
+
|
|
12
|
+
_sym_db = _symbol_database.Default()
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
from google.protobuf import descriptor_pb2 as google_dot_protobuf_dot_descriptor__pb2
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x18\x62yteplus/base/base.proto\x12\x19\x42yteplus.Base.Models.Base\x1a google/protobuf/descriptor.proto\"\xa0\x01\n\x10ResponseMetadata\x12\x11\n\tRequestId\x18\x01 \x01(\t\x12\x0e\n\x06\x41\x63tion\x18\x02 \x01(\t\x12\x0f\n\x07Version\x18\x03 \x01(\t\x12\x0f\n\x07Service\x18\x04 \x01(\t\x12\x0e\n\x06Region\x18\x05 \x01(\t\x12\x37\n\x05\x45rror\x18\x06 \x01(\x0b\x32(.Byteplus.Base.Models.Base.ResponseError\".\n\rResponseError\x12\x0c\n\x04\x43ode\x18\x01 \x01(\t\x12\x0f\n\x07Message\x18\x02 \x01(\tB\xcc\x01\n$com.byteplus.service.base.model.baseB\x04\x42\x61seP\x01ZDgithub.com/byteplus-sdk/byteplus-sdk-golang/service/base/models/base\xa0\x01\x01\xd8\x01\x01\xc2\x02\x00\xca\x02!Byteplus\\Service\\Base\\Models\\Base\xe2\x02(Byteplus\\Service\\Base\\Models\\GPBMetadatab\x06proto3')
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
_RESPONSEMETADATA = DESCRIPTOR.message_types_by_name['ResponseMetadata']
|
|
23
|
+
_RESPONSEERROR = DESCRIPTOR.message_types_by_name['ResponseError']
|
|
24
|
+
ResponseMetadata = _reflection.GeneratedProtocolMessageType('ResponseMetadata', (_message.Message,), {
|
|
25
|
+
'DESCRIPTOR' : _RESPONSEMETADATA,
|
|
26
|
+
'__module__' : 'byteplus.base.base_pb2'
|
|
27
|
+
# @@protoc_insertion_point(class_scope:Byteplus.Base.Models.Base.ResponseMetadata)
|
|
28
|
+
})
|
|
29
|
+
_sym_db.RegisterMessage(ResponseMetadata)
|
|
30
|
+
|
|
31
|
+
ResponseError = _reflection.GeneratedProtocolMessageType('ResponseError', (_message.Message,), {
|
|
32
|
+
'DESCRIPTOR' : _RESPONSEERROR,
|
|
33
|
+
'__module__' : 'byteplus.base.base_pb2'
|
|
34
|
+
# @@protoc_insertion_point(class_scope:Byteplus.Base.Models.Base.ResponseError)
|
|
35
|
+
})
|
|
36
|
+
_sym_db.RegisterMessage(ResponseError)
|
|
37
|
+
|
|
38
|
+
if _descriptor._USE_C_DESCRIPTORS == False:
|
|
39
|
+
|
|
40
|
+
DESCRIPTOR._options = None
|
|
41
|
+
DESCRIPTOR._serialized_options = b'\n$com.byteplus.service.base.model.baseB\004BaseP\001ZDgithub.com/byteplus-sdk/byteplus-sdk-golang/service/base/models/base\240\001\001\330\001\001\302\002\000\312\002!Byteplus\\Service\\Base\\Models\\Base\342\002(Byteplus\\Service\\Base\\Models\\GPBMetadata'
|
|
42
|
+
_RESPONSEMETADATA._serialized_start=90
|
|
43
|
+
_RESPONSEMETADATA._serialized_end=250
|
|
44
|
+
_RESPONSEERROR._serialized_start=252
|
|
45
|
+
_RESPONSEERROR._serialized_end=298
|
|
46
|
+
# @@protoc_insertion_point(module_scope)
|
|
File without changes
|