bce-python-sdk 0.9.50__py3-none-any.whl → 0.9.52__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.
- baidubce/__init__.py +1 -1
- baidubce/auth/bce_v1_signer.py +52 -3
- baidubce/services/aihc/__init__.py +18 -0
- baidubce/services/aihc/aihc_client.py +171 -0
- baidubce/services/aihc/aihc_handler.py +145 -0
- baidubce/services/aihc/aihc_model.py +37 -0
- baidubce/services/aihc/base/__init__.py +15 -0
- baidubce/services/aihc/base/aihc_base_client.py +174 -0
- baidubce/services/aihc/base/aihc_common.py +48 -0
- baidubce/services/aihc/modules/__init__.py +15 -0
- baidubce/services/aihc/modules/dataset/__init__.py +18 -0
- baidubce/services/aihc/modules/dataset/dataset_client.py +386 -0
- baidubce/services/aihc/modules/dataset/dataset_model.py +75 -0
- baidubce/services/aihc/modules/dev_instance/__init__.py +18 -0
- baidubce/services/aihc/modules/dev_instance/dev_instance_client.py +390 -0
- baidubce/services/aihc/modules/job/__init__.py +18 -0
- baidubce/services/aihc/modules/job/job_client.py +590 -0
- baidubce/services/aihc/modules/job/job_model.py +254 -0
- baidubce/services/aihc/modules/model/__init__.py +18 -0
- baidubce/services/aihc/modules/model/model_client.py +294 -0
- baidubce/services/aihc/modules/model/model_model.py +54 -0
- baidubce/services/aihc/modules/service/__init__.py +18 -0
- baidubce/services/aihc/modules/service/service_client.py +562 -0
- baidubce/services/aihc/modules/service/service_model.py +512 -0
- baidubce/services/vpc/vpc_client.py +1 -1
- {bce_python_sdk-0.9.50.dist-info → bce_python_sdk-0.9.52.dist-info}/METADATA +1 -1
- {bce_python_sdk-0.9.50.dist-info → bce_python_sdk-0.9.52.dist-info}/RECORD +29 -7
- {bce_python_sdk-0.9.50.dist-info → bce_python_sdk-0.9.52.dist-info}/WHEEL +0 -0
- {bce_python_sdk-0.9.50.dist-info → bce_python_sdk-0.9.52.dist-info}/top_level.txt +0 -0
baidubce/__init__.py
CHANGED
baidubce/auth/bce_v1_signer.py
CHANGED
|
@@ -14,8 +14,6 @@
|
|
|
14
14
|
This module provides authentication functions for bce services.
|
|
15
15
|
"""
|
|
16
16
|
from __future__ import absolute_import
|
|
17
|
-
from builtins import str
|
|
18
|
-
from builtins import bytes
|
|
19
17
|
import hashlib
|
|
20
18
|
import hmac
|
|
21
19
|
import logging
|
|
@@ -74,7 +72,7 @@ def sign(credentials, http_method, path, headers, params,
|
|
|
74
72
|
|
|
75
73
|
canonical_headers = _get_canonical_headers(headers, headers_to_sign)
|
|
76
74
|
string_to_sign = (b'\n').join([
|
|
77
|
-
http_method, canonical_uri,
|
|
75
|
+
http_method, canonical_uri,
|
|
78
76
|
canonical_querystring, canonical_headers
|
|
79
77
|
])
|
|
80
78
|
sign_result = hmac.new(compat.convert_to_bytes(sign_key), string_to_sign, hashlib.sha256).hexdigest()
|
|
@@ -90,3 +88,54 @@ def sign(credentials, http_method, path, headers, params,
|
|
|
90
88
|
(sign_key, len(string_to_sign), string_to_sign))
|
|
91
89
|
_logger.debug('result=%s' % result)
|
|
92
90
|
return result
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def resource_pool_sign(credentials, http_method, path, headers, params,
|
|
94
|
+
timestamp=0, expiration_in_seconds=1800, headers_to_sign=None):
|
|
95
|
+
"""
|
|
96
|
+
资源池接口签名适配方法,与普通接口签名不同的是,资源池接口需要额外处理host头
|
|
97
|
+
"""
|
|
98
|
+
|
|
99
|
+
_logger.debug('Sign params: %s %s %s %s %d %d %s' % (
|
|
100
|
+
http_method, path, headers, params, timestamp, expiration_in_seconds, headers_to_sign))
|
|
101
|
+
|
|
102
|
+
headers = headers or {}
|
|
103
|
+
params = params or {}
|
|
104
|
+
|
|
105
|
+
sign_key_info = b'bce-auth-v1/%s/%s/%d' % (
|
|
106
|
+
credentials.access_key_id,
|
|
107
|
+
utils.get_canonical_time(timestamp),
|
|
108
|
+
expiration_in_seconds)
|
|
109
|
+
sign_key = hmac.new(
|
|
110
|
+
credentials.secret_access_key,
|
|
111
|
+
sign_key_info,
|
|
112
|
+
hashlib.sha256).hexdigest()
|
|
113
|
+
|
|
114
|
+
canonical_uri = path
|
|
115
|
+
canonical_querystring = utils.get_canonical_querystring(params, True)
|
|
116
|
+
|
|
117
|
+
# 从headers中提取host
|
|
118
|
+
host_header = None
|
|
119
|
+
for key, value in headers.items():
|
|
120
|
+
if key.lower() == b'host':
|
|
121
|
+
host_header = utils.convert_to_standard_string(value).strip()
|
|
122
|
+
break
|
|
123
|
+
|
|
124
|
+
canonical_headers = b'host:' + utils.normalize_string(host_header)
|
|
125
|
+
string_to_sign = (b'\n').join([
|
|
126
|
+
http_method, canonical_uri,
|
|
127
|
+
canonical_querystring, canonical_headers
|
|
128
|
+
])
|
|
129
|
+
sign_result = hmac.new(compat.convert_to_bytes(sign_key), string_to_sign, hashlib.sha256).hexdigest()
|
|
130
|
+
# convert to bytes
|
|
131
|
+
sign_result = compat.convert_to_bytes(sign_result)
|
|
132
|
+
|
|
133
|
+
if headers_to_sign:
|
|
134
|
+
result = b'%s/%s/%s' % (sign_key_info, (b';').join(headers_to_sign), sign_result)
|
|
135
|
+
else:
|
|
136
|
+
result = b'%s/host/%s' % (sign_key_info, sign_result)
|
|
137
|
+
|
|
138
|
+
_logger.debug('sign_key=[%s] sign_string=[%d bytes][ %s ]' %
|
|
139
|
+
(sign_key, len(string_to_sign), string_to_sign))
|
|
140
|
+
_logger.debug('result=%s' % result)
|
|
141
|
+
return result
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Copyright 2014 Baidu, Inc.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
|
|
4
|
+
# except in compliance with the License. You may obtain a copy of the License at
|
|
5
|
+
#
|
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
#
|
|
8
|
+
# Unless required by applicable law or agreed to in writing, software distributed under the
|
|
9
|
+
# License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|
10
|
+
# either express or implied. See the License for the specific language governing permissions
|
|
11
|
+
# and limitations under the License.
|
|
12
|
+
|
|
13
|
+
"""
|
|
14
|
+
AIHC V2 service module.
|
|
15
|
+
"""
|
|
16
|
+
from baidubce.services.aihc.aihc_client import AihcClient
|
|
17
|
+
|
|
18
|
+
__all__ = ['AihcClient']
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
|
|
4
|
+
# except in compliance with the License. You may obtain a copy of the License at
|
|
5
|
+
#
|
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
#
|
|
8
|
+
# Unless required by applicable law or agreed to in writing, software distributed under the
|
|
9
|
+
# License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|
10
|
+
# either express or implied. See the License for the specific language governing permissions
|
|
11
|
+
# and limitations under the License.
|
|
12
|
+
|
|
13
|
+
"""
|
|
14
|
+
This module provides a client class for AIHC V2.
|
|
15
|
+
"""
|
|
16
|
+
import inspect
|
|
17
|
+
from functools import wraps
|
|
18
|
+
from typing import Any, Dict, Optional, Union, List, get_type_hints
|
|
19
|
+
|
|
20
|
+
from baidubce.services.aihc.modules.job.job_client import JobClient
|
|
21
|
+
from baidubce.services.aihc.modules.dataset.dataset_client import DatasetClient
|
|
22
|
+
from baidubce.services.aihc.modules.model.model_client import ModelClient
|
|
23
|
+
from baidubce.services.aihc.modules.service.service_client import ServiceClient
|
|
24
|
+
from baidubce.services.aihc.modules.dev_instance.dev_instance_client import DevInstanceClient
|
|
25
|
+
from baidubce.services.aihc.modules.resource_pool.resource_pool_client import ResourcePoolClient
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def create_typed_proxy_method(target_client, method_name):
|
|
29
|
+
"""
|
|
30
|
+
创建带有完整类型信息的代理方法
|
|
31
|
+
|
|
32
|
+
这个函数会:
|
|
33
|
+
1. 获取原始方法的完整签名和类型注解
|
|
34
|
+
2. 创建一个新的代理方法,保持所有类型信息
|
|
35
|
+
3. 确保IDE能够正确识别类型提示
|
|
36
|
+
"""
|
|
37
|
+
original_method = getattr(target_client, method_name)
|
|
38
|
+
|
|
39
|
+
# 获取原始方法的完整信息
|
|
40
|
+
sig = inspect.signature(original_method)
|
|
41
|
+
|
|
42
|
+
# 创建代理方法
|
|
43
|
+
@wraps(original_method)
|
|
44
|
+
def proxy_method(*args, **kwargs):
|
|
45
|
+
"""
|
|
46
|
+
代理方法,用于调用原始目标方法并保持类型信息
|
|
47
|
+
"""
|
|
48
|
+
return original_method(*args, **kwargs)
|
|
49
|
+
|
|
50
|
+
# 设置完整的类型信息
|
|
51
|
+
proxy_method.__signature__ = sig
|
|
52
|
+
proxy_method.__annotations__ = original_method.__annotations__
|
|
53
|
+
|
|
54
|
+
# 确保IDE能够识别类型信息
|
|
55
|
+
if hasattr(original_method, '__module__'):
|
|
56
|
+
proxy_method.__module__ = original_method.__module__
|
|
57
|
+
if hasattr(original_method, '__qualname__'):
|
|
58
|
+
proxy_method.__qualname__ = original_method.__qualname__
|
|
59
|
+
if hasattr(original_method, '__name__'):
|
|
60
|
+
proxy_method.__name__ = original_method.__name__
|
|
61
|
+
|
|
62
|
+
# 使用 typing.get_type_hints 确保类型信息正确
|
|
63
|
+
try:
|
|
64
|
+
type_hints = get_type_hints(original_method)
|
|
65
|
+
proxy_method.__annotations__ = type_hints
|
|
66
|
+
except Exception:
|
|
67
|
+
# 如果获取类型提示失败,使用原始注解
|
|
68
|
+
proxy_method.__annotations__ = original_method.__annotations__
|
|
69
|
+
|
|
70
|
+
return proxy_method
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class AihcClient:
|
|
74
|
+
"""
|
|
75
|
+
AIHC主客户端,适配V2版本OpenAPI
|
|
76
|
+
|
|
77
|
+
该客户端提供了对AIHC各个模块功能的统一访问接口,
|
|
78
|
+
包括任务管理、数据集管理、模型管理、在线服务和开发机管理等功能。
|
|
79
|
+
|
|
80
|
+
使用示例:
|
|
81
|
+
>>> from baidubce.services.aihc.aihc_client import AihcClient
|
|
82
|
+
>>> client = AihcClient(config)
|
|
83
|
+
>>> jobs = client.DescribeJobs(resourcePoolId="xxx")
|
|
84
|
+
"""
|
|
85
|
+
|
|
86
|
+
def __init__(self, config=None):
|
|
87
|
+
"""
|
|
88
|
+
初始化AIHC V2客户端
|
|
89
|
+
|
|
90
|
+
Args:
|
|
91
|
+
config: 配置对象
|
|
92
|
+
baidubce.bce_client_configuration.BceClientConfiguration实例
|
|
93
|
+
"""
|
|
94
|
+
self.job = JobClient(config)
|
|
95
|
+
self.dataset = DatasetClient(config)
|
|
96
|
+
self.model = ModelClient(config)
|
|
97
|
+
self.service = ServiceClient(config)
|
|
98
|
+
self.dev_instance = DevInstanceClient(config)
|
|
99
|
+
self.resource_pool = ResourcePoolClient(config)
|
|
100
|
+
|
|
101
|
+
# 动态创建代理方法
|
|
102
|
+
self._setup_proxy_methods()
|
|
103
|
+
|
|
104
|
+
def _setup_proxy_methods(self):
|
|
105
|
+
"""
|
|
106
|
+
设置代理方法
|
|
107
|
+
|
|
108
|
+
为各个子模块的方法创建代理,使主客户端可以直接调用子模块的方法
|
|
109
|
+
"""
|
|
110
|
+
# 任务相关接口
|
|
111
|
+
job_methods = [
|
|
112
|
+
'DescribeJobs', 'DescribeJob', 'DeleteJob', 'ModifyJob',
|
|
113
|
+
'DescribeJobEvents', 'DescribeJobLogs', 'DescribeJobPodEvents',
|
|
114
|
+
'StopJob', 'DescribeJobNodes', 'DescribeJobWebterminal', 'CreateJob',
|
|
115
|
+
'DescribeJobMetrics'
|
|
116
|
+
]
|
|
117
|
+
|
|
118
|
+
# 数据集相关接口
|
|
119
|
+
dataset_methods = [
|
|
120
|
+
'DescribeDatasets', 'DescribeDataset', 'ModifyDataset', 'DeleteDataset',
|
|
121
|
+
'CreateDataset', 'DescribeDatasetVersions', 'DescribeDatasetVersion',
|
|
122
|
+
'DeleteDatasetVersion', 'CreateDatasetVersion'
|
|
123
|
+
]
|
|
124
|
+
|
|
125
|
+
# 模型相关接口
|
|
126
|
+
model_methods = [
|
|
127
|
+
'DescribeModels', 'CreateModel', 'DeleteModel', 'ModifyModel',
|
|
128
|
+
'DescribeModel', 'DescribeModelVersions', 'DescribeModelVersion',
|
|
129
|
+
'CreateModelVersion', 'DeleteModelVersion'
|
|
130
|
+
]
|
|
131
|
+
|
|
132
|
+
# 在线服务相关接口
|
|
133
|
+
service_methods = [
|
|
134
|
+
'DescribeServices', 'DescribeService', 'DescribeServiceStatus',
|
|
135
|
+
'CreateService', 'DeleteService', 'ModifyService', 'ModifyServiceReplicas', 'UpgradeService',
|
|
136
|
+
'DescribeServicePods', 'DeleteServicePod', 'DisableServicePod', 'ModifyServiceNetConfig',
|
|
137
|
+
'DescribeServicePodGroups', 'DescribeServiceChangelogs', 'DescribeServiceChangelog'
|
|
138
|
+
]
|
|
139
|
+
|
|
140
|
+
# 开发机相关接口
|
|
141
|
+
dev_instance_methods = [
|
|
142
|
+
'DescribeDevInstances', 'DescribeDevInstance', 'StartDevInstance', 'StopDevInstance',
|
|
143
|
+
'CreateDevInstance', 'DeleteDevInstance', 'ModifyDevInstance',
|
|
144
|
+
'DescribeDevInstanceEvents', 'CreateDevInstanceImagePackJob',
|
|
145
|
+
'DescribeDevInstanceImagePackJob', 'TimedStopDevInstance'
|
|
146
|
+
]
|
|
147
|
+
|
|
148
|
+
# 资源池相关接口
|
|
149
|
+
resource_pool_methods = [
|
|
150
|
+
'DescribeResourcePools', 'DescribeResourcePool', 'DescribeResourcePoolOverview',
|
|
151
|
+
'DescribeResourcePoolConfiguration', 'DescribeQueues', 'DescribeQueue'
|
|
152
|
+
]
|
|
153
|
+
|
|
154
|
+
# 为每个方法创建代理
|
|
155
|
+
for method_name in job_methods:
|
|
156
|
+
setattr(self, method_name, create_typed_proxy_method(self.job, method_name))
|
|
157
|
+
|
|
158
|
+
for method_name in dataset_methods:
|
|
159
|
+
setattr(self, method_name, create_typed_proxy_method(self.dataset, method_name))
|
|
160
|
+
|
|
161
|
+
for method_name in model_methods:
|
|
162
|
+
setattr(self, method_name, create_typed_proxy_method(self.model, method_name))
|
|
163
|
+
|
|
164
|
+
for method_name in service_methods:
|
|
165
|
+
setattr(self, method_name, create_typed_proxy_method(self.service, method_name))
|
|
166
|
+
|
|
167
|
+
for method_name in dev_instance_methods:
|
|
168
|
+
setattr(self, method_name, create_typed_proxy_method(self.dev_instance, method_name))
|
|
169
|
+
|
|
170
|
+
for method_name in resource_pool_methods:
|
|
171
|
+
setattr(self, method_name, create_typed_proxy_method(self.resource_pool, method_name))
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# Copyright 2014 Baidu, Inc.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
|
|
4
|
+
# except in compliance with the License. You may obtain a copy of the License at
|
|
5
|
+
#
|
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
#
|
|
8
|
+
# Unless required by applicable law or agreed to in writing, software distributed under the
|
|
9
|
+
# License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|
10
|
+
# either express or implied. See the License for the specific language governing permissions
|
|
11
|
+
# and limitations under the License.
|
|
12
|
+
|
|
13
|
+
"""
|
|
14
|
+
This module provides general http handler functions for processing http responses from AIHC V2 services.
|
|
15
|
+
|
|
16
|
+
This module contains utility functions for parsing HTTP responses from AIHC (AI Hosting Cloud) services,
|
|
17
|
+
including JSON parsing, error handling, and response object conversion. The functions are designed
|
|
18
|
+
to work with the BCE (Baidu Cloud Engine) SDK framework.
|
|
19
|
+
|
|
20
|
+
Functions:
|
|
21
|
+
parse_json: Parse JSON response and convert to Python object
|
|
22
|
+
dict_to_python_object: Convert dictionary to Python object with Expando
|
|
23
|
+
parse_json_list: Parse JSON list response
|
|
24
|
+
parse_error: Handle error responses and raise appropriate exceptions
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
import http.client
|
|
28
|
+
import json
|
|
29
|
+
from baidubce import compat, utils
|
|
30
|
+
from baidubce.utils import Expando
|
|
31
|
+
from baidubce.exception import BceClientError
|
|
32
|
+
from baidubce.exception import BceServerError
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def parse_json(http_response, response):
|
|
36
|
+
"""
|
|
37
|
+
Parse JSON response and convert to Python object.
|
|
38
|
+
|
|
39
|
+
If the response body is not empty, convert it to a Python object and update
|
|
40
|
+
the response object's attributes. The http_response is always closed if no error occurs.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
http_response: The http_response object returned by HTTPConnection.getresponse()
|
|
44
|
+
response: General response object which will be returned to the caller
|
|
45
|
+
|
|
46
|
+
Returns:
|
|
47
|
+
bool: Always returns True
|
|
48
|
+
|
|
49
|
+
Note:
|
|
50
|
+
This function removes the 'metadata' key from the response if it exists.
|
|
51
|
+
"""
|
|
52
|
+
body = http_response.read()
|
|
53
|
+
if body:
|
|
54
|
+
response.__dict__.update(json.loads(
|
|
55
|
+
body, object_hook=dict_to_python_object).__dict__)
|
|
56
|
+
|
|
57
|
+
# 移除metadata key(如果存在)
|
|
58
|
+
if 'metadata' in response.__dict__:
|
|
59
|
+
del response.__dict__['metadata']
|
|
60
|
+
http_response.close()
|
|
61
|
+
return True
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def dict_to_python_object(d):
|
|
65
|
+
"""
|
|
66
|
+
Convert dictionary to Python object with Expando.
|
|
67
|
+
|
|
68
|
+
Args:
|
|
69
|
+
d: Dictionary to convert
|
|
70
|
+
|
|
71
|
+
Returns:
|
|
72
|
+
Expando: Python object with dictionary attributes
|
|
73
|
+
"""
|
|
74
|
+
attr = {}
|
|
75
|
+
for k, v in list(d.items()):
|
|
76
|
+
k = str(k)
|
|
77
|
+
attr[k] = v
|
|
78
|
+
return Expando(attr)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def parse_json_list(http_response, response):
|
|
82
|
+
"""
|
|
83
|
+
Parse JSON list response and convert to Python object.
|
|
84
|
+
|
|
85
|
+
If the body is not empty, convert it to a Python object and set as the value of
|
|
86
|
+
response.result. The http_response is always closed if no error occurs.
|
|
87
|
+
|
|
88
|
+
Args:
|
|
89
|
+
http_response: The http_response object returned by HTTPConnection.getresponse()
|
|
90
|
+
response: General response object which will be returned to the caller
|
|
91
|
+
|
|
92
|
+
Returns:
|
|
93
|
+
bool: Always returns True
|
|
94
|
+
"""
|
|
95
|
+
body = http_response.read()
|
|
96
|
+
if body:
|
|
97
|
+
body = compat.convert_to_string(body)
|
|
98
|
+
response.__dict__["result"] = json.loads(body, object_hook=utils.dict_to_python_object)
|
|
99
|
+
response.__dict__["raw_data"] = body
|
|
100
|
+
http_response.close()
|
|
101
|
+
return True
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def parse_error(http_response, response):
|
|
105
|
+
"""
|
|
106
|
+
Handle error responses and raise appropriate exceptions.
|
|
107
|
+
|
|
108
|
+
If the HTTP status code is not 2xx, parse the error response and raise
|
|
109
|
+
appropriate BCE exceptions. The http_response is always closed.
|
|
110
|
+
|
|
111
|
+
Args:
|
|
112
|
+
http_response: The http_response object returned by HTTPConnection.getresponse()
|
|
113
|
+
response: General response object which will be returned to the caller
|
|
114
|
+
|
|
115
|
+
Returns:
|
|
116
|
+
bool: False if HTTP status code is 2xx
|
|
117
|
+
|
|
118
|
+
Raises:
|
|
119
|
+
BceClientError: If HTTP status code is 1xx (not handled)
|
|
120
|
+
BceServerError: If HTTP status code is not 2xx (error response)
|
|
121
|
+
"""
|
|
122
|
+
if http_response.status // 100 == http.client.OK // 100:
|
|
123
|
+
return False
|
|
124
|
+
if http_response.status // 100 == http.client.CONTINUE // 100:
|
|
125
|
+
raise BceClientError(b'Can not handle 1xx http status code')
|
|
126
|
+
body = http_response.read()
|
|
127
|
+
if not body:
|
|
128
|
+
bse = BceServerError(http_response.reason, request_id=response.metadata.bce_request_id)
|
|
129
|
+
bse.status_code = http_response.status
|
|
130
|
+
raise bse
|
|
131
|
+
|
|
132
|
+
error_dict = json.loads(compat.convert_to_string(body))
|
|
133
|
+
message = str(error_dict)
|
|
134
|
+
if 'message' in error_dict and error_dict['message'] is not None:
|
|
135
|
+
message = error_dict['message']
|
|
136
|
+
code = "Exception"
|
|
137
|
+
if 'code' in error_dict and error_dict['code'] is not None:
|
|
138
|
+
code = error_dict['code']
|
|
139
|
+
request_id = response.metadata.bce_request_id
|
|
140
|
+
if 'request_id' in error_dict and error_dict['request_id'] is not None:
|
|
141
|
+
request_id = error_dict['request_id']
|
|
142
|
+
|
|
143
|
+
bse = BceServerError(message, code=code, request_id=request_id)
|
|
144
|
+
bse.status_code = http_response.status
|
|
145
|
+
raise bse
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# Copyright (c) 2011 X.commerce, a business unit of eBay Inc.
|
|
2
|
+
# Copyright 2010 United States Government as represented by the
|
|
3
|
+
# Administrator of the National Aeronautics and Space Administration.
|
|
4
|
+
# Copyright 2011 Piston Cloud Computing, Inc.
|
|
5
|
+
# All Rights Reserved.
|
|
6
|
+
#
|
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
8
|
+
# not use this file except in compliance with the License. You may obtain
|
|
9
|
+
# a copy of the License at
|
|
10
|
+
#
|
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
#
|
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
15
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
16
|
+
# License for the specific language governing permissions and limitations
|
|
17
|
+
# under the License.
|
|
18
|
+
"""
|
|
19
|
+
This module provides common models for AIHC-SDK.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class DemoModel(object):
|
|
24
|
+
"""
|
|
25
|
+
This class defines the configuration for a demo.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def __init__(self, demo_name, demo_config):
|
|
29
|
+
"""
|
|
30
|
+
初始化演示模型
|
|
31
|
+
|
|
32
|
+
Args:
|
|
33
|
+
demo_name: 演示名称
|
|
34
|
+
demo_config: 演示配置
|
|
35
|
+
"""
|
|
36
|
+
self.demo_name = demo_name
|
|
37
|
+
self.demo_config = demo_config
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
|
|
4
|
+
# except in compliance with the License. You may obtain a copy of the License at
|
|
5
|
+
#
|
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
#
|
|
8
|
+
# Unless required by applicable law or agreed to in writing, software distributed under the
|
|
9
|
+
# License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|
10
|
+
# either express or implied. See the License for the specific language governing permissions
|
|
11
|
+
# and limitations under the License.
|
|
12
|
+
|
|
13
|
+
"""
|
|
14
|
+
AIHC base module.
|
|
15
|
+
"""
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
|
|
4
|
+
# except in compliance with the License. You may obtain a copy of the License at
|
|
5
|
+
#
|
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
#
|
|
8
|
+
# Unless required by applicable law or agreed to in writing, software distributed under the
|
|
9
|
+
# License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|
10
|
+
# either express or implied. See the License for the specific language governing permissions
|
|
11
|
+
# and limitations under the License.
|
|
12
|
+
|
|
13
|
+
"""
|
|
14
|
+
AIHC base client module.
|
|
15
|
+
"""
|
|
16
|
+
import copy
|
|
17
|
+
|
|
18
|
+
from baidubce import bce_base_client
|
|
19
|
+
from baidubce.auth import bce_v1_signer
|
|
20
|
+
from baidubce.http import handler, bce_http_client
|
|
21
|
+
from baidubce.services.aihc import aihc_handler
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class AIHCBaseClient(bce_base_client.BceBaseClient):
|
|
25
|
+
"""
|
|
26
|
+
AIHC基础客户端类,提供公共方法
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
version = b'v2'
|
|
30
|
+
|
|
31
|
+
def __init__(self, config=None):
|
|
32
|
+
"""
|
|
33
|
+
初始化AIHC基础客户端
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
config: 配置对象,baidubce.bce_client_configuration.BceClientConfiguration实例
|
|
37
|
+
"""
|
|
38
|
+
bce_base_client.BceBaseClient.__init__(self, config)
|
|
39
|
+
|
|
40
|
+
def _merge_config(self, config=None):
|
|
41
|
+
"""
|
|
42
|
+
合并配置对象
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
config: 要合并的配置对象,如果为None则返回当前配置
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
baidubce.bce_client_configuration.BceClientConfiguration: 合并后的配置对象
|
|
49
|
+
"""
|
|
50
|
+
if config is None:
|
|
51
|
+
return self.config
|
|
52
|
+
else:
|
|
53
|
+
new_config = copy.copy(self.config)
|
|
54
|
+
new_config.merge_non_none_values(config)
|
|
55
|
+
return new_config
|
|
56
|
+
|
|
57
|
+
def _send_request(self, http_method, path,
|
|
58
|
+
body=None, headers=None, params=None,
|
|
59
|
+
config=None, body_parser=None):
|
|
60
|
+
"""
|
|
61
|
+
发送HTTP请求
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
http_method: HTTP方法
|
|
65
|
+
path: 请求路径
|
|
66
|
+
body: 请求体(可选)
|
|
67
|
+
headers: 请求头(可选)
|
|
68
|
+
params: 请求参数(可选)
|
|
69
|
+
config: 配置对象(可选)
|
|
70
|
+
body_parser: 响应体解析器(可选)
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
baidubce.bce_response.BceResponse: 响应对象
|
|
74
|
+
"""
|
|
75
|
+
config = self._merge_config(config)
|
|
76
|
+
if body_parser is None:
|
|
77
|
+
body_parser = aihc_handler.parse_json
|
|
78
|
+
|
|
79
|
+
if headers is None:
|
|
80
|
+
headers = {
|
|
81
|
+
b'version': AIHCBaseClient.version,
|
|
82
|
+
b'Content-Type': b'application/json'
|
|
83
|
+
}
|
|
84
|
+
else:
|
|
85
|
+
headers[b'version'] = AIHCBaseClient.version
|
|
86
|
+
headers[b'Content-Type'] = b'application/json'
|
|
87
|
+
|
|
88
|
+
return bce_http_client.send_request(
|
|
89
|
+
config, bce_v1_signer.sign, [handler.parse_error, body_parser],
|
|
90
|
+
http_method, path, body, headers, params)
|
|
91
|
+
|
|
92
|
+
def _send_job_request(
|
|
93
|
+
self,
|
|
94
|
+
http_method,
|
|
95
|
+
path,
|
|
96
|
+
body=None,
|
|
97
|
+
headers=None,
|
|
98
|
+
params=None,
|
|
99
|
+
config=None,
|
|
100
|
+
body_parser=None
|
|
101
|
+
):
|
|
102
|
+
"""
|
|
103
|
+
发送任务相关HTTP请求
|
|
104
|
+
|
|
105
|
+
Args:
|
|
106
|
+
http_method: HTTP方法
|
|
107
|
+
path: 请求路径
|
|
108
|
+
body: 请求体(可选)
|
|
109
|
+
headers: 请求头(可选)
|
|
110
|
+
params: 请求参数(可选)
|
|
111
|
+
config: 配置对象(可选)
|
|
112
|
+
body_parser: 响应体解析器(可选)
|
|
113
|
+
|
|
114
|
+
Returns:
|
|
115
|
+
baidubce.bce_response.BceResponse: 响应对象
|
|
116
|
+
"""
|
|
117
|
+
config = self._merge_config(config)
|
|
118
|
+
if body_parser is None:
|
|
119
|
+
body_parser = aihc_handler.parse_json
|
|
120
|
+
|
|
121
|
+
if headers is None:
|
|
122
|
+
headers = {
|
|
123
|
+
b'X-API-Version': AIHCBaseClient.version,
|
|
124
|
+
b'Content-Type': b'application/json'
|
|
125
|
+
}
|
|
126
|
+
else:
|
|
127
|
+
headers[b'X-API-Version'] = AIHCBaseClient.version
|
|
128
|
+
headers[b'Content-Type'] = b'application/json'
|
|
129
|
+
|
|
130
|
+
return bce_http_client.send_request(
|
|
131
|
+
config, bce_v1_signer.sign, [handler.parse_error, body_parser],
|
|
132
|
+
http_method, path, body, headers, params)
|
|
133
|
+
|
|
134
|
+
def _send_resource_pool_request(
|
|
135
|
+
self,
|
|
136
|
+
http_method,
|
|
137
|
+
path,
|
|
138
|
+
body=None,
|
|
139
|
+
headers=None,
|
|
140
|
+
params=None,
|
|
141
|
+
config=None,
|
|
142
|
+
body_parser=None
|
|
143
|
+
):
|
|
144
|
+
"""
|
|
145
|
+
适配资源池接口,发送请求时使用bce_v1_signer.resource_pool_sign方法签名
|
|
146
|
+
|
|
147
|
+
Args:
|
|
148
|
+
http_method: HTTP方法
|
|
149
|
+
path: 请求路径
|
|
150
|
+
body: 请求体(可选)
|
|
151
|
+
headers: 请求头(可选)
|
|
152
|
+
params: 请求参数(可选)
|
|
153
|
+
config: 配置对象(可选)
|
|
154
|
+
body_parser: 响应体解析器(可选)
|
|
155
|
+
|
|
156
|
+
Returns:
|
|
157
|
+
baidubce.bce_response.BceResponse: 响应对象
|
|
158
|
+
"""
|
|
159
|
+
config = self._merge_config(config)
|
|
160
|
+
if body_parser is None:
|
|
161
|
+
body_parser = aihc_handler.parse_json
|
|
162
|
+
|
|
163
|
+
if headers is None:
|
|
164
|
+
headers = {
|
|
165
|
+
b'version': AIHCBaseClient.version,
|
|
166
|
+
b'Content-Type': b'application/json'
|
|
167
|
+
}
|
|
168
|
+
else:
|
|
169
|
+
headers[b'version'] = AIHCBaseClient.version
|
|
170
|
+
headers[b'Content-Type'] = b'application/json'
|
|
171
|
+
|
|
172
|
+
return bce_http_client.send_request(
|
|
173
|
+
config, bce_v1_signer.resource_pool_sign, [handler.parse_error, body_parser],
|
|
174
|
+
http_method, path, body, headers, params)
|