baiducloud-python-sdk-core 0.0.1__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.
- baiducloud_python_sdk_core/__init__.py +24 -0
- baiducloud_python_sdk_core/abstract_model.py +33 -0
- baiducloud_python_sdk_core/auth/__init__.py +0 -0
- baiducloud_python_sdk_core/auth/bce_credentials.py +27 -0
- baiducloud_python_sdk_core/auth/bce_v1_signer.py +92 -0
- baiducloud_python_sdk_core/bce_base_client.py +90 -0
- baiducloud_python_sdk_core/bce_client_configuration.py +87 -0
- baiducloud_python_sdk_core/bce_response.py +61 -0
- baiducloud_python_sdk_core/compat.py +125 -0
- baiducloud_python_sdk_core/exception.py +52 -0
- baiducloud_python_sdk_core/http/__init__.py +0 -0
- baiducloud_python_sdk_core/http/bce_http_client.py +280 -0
- baiducloud_python_sdk_core/http/handler.py +76 -0
- baiducloud_python_sdk_core/http/http_content_types.py +18 -0
- baiducloud_python_sdk_core/http/http_headers.py +122 -0
- baiducloud_python_sdk_core/http/http_methods.py +25 -0
- baiducloud_python_sdk_core/protocol.py +25 -0
- baiducloud_python_sdk_core/region.py +27 -0
- baiducloud_python_sdk_core/retry/__init__.py +0 -0
- baiducloud_python_sdk_core/retry/retry_policy.py +133 -0
- baiducloud_python_sdk_core/utils.py +762 -0
- baiducloud_python_sdk_core-0.0.1.dist-info/METADATA +85 -0
- baiducloud_python_sdk_core-0.0.1.dist-info/RECORD +26 -0
- baiducloud_python_sdk_core-0.0.1.dist-info/WHEEL +5 -0
- baiducloud_python_sdk_core-0.0.1.dist-info/licenses/LICENSE +177 -0
- baiducloud_python_sdk_core-0.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,24 @@
|
|
|
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 defines some common string constants.
|
|
15
|
+
"""
|
|
16
|
+
from builtins import str
|
|
17
|
+
from builtins import bytes
|
|
18
|
+
from . import protocol
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
SDK_VERSION = b'0.0.1'
|
|
22
|
+
DEFAULT_SERVICE_DOMAIN = b'bcebos.com'
|
|
23
|
+
URL_PREFIX = b'/v1'
|
|
24
|
+
DEFAULT_ENCODING = 'UTF-8'
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
import json
|
|
13
|
+
|
|
14
|
+
"""
|
|
15
|
+
This module provides a general model class for baiducloud services.
|
|
16
|
+
"""
|
|
17
|
+
class AbstractModel(object):
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
"""
|
|
21
|
+
def __init__(self):
|
|
22
|
+
self._map = None
|
|
23
|
+
|
|
24
|
+
def to_dict(self):
|
|
25
|
+
return self._map
|
|
26
|
+
|
|
27
|
+
def from_dict(self, map=None):
|
|
28
|
+
pass
|
|
29
|
+
|
|
30
|
+
def to_json_string(self, *args, **kwargs):
|
|
31
|
+
if "ensure_ascii" not in kwargs:
|
|
32
|
+
kwargs["ensure_ascii"] = False
|
|
33
|
+
return json.dumps(self.to_dict(), *args, **kwargs)
|
|
File without changes
|
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
Provides access to the BCE credentials used for accessing BCE services: BCE access key ID and
|
|
15
|
+
secret access key.
|
|
16
|
+
These credentials are used to securely sign requests to BCE services.
|
|
17
|
+
"""
|
|
18
|
+
from baiducloud_python_sdk_core import compat
|
|
19
|
+
|
|
20
|
+
class BceCredentials(object):
|
|
21
|
+
"""
|
|
22
|
+
Provides access to the BCE credentials used for accessing BCE services:
|
|
23
|
+
BCE access key ID and secret access key.
|
|
24
|
+
"""
|
|
25
|
+
def __init__(self, access_key_id, secret_access_key):
|
|
26
|
+
self.access_key_id = compat.convert_to_bytes(access_key_id)
|
|
27
|
+
self.secret_access_key = compat.convert_to_bytes(secret_access_key)
|
|
@@ -0,0 +1,92 @@
|
|
|
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 authentication functions for bce services.
|
|
15
|
+
"""
|
|
16
|
+
from __future__ import absolute_import
|
|
17
|
+
from builtins import str
|
|
18
|
+
from builtins import bytes
|
|
19
|
+
import hashlib
|
|
20
|
+
import hmac
|
|
21
|
+
import logging
|
|
22
|
+
|
|
23
|
+
from baiducloud_python_sdk_core.http import http_headers
|
|
24
|
+
from baiducloud_python_sdk_core import utils
|
|
25
|
+
from baiducloud_python_sdk_core import compat
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
_logger = logging.getLogger(__name__)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _get_canonical_headers(headers, headers_to_sign=None):
|
|
32
|
+
headers = headers or {}
|
|
33
|
+
|
|
34
|
+
if headers_to_sign is None or len(headers_to_sign) == 0:
|
|
35
|
+
headers_to_sign = set([b"host",
|
|
36
|
+
b"content-md5",
|
|
37
|
+
b"content-length",
|
|
38
|
+
b"content-type"])
|
|
39
|
+
result = []
|
|
40
|
+
for k in headers:
|
|
41
|
+
k_lower = k.strip().lower()
|
|
42
|
+
value = utils.convert_to_standard_string(headers[k]).strip()
|
|
43
|
+
if k_lower.startswith(http_headers.BCE_PREFIX) \
|
|
44
|
+
or k_lower in headers_to_sign:
|
|
45
|
+
str_tmp = b"%s:%s" % (utils.normalize_string(k_lower), utils.normalize_string(value))
|
|
46
|
+
result.append(str_tmp)
|
|
47
|
+
result.sort()
|
|
48
|
+
return (b'\n').join(result)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def sign(credentials, http_method, path, headers, params,
|
|
52
|
+
timestamp=0, expiration_in_seconds=1800, headers_to_sign=None):
|
|
53
|
+
"""
|
|
54
|
+
Create the authorization
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
_logger.debug('Sign params: %s %s %s %s %d %d %s' % (
|
|
58
|
+
http_method, path, headers, params, timestamp, expiration_in_seconds, headers_to_sign))
|
|
59
|
+
|
|
60
|
+
headers = headers or {}
|
|
61
|
+
params = params or {}
|
|
62
|
+
|
|
63
|
+
sign_key_info = b'bce-auth-v1/%s/%s/%d' % (
|
|
64
|
+
credentials.access_key_id,
|
|
65
|
+
utils.get_canonical_time(timestamp),
|
|
66
|
+
expiration_in_seconds)
|
|
67
|
+
sign_key = hmac.new(
|
|
68
|
+
credentials.secret_access_key,
|
|
69
|
+
sign_key_info,
|
|
70
|
+
hashlib.sha256).hexdigest()
|
|
71
|
+
|
|
72
|
+
canonical_uri = path
|
|
73
|
+
canonical_querystring = utils.get_canonical_querystring(params, True)
|
|
74
|
+
|
|
75
|
+
canonical_headers = _get_canonical_headers(headers, headers_to_sign)
|
|
76
|
+
string_to_sign = (b'\n').join([
|
|
77
|
+
http_method, canonical_uri,
|
|
78
|
+
canonical_querystring, canonical_headers
|
|
79
|
+
])
|
|
80
|
+
sign_result = hmac.new(compat.convert_to_bytes(sign_key), string_to_sign, hashlib.sha256).hexdigest()
|
|
81
|
+
# convert to bytes
|
|
82
|
+
sign_result = compat.convert_to_bytes(sign_result)
|
|
83
|
+
|
|
84
|
+
if headers_to_sign:
|
|
85
|
+
result = b'%s/%s/%s' % (sign_key_info, (b';').join(headers_to_sign), sign_result)
|
|
86
|
+
else:
|
|
87
|
+
result = b'%s//%s' % (sign_key_info, sign_result)
|
|
88
|
+
|
|
89
|
+
_logger.debug('sign_key=[%s] sign_string=[%d bytes][ %s ]' %
|
|
90
|
+
(sign_key, len(string_to_sign), string_to_sign))
|
|
91
|
+
_logger.debug('result=%s' % result)
|
|
92
|
+
return result
|
|
@@ -0,0 +1,90 @@
|
|
|
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 provide base class for BCE service clients.
|
|
15
|
+
"""
|
|
16
|
+
from __future__ import absolute_import
|
|
17
|
+
import copy
|
|
18
|
+
from builtins import str, bytes
|
|
19
|
+
|
|
20
|
+
import baiducloud_python_sdk_core
|
|
21
|
+
from baiducloud_python_sdk_core import bce_client_configuration
|
|
22
|
+
from baiducloud_python_sdk_core.exception import BceClientError
|
|
23
|
+
from baiducloud_python_sdk_core.auth import bce_v1_signer
|
|
24
|
+
from baiducloud_python_sdk_core.http import handler
|
|
25
|
+
from baiducloud_python_sdk_core.http import bce_http_client
|
|
26
|
+
|
|
27
|
+
class BceBaseClient(object):
|
|
28
|
+
"""
|
|
29
|
+
TODO: add docstring
|
|
30
|
+
"""
|
|
31
|
+
def __init__(self, config, region_supported=True):
|
|
32
|
+
"""
|
|
33
|
+
:param config: the client configuration. The constructor makes a copy of this parameter so
|
|
34
|
+
that it is safe to change the configuration after then.
|
|
35
|
+
:type config: BceClientConfiguration
|
|
36
|
+
|
|
37
|
+
:param region_supported: true if this client supports region.
|
|
38
|
+
:type region_supported: bool
|
|
39
|
+
"""
|
|
40
|
+
self.service_id = self._compute_service_id()
|
|
41
|
+
self.region_supported = region_supported
|
|
42
|
+
# just for debug
|
|
43
|
+
self.config = copy.deepcopy(bce_client_configuration.DEFAULT_CONFIG)
|
|
44
|
+
if config is not None:
|
|
45
|
+
self.config.merge_non_none_values(config)
|
|
46
|
+
if self.config.endpoint is None:
|
|
47
|
+
self.config.endpoint = self._compute_endpoint()
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _compute_service_id(self):
|
|
51
|
+
return self.__module__.split('.')[2]
|
|
52
|
+
|
|
53
|
+
def _compute_endpoint(self):
|
|
54
|
+
if self.config.endpoint:
|
|
55
|
+
return self.config.endpoint
|
|
56
|
+
if self.region_supported:
|
|
57
|
+
return b'%s://%s.%s.%s' % (
|
|
58
|
+
self.config.protocol,
|
|
59
|
+
self.service_id,
|
|
60
|
+
self.config.region,
|
|
61
|
+
baiducloud_python_sdk_core.DEFAULT_SERVICE_DOMAIN)
|
|
62
|
+
else:
|
|
63
|
+
return b'%s://%s.%s' % (
|
|
64
|
+
self.config.protocol,
|
|
65
|
+
self.service_id,
|
|
66
|
+
baiducloud_python_sdk_core.DEFAULT_SERVICE_DOMAIN)
|
|
67
|
+
|
|
68
|
+
def _send_request(self, http_method, path, headers=None, params=None, body=None, model=None):
|
|
69
|
+
return bce_http_client.send_request(
|
|
70
|
+
self.config, bce_v1_signer.sign, [handler.parse_error, handler.parse_json],
|
|
71
|
+
http_method, path, body, headers, params, model=model)
|
|
72
|
+
|
|
73
|
+
def _get_config(self, apiDict, apiName):
|
|
74
|
+
return copy.deepcopy(apiDict[apiName])
|
|
75
|
+
|
|
76
|
+
def _add_header(self, apiConfig, key, value):
|
|
77
|
+
self._set_if_nonnull(apiConfig["headers"], key, value)
|
|
78
|
+
|
|
79
|
+
def _add_query(self, apiConfig, key, value):
|
|
80
|
+
# key-only query parameter's value is "" and can satisfy non-null
|
|
81
|
+
self._set_if_nonnull(apiConfig["queries"], key, value)
|
|
82
|
+
|
|
83
|
+
def _add_path_param(self, apiConfig, key, value):
|
|
84
|
+
if value is None:
|
|
85
|
+
raise BceClientError(b"Path param can't be none.")
|
|
86
|
+
apiConfig["path"] = apiConfig["path"].replace("[" + key + "]", value)
|
|
87
|
+
|
|
88
|
+
def _set_if_nonnull(self, params, param_name=None, value=None):
|
|
89
|
+
if value is not None:
|
|
90
|
+
params[param_name] = value
|
|
@@ -0,0 +1,87 @@
|
|
|
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 defines a common configuration class for BCE.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from future.utils import iteritems
|
|
18
|
+
from builtins import str
|
|
19
|
+
from builtins import bytes
|
|
20
|
+
import baiducloud_python_sdk_core.protocol
|
|
21
|
+
import baiducloud_python_sdk_core.region
|
|
22
|
+
from baiducloud_python_sdk_core.retry.retry_policy import BackOffRetryPolicy
|
|
23
|
+
from baiducloud_python_sdk_core import compat
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class BceClientConfiguration(object):
|
|
27
|
+
"""Configuration of Bce client."""
|
|
28
|
+
|
|
29
|
+
def __init__(self,
|
|
30
|
+
credentials=None,
|
|
31
|
+
endpoint=None,
|
|
32
|
+
protocol=None,
|
|
33
|
+
region=None,
|
|
34
|
+
connection_timeout_in_mills=None,
|
|
35
|
+
send_buf_size=None,
|
|
36
|
+
recv_buf_size=None,
|
|
37
|
+
retry_policy=None,
|
|
38
|
+
security_token=None,
|
|
39
|
+
cname_enabled=False,
|
|
40
|
+
backup_endpoint=None,
|
|
41
|
+
proxy_host=None,
|
|
42
|
+
proxy_port=None,
|
|
43
|
+
path_style_enable=False,
|
|
44
|
+
auto_follow_redirect=False,
|
|
45
|
+
):
|
|
46
|
+
self.credentials = credentials
|
|
47
|
+
self.endpoint = compat.convert_to_bytes(endpoint) if endpoint is not None else endpoint
|
|
48
|
+
self.protocol = protocol
|
|
49
|
+
self.region = region
|
|
50
|
+
self.connection_timeout_in_mills = connection_timeout_in_mills
|
|
51
|
+
self.send_buf_size = send_buf_size
|
|
52
|
+
self.recv_buf_size = recv_buf_size
|
|
53
|
+
self.proxy_host = proxy_host
|
|
54
|
+
self.proxy_port = proxy_port
|
|
55
|
+
if retry_policy is None:
|
|
56
|
+
self.retry_policy = BackOffRetryPolicy()
|
|
57
|
+
else:
|
|
58
|
+
self.retry_policy = retry_policy
|
|
59
|
+
self.security_token = security_token
|
|
60
|
+
self.cname_enabled = cname_enabled
|
|
61
|
+
self.path_style_enable = path_style_enable
|
|
62
|
+
self.backup_endpoint = compat.convert_to_bytes(backup_endpoint) if backup_endpoint is not None else backup_endpoint
|
|
63
|
+
self.auto_follow_redirect = auto_follow_redirect
|
|
64
|
+
|
|
65
|
+
def merge_non_none_values(self, other):
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
:param other:
|
|
69
|
+
:return:
|
|
70
|
+
"""
|
|
71
|
+
for k, v in iteritems(other.__dict__):
|
|
72
|
+
if v is not None:
|
|
73
|
+
self.__dict__[k] = v
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
DEFAULT_PROTOCOL = baiducloud_python_sdk_core.protocol.HTTP
|
|
77
|
+
DEFAULT_REGION = baiducloud_python_sdk_core.region.BEIJING
|
|
78
|
+
DEFAULT_CONNECTION_TIMEOUT_IN_MILLIS = 50 * 1000
|
|
79
|
+
DEFAULT_SEND_BUF_SIZE = 1024 * 1024
|
|
80
|
+
DEFAULT_RECV_BUF_SIZE = 10 * 1024 * 1024
|
|
81
|
+
DEFAULT_CONFIG = BceClientConfiguration(
|
|
82
|
+
protocol=DEFAULT_PROTOCOL,
|
|
83
|
+
region=DEFAULT_REGION,
|
|
84
|
+
connection_timeout_in_mills=DEFAULT_CONNECTION_TIMEOUT_IN_MILLIS,
|
|
85
|
+
send_buf_size=DEFAULT_SEND_BUF_SIZE,
|
|
86
|
+
recv_buf_size=DEFAULT_RECV_BUF_SIZE,
|
|
87
|
+
retry_policy=BackOffRetryPolicy())
|
|
@@ -0,0 +1,61 @@
|
|
|
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 a general response class for BCE services.
|
|
15
|
+
"""
|
|
16
|
+
from future.utils import iteritems
|
|
17
|
+
from builtins import str
|
|
18
|
+
from builtins import bytes
|
|
19
|
+
from baiducloud_python_sdk_core import utils
|
|
20
|
+
from baiducloud_python_sdk_core import compat
|
|
21
|
+
from baiducloud_python_sdk_core.http import http_headers
|
|
22
|
+
import json
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class BceResponse(object):
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
"""
|
|
29
|
+
def __init__(self):
|
|
30
|
+
self.metadata = utils.Expando()
|
|
31
|
+
self._map = None
|
|
32
|
+
|
|
33
|
+
def set_metadata_from_headers(self, headers):
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
:param headers:
|
|
37
|
+
:return:
|
|
38
|
+
"""
|
|
39
|
+
for k, v in iteritems(headers):
|
|
40
|
+
if k.startswith(compat.convert_to_string(http_headers.BCE_PREFIX)):
|
|
41
|
+
k = 'bce_' + k[len(compat.convert_to_string(http_headers.BCE_PREFIX)):]
|
|
42
|
+
k = utils.pythonize_name(k.replace('-', '_'))
|
|
43
|
+
if k.lower() == compat.convert_to_string(http_headers.ETAG.lower()):
|
|
44
|
+
v = v.strip('"')
|
|
45
|
+
setattr(self.metadata, k, v)
|
|
46
|
+
|
|
47
|
+
def to_dict(self):
|
|
48
|
+
return self._map
|
|
49
|
+
|
|
50
|
+
def from_dict(self, map=None):
|
|
51
|
+
pass
|
|
52
|
+
|
|
53
|
+
def to_json_string(self, *args, **kwargs):
|
|
54
|
+
if "ensure_ascii" not in kwargs:
|
|
55
|
+
kwargs["ensure_ascii"] = False
|
|
56
|
+
return json.dumps(self.to_dict(), *args, **kwargs)
|
|
57
|
+
|
|
58
|
+
def __getattr__(self, item):
|
|
59
|
+
if item.startswith('__'):
|
|
60
|
+
raise AttributeError
|
|
61
|
+
return None
|
|
@@ -0,0 +1,125 @@
|
|
|
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
|
+
This module provides string converting tools and compatibility on py2 vs py3
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
import functools
|
|
17
|
+
import itertools
|
|
18
|
+
import operator
|
|
19
|
+
import sys
|
|
20
|
+
import types
|
|
21
|
+
|
|
22
|
+
PY2 = sys.version_info[0]==2
|
|
23
|
+
PY3 = sys.version_info[0]==3
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
if PY3:
|
|
27
|
+
string_types = str,
|
|
28
|
+
integer_types = int,
|
|
29
|
+
class_types = type,
|
|
30
|
+
text_type = str
|
|
31
|
+
binary_type = bytes
|
|
32
|
+
|
|
33
|
+
def convert_to_bytes(idata):
|
|
34
|
+
"""
|
|
35
|
+
convert source type idata to bytes string
|
|
36
|
+
|
|
37
|
+
:type idata: any valid python type
|
|
38
|
+
:param idata: source data
|
|
39
|
+
:return : bytes string
|
|
40
|
+
"""
|
|
41
|
+
# unicode
|
|
42
|
+
if isinstance(idata, str):
|
|
43
|
+
return idata.encode(encoding='utf-8')
|
|
44
|
+
# Ascii
|
|
45
|
+
elif isinstance(idata, bytes):
|
|
46
|
+
return idata
|
|
47
|
+
# int,dict,list
|
|
48
|
+
else:
|
|
49
|
+
return str(idata).encode(encoding='utf-8')
|
|
50
|
+
|
|
51
|
+
def convert_to_string(idata):
|
|
52
|
+
"""
|
|
53
|
+
convert source data to str string on py3
|
|
54
|
+
|
|
55
|
+
:type idata:any valid python type
|
|
56
|
+
:param idata:source data
|
|
57
|
+
:return :uniocde string on py3
|
|
58
|
+
"""
|
|
59
|
+
return convert_to_unicode(idata)
|
|
60
|
+
|
|
61
|
+
def convert_to_unicode(idata):
|
|
62
|
+
"""
|
|
63
|
+
convert source type idata to unicode string
|
|
64
|
+
|
|
65
|
+
:type idata: any valid python type
|
|
66
|
+
:param idata: source data
|
|
67
|
+
:return : unicode string
|
|
68
|
+
"""
|
|
69
|
+
# Ascii
|
|
70
|
+
if isinstance(idata, bytes):
|
|
71
|
+
return idata.decode(encoding='utf-8')
|
|
72
|
+
# unicode
|
|
73
|
+
elif isinstance(idata, str):
|
|
74
|
+
return idata
|
|
75
|
+
# int,dict,list
|
|
76
|
+
else:
|
|
77
|
+
return str(idata)
|
|
78
|
+
|
|
79
|
+
else: # py2
|
|
80
|
+
string_types = basestring,
|
|
81
|
+
integer_types = (int, long)
|
|
82
|
+
class_types = (type, types.ClassType)
|
|
83
|
+
text_type = unicode
|
|
84
|
+
binary_type = str
|
|
85
|
+
|
|
86
|
+
def convert_to_bytes(idata):
|
|
87
|
+
"""
|
|
88
|
+
convert source type idata to bytes string
|
|
89
|
+
|
|
90
|
+
:type idata: any valid python type
|
|
91
|
+
:param idata: source data
|
|
92
|
+
:return : bytes string
|
|
93
|
+
"""
|
|
94
|
+
if isinstance(idata, unicode):
|
|
95
|
+
return idata.encode(encoding='utf-8')
|
|
96
|
+
elif isinstance(idata, str):
|
|
97
|
+
return idata
|
|
98
|
+
# int ,long, dict, list
|
|
99
|
+
else:
|
|
100
|
+
return str(idata)
|
|
101
|
+
|
|
102
|
+
def convert_to_string(idata):
|
|
103
|
+
"""
|
|
104
|
+
convert source data to str string on py2
|
|
105
|
+
|
|
106
|
+
:type idata:any valid python type
|
|
107
|
+
:param idata:source data
|
|
108
|
+
:return :bytes string on py2
|
|
109
|
+
"""
|
|
110
|
+
return convert_to_bytes(idata)
|
|
111
|
+
|
|
112
|
+
def convert_to_unicode(idata):
|
|
113
|
+
"""
|
|
114
|
+
convert source type idata to unicode string
|
|
115
|
+
|
|
116
|
+
:type idata: any valid python type
|
|
117
|
+
:param idata: source data
|
|
118
|
+
:return : unicode string
|
|
119
|
+
"""
|
|
120
|
+
if isinstance(idata, str): #Ascii
|
|
121
|
+
return idata.decode(encoding='utf-8')
|
|
122
|
+
elif isinstance(idata, unicode):
|
|
123
|
+
return idata
|
|
124
|
+
else:
|
|
125
|
+
return str(idata).decode(encoding='utf-8')
|
|
@@ -0,0 +1,52 @@
|
|
|
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 defines exceptions for BCE.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from baiducloud_python_sdk_core import utils
|
|
18
|
+
from builtins import str
|
|
19
|
+
from builtins import bytes
|
|
20
|
+
|
|
21
|
+
class BceError(Exception):
|
|
22
|
+
"""Base Error of BCE."""
|
|
23
|
+
def __init__(self, message):
|
|
24
|
+
Exception.__init__(self, message)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class BceClientError(BceError):
|
|
28
|
+
"""Error from BCE client."""
|
|
29
|
+
def __init__(self, message):
|
|
30
|
+
BceError.__init__(self, message)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class BceServerError(BceError):
|
|
34
|
+
"""Error from BCE servers."""
|
|
35
|
+
REQUEST_EXPIRED = b'RequestExpired'
|
|
36
|
+
|
|
37
|
+
"""Error threw when connect to server."""
|
|
38
|
+
def __init__(self, message, status_code=None, code=None, request_id=None):
|
|
39
|
+
BceError.__init__(self, message)
|
|
40
|
+
self.status_code = status_code
|
|
41
|
+
self.code = code
|
|
42
|
+
self.request_id = request_id
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class BceHttpClientError(BceError):
|
|
46
|
+
"""Exception threw after retry"""
|
|
47
|
+
def __init__(self, message, last_error, status_code=None, code=None, request_id=None):
|
|
48
|
+
BceError.__init__(self, message)
|
|
49
|
+
self.last_error = last_error
|
|
50
|
+
self.status_code = status_code
|
|
51
|
+
self.code = code
|
|
52
|
+
self.request_id = request_id
|
|
File without changes
|