baidubce-sdk 0.9.0 → 0.9.1
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.
- checksums.yaml +4 -4
- data/.gitignore +2 -12
- data/lib/baidubce/bce_client_configuration.rb +4 -1
- data/lib/baidubce/bce_constants.rb +1 -0
- data/lib/baidubce/http/base_http_client.rb +12 -3
- data/lib/baidubce/http/http_constants.rb +1 -0
- data/lib/baidubce/retry_policy.rb +7 -0
- data/lib/baidubce/services/bos/bos_client.rb +35 -5
- data/lib/baidubce/utils/utils.rb +14 -2
- data/lib/baidubce/version.rb +1 -1
- data/samples/baidubce/bos_sample.rb +1 -1
- metadata +2 -4
- data/.rspec +0 -2
- data/.travis.yml +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 37ee3d088dfe300d2c4bc59ede9c3295460349d7
|
4
|
+
data.tar.gz: 9c7fcf154a0f0a51b6b4a731e1ab2193e8247f04
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7eaa2b1a43bab2af6d5355cb24bf37d2ada15a10db184364a65f1c6ddab3f5a58ef1dddae0851c08f400f9c163b5c39a33058a0805c7582f9e865cb11490eba7
|
7
|
+
data.tar.gz: 70c5c1e51449b504d1a8a05b21dd1f5e2fb3f7831ce2713f48a83f4633814cb056d74384328229534bd8d0563933a3dda94f2a79d984218fca1b68df0d89a10d
|
data/.gitignore
CHANGED
@@ -25,7 +25,8 @@ module Baidubce
|
|
25
25
|
|
26
26
|
class BceClientConfiguration
|
27
27
|
attr_accessor :credentials, :endpoint, :protocol, :region, :open_timeout_in_millis,
|
28
|
-
:read_timeout_in_millis, :send_buf_size, :recv_buf_size, :retry_policy
|
28
|
+
:read_timeout_in_millis, :send_buf_size, :recv_buf_size, :retry_policy,
|
29
|
+
:cname_enabled, :backup_endpoint
|
29
30
|
|
30
31
|
def initialize(credentials,
|
31
32
|
endpoint,
|
@@ -41,6 +42,8 @@ module Baidubce
|
|
41
42
|
@send_buf_size = options['send_buf_size'] || DEFAULT_SEND_BUF_SIZE
|
42
43
|
@recv_buf_size = options['recv_buf_size'] || DEFAULT_RECV_BUF_SIZE
|
43
44
|
@retry_policy = options['retry_policy'] || BackOffRetryPolicy.new
|
45
|
+
@cname_enabled = options['cname_enabled'] || false
|
46
|
+
@backup_endpoint = options['backup_endpoint']
|
44
47
|
end
|
45
48
|
|
46
49
|
end
|
@@ -32,7 +32,8 @@ module Baidubce
|
|
32
32
|
|
33
33
|
include Log
|
34
34
|
# Send request to BCE services.
|
35
|
-
def send_request(config, signer, http_method, path, params, headers, body,
|
35
|
+
def send_request(config, signer, http_method, path, params, headers, body,
|
36
|
+
save_path=nil, use_backup_endpoint=false, &block)
|
36
37
|
headers[USER_AGENT] = sprintf(
|
37
38
|
'bce-sdk-ruby/%s/%s/%s',
|
38
39
|
VERSION,
|
@@ -42,7 +43,7 @@ module Baidubce
|
|
42
43
|
|
43
44
|
should_get_new_date = headers.has_key?(BCE_DATE) ? false : true
|
44
45
|
|
45
|
-
url, headers[HOST] = Utils.parse_url_host(config)
|
46
|
+
url, headers[HOST] = Utils.parse_url_host(config, use_backup_endpoint)
|
46
47
|
url += Utils.url_encode_except_slash(path)
|
47
48
|
query_str = Utils.get_canonical_querystring(params, false)
|
48
49
|
url += "?#{query_str}" unless query_str.to_s.empty?
|
@@ -90,6 +91,13 @@ module Baidubce
|
|
90
91
|
logger.debug("Response headers: #{resp.headers.to_s}")
|
91
92
|
return resp.body, resp.headers
|
92
93
|
end
|
94
|
+
rescue SocketError => err
|
95
|
+
if config.retry_policy.should_retry(SOCKET_ERROR_CODE, retries_attempted)
|
96
|
+
delay_in_millis = config.retry_policy.get_delay_before_next_retry_in_millis(retries_attempted)
|
97
|
+
sleep(delay_in_millis / 1000.0)
|
98
|
+
else
|
99
|
+
raise BceClientException.new("SocketError: #{err}")
|
100
|
+
end
|
93
101
|
rescue BceHttpException, RestClient::ExceptionWithResponse => err
|
94
102
|
logger.debug("ExceptionWithResponse: #{err.http_code}, #{err.http_body}, #{err.http_headers}, #{err.message}")
|
95
103
|
if config.retry_policy.should_retry(err.http_code, retries_attempted)
|
@@ -117,7 +125,8 @@ module Baidubce
|
|
117
125
|
def set_content_length_header(headers, body, &block)
|
118
126
|
unless block_given?
|
119
127
|
if body.to_s.empty?
|
120
|
-
headers[CONTENT_LENGTH] = 0
|
128
|
+
# headers[CONTENT_LENGTH] = 0
|
129
|
+
headers.delete(CONTENT_LENGTH)
|
121
130
|
elsif body.instance_of?(String)
|
122
131
|
body = body.encode('UTF-8') if body.encoding.name != 'UTF-8'
|
123
132
|
headers[CONTENT_LENGTH] = body.bytesize
|
@@ -68,6 +68,12 @@ module Baidubce
|
|
68
68
|
logger.debug('Retry for request timeout.')
|
69
69
|
return true
|
70
70
|
end
|
71
|
+
# retry for SocketError
|
72
|
+
if http_code == SOCKET_ERROR_CODE
|
73
|
+
logger.debug('Retry for request SocketError.')
|
74
|
+
return true
|
75
|
+
end
|
76
|
+
|
71
77
|
if http_code >= 500 && http_code != 501
|
72
78
|
logger.debug('Retry for server error.')
|
73
79
|
return true
|
@@ -82,6 +88,7 @@ module Baidubce
|
|
82
88
|
return @max_delay_in_millis if delay_in_millis > @max_delay_in_millis
|
83
89
|
return delay_in_millis
|
84
90
|
end
|
91
|
+
|
85
92
|
end
|
86
93
|
|
87
94
|
end
|
@@ -251,8 +251,10 @@ module Baidubce
|
|
251
251
|
|
252
252
|
path = Utils.append_uri("/", key)
|
253
253
|
url, headers[HOST] = Utils.parse_url_host(@config)
|
254
|
-
|
255
|
-
|
254
|
+
unless @config.cname_enabled || Utils.is_cname_like_host(@config.endpoint)
|
255
|
+
url.insert(url.index('/') + 2, bucket_name + '.')
|
256
|
+
headers[HOST] = bucket_name + '.' + headers[HOST]
|
257
|
+
end
|
256
258
|
params[AUTHORIZATION.downcase] = @signer.sign(@config.credentials,
|
257
259
|
GET,
|
258
260
|
path,
|
@@ -421,10 +423,38 @@ module Baidubce
|
|
421
423
|
end
|
422
424
|
|
423
425
|
def send_request(http_method, bucket_name="", params={}, key="", headers={}, body="", save_path=nil, return_body=false, &block)
|
424
|
-
|
425
|
-
|
426
|
+
if @config.cname_enabled || Utils.is_cname_like_host(@config.endpoint)
|
427
|
+
path = Utils.append_uri("/", key)
|
428
|
+
else
|
429
|
+
path = Utils.append_uri("/", bucket_name, key)
|
430
|
+
end
|
431
|
+
|
432
|
+
begin
|
433
|
+
resp_body, resp_headers = @http_client.send_request(@config, @signer, http_method, path, params,
|
434
|
+
headers, body, save_path, false, &block)
|
435
|
+
rescue BceClientException, BceServerException => err
|
436
|
+
# retry backup endpoint
|
437
|
+
if err.is_a?(BceClientException) || (err.is_a?(BceServerException) && (err.status_code.nil? ||
|
438
|
+
err.status_code == 408 || (err.status_code >= 500 && err.status_code != 501)))
|
439
|
+
unless @config.backup_endpoint.to_s.empty?
|
440
|
+
if @config.cname_enabled || Utils.is_cname_like_host(@config.backup_endpoint)
|
441
|
+
path = Utils.append_uri("/", key)
|
442
|
+
else
|
443
|
+
path = Utils.append_uri("/", bucket_name, key)
|
444
|
+
end
|
445
|
+
|
446
|
+
resp_body, resp_headers = @http_client.send_request(@config, @signer, http_method, path, params,
|
447
|
+
headers, body, save_path, true, &block)
|
448
|
+
else
|
449
|
+
raise err
|
450
|
+
end
|
451
|
+
else
|
452
|
+
raise err
|
453
|
+
end
|
454
|
+
end
|
455
|
+
|
426
456
|
# Generate result from headers and body
|
427
|
-
Utils.generate_response(
|
457
|
+
Utils.generate_response(resp_headers, resp_body, return_body)
|
428
458
|
end
|
429
459
|
|
430
460
|
def get_range_header_dict(range)
|
data/lib/baidubce/utils/utils.rb
CHANGED
@@ -23,9 +23,14 @@ module Baidubce
|
|
23
23
|
|
24
24
|
class Utils
|
25
25
|
|
26
|
+
DEFAULT_CNAME_LIKE_LIST = [".cdn.bcebos.com"]
|
27
|
+
|
26
28
|
# parse protocol, host, port from endpoint in config.
|
27
|
-
def self.parse_url_host(config)
|
29
|
+
def self.parse_url_host(config, use_backup_endpoint=false)
|
28
30
|
endpoint = config.endpoint
|
31
|
+
if use_backup_endpoint
|
32
|
+
endpoint = config.backup_endpoint
|
33
|
+
end
|
29
34
|
unless endpoint.include?"://"
|
30
35
|
protocol = config.protocol.downcase
|
31
36
|
raise "Invalid protocol #{protocol}." if protocol != "http" && protocol != 'https'
|
@@ -91,7 +96,7 @@ module Baidubce
|
|
91
96
|
|
92
97
|
def self.generate_response(headers, body, return_body)
|
93
98
|
return body if return_body
|
94
|
-
return generate_headers(headers) if body.empty?
|
99
|
+
return generate_headers(headers) if body.to_s.empty?
|
95
100
|
ret = JSON.parse(body)
|
96
101
|
return ret
|
97
102
|
rescue JSON::ParserError
|
@@ -119,6 +124,13 @@ module Baidubce
|
|
119
124
|
resp_headers
|
120
125
|
end
|
121
126
|
|
127
|
+
def self.is_cname_like_host(host)
|
128
|
+
DEFAULT_CNAME_LIKE_LIST.each do |suffix|
|
129
|
+
return true if host.to_s.downcase.end_with?(suffix)
|
130
|
+
end
|
131
|
+
return false
|
132
|
+
end
|
133
|
+
|
122
134
|
end
|
123
135
|
|
124
136
|
end
|
data/lib/baidubce/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: baidubce-sdk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- xiaoyong
|
8
8
|
autorequire:
|
9
9
|
bindir: lib/baidubce
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-04-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -120,8 +120,6 @@ extensions: []
|
|
120
120
|
extra_rdoc_files: []
|
121
121
|
files:
|
122
122
|
- ".gitignore"
|
123
|
-
- ".rspec"
|
124
|
-
- ".travis.yml"
|
125
123
|
- CODE_OF_CONDUCT.md
|
126
124
|
- Gemfile
|
127
125
|
- LICENSE
|
data/.rspec
DELETED