aliyun-rails 0.1.19 → 0.1.20

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cb272181c60129e57686d1086eb34e5b4cb20776926c600ca9865e8a8e7537d3
4
- data.tar.gz: 345f2c7bfc4135d42c4f8d9a0c4caab0218a5c419bde70fb4e13ae5f44414ac0
3
+ metadata.gz: 36e5418e787a9c2336ad30666d24c92e0442b561c1582013c58ce7d64d2011db
4
+ data.tar.gz: 1ca01ce359761a938426b45c210cf0d7ac840d7233c43e8ebf40880c4ce48191
5
5
  SHA512:
6
- metadata.gz: 62c17e6d850d702b250aefab7c0934990271a244ff97cfcc1e2f27870117f5ccbee62445e95fb0e3cec7207fedbc226986bd7e40ffd50da951e9bbdbad36ee46
7
- data.tar.gz: 234b8e75fb51905c926ac4204c6ee5259a9f854ad0f8efb6b2b482a0870abc51fae36ce5203d131c970175474ccc06e4e3dddb0480f03d27d2b0a5471c8e1ba2
6
+ metadata.gz: 59da25dd5e90cffe81986ba120b53e9314a82f226bc65c58ba015b24104d3fca76c804e65d129c135b7c356157a39c4679d06f9dd8b6010810e1579ef7d4f75e
7
+ data.tar.gz: 0a0b90b91aa272a7679ccc2db9ce14f4a279a681f150fefed50ffd4e81149a97dcfd1948d7decac0b762ff2f842a247f26aaaf2fb502db58423068c2c570c787
@@ -4,144 +4,145 @@ require "faraday"
4
4
  require "active_support/all"
5
5
 
6
6
  module Aliyun
7
+ module Connector
8
+ class RPCClient
7
9
 
8
- class RPCClient
10
+ attr_accessor :endpoint, :api_version, :access_key_id, :access_key_secret,
11
+ :security_token, :codes, :opts, :verbose
9
12
 
10
- attr_accessor :endpoint, :api_version, :access_key_id, :access_key_secret,
11
- :security_token, :codes, :opts, :verbose
13
+ # 对象初始化属性
14
+ def initialize(config, verbose = false)
12
15
 
13
- # 对象初始化属性
14
- def initialize(config, verbose = false)
16
+ validate config
15
17
 
16
- validate config
18
+ self.endpoint = config[:endpoint]
19
+ self.api_version = config[:api_version]
20
+ self.access_key_id = config[:access_key_id] || Aliyun.access_key_id
21
+ self.access_key_secret = config[:access_key_secret] || Aliyun.access_key_secret
22
+ self.security_token = config[:security_token]
23
+ self.opts = config[:opts] || {}
24
+ self.verbose = verbose.instance_of?(TrueClass) && verbose
17
25
 
18
- self.endpoint = config[:endpoint]
19
- self.api_version = config[:api_version]
20
- self.access_key_id = config[:access_key_id] || Aliyun.access_key_id
21
- self.access_key_secret = config[:access_key_secret] || Aliyun.access_key_secret
22
- self.security_token = config[:security_token]
23
- self.opts = config[:opts] || {}
24
- self.verbose = verbose.instance_of?(TrueClass) && verbose
26
+ self.codes = Set.new [200, "200", "OK", "Success"]
27
+ self.codes.merge config[:codes] if config[:codes]
28
+ end
25
29
 
26
- self.codes = Set.new [200, "200", "OK", "Success"]
27
- self.codes.merge config[:codes] if config[:codes]
28
- end
30
+ # 通用请求接口
31
+ def request(action:, params: {}, opts: {})
32
+ opts = self.opts.merge(opts)
33
+ action = action.upcase_first if opts[:format_action]
34
+ params = format_params(params) unless opts[:format_params]
35
+ defaults = default_params
36
+ params = { Action: action }.merge(defaults).merge(params)
37
+ method = (opts[:method] || "GET").upcase
38
+ normalized = normalize(params)
39
+ canonicalized = canonicalize(normalized)
40
+ string_to_sign = "#{method}&#{encode('/')}&#{encode(canonicalized)}"
41
+ secret = "#{self.access_key_secret}&"
42
+ signature = Base64.encode64(OpenSSL::HMAC.digest("sha1", secret, string_to_sign)).strip
43
+ normalized.push(["Signature", encode(signature)])
44
+
45
+ # 转换为 query 样式
46
+ querystring = canonicalize(normalized)
47
+
48
+ # 特殊处理 POST
49
+ uri = opts[:method] == "POST" ? "/" : "/?#{querystring}"
50
+
51
+ # 初始化会话
52
+ response = connection.send(method.downcase, uri) do |request|
53
+ if opts[:method] == "POST"
54
+ request.headers["Content-Type"] = "application/x-www-form-urlencoded"
55
+ request.body = querystring
56
+ end
57
+ request.headers["User-Agent"] = DEFAULT_UA
58
+ end
29
59
 
30
- # 通用请求接口
31
- def request(action:, params: {}, opts: {})
32
- opts = self.opts.merge(opts)
33
- action = action.upcase_first if opts[:format_action]
34
- params = format_params(params) unless opts[:format_params]
35
- defaults = default_params
36
- params = { Action: action }.merge(defaults).merge(params)
37
- method = (opts[:method] || "GET").upcase
38
- normalized = normalize(params)
39
- canonicalized = canonicalize(normalized)
40
- string_to_sign = "#{method}&#{encode('/')}&#{encode(canonicalized)}"
41
- secret = "#{self.access_key_secret}&"
42
- signature = Base64.encode64(OpenSSL::HMAC.digest("sha1", secret, string_to_sign)).strip
43
- normalized.push(["Signature", encode(signature)])
44
-
45
- # 转换为 query 样式
46
- querystring = canonicalize(normalized)
47
-
48
- # 特殊处理 POST
49
- uri = opts[:method] == "POST" ? "/" : "/?#{querystring}"
50
-
51
- # 初始化会话
52
- response = connection.send(method.downcase, uri) do |request|
53
- if opts[:method] == "POST"
54
- request.headers["Content-Type"] = "application/x-www-form-urlencoded"
55
- request.body = querystring
60
+ # 解析接口响应
61
+ response_body = JSON.parse(response.body)
62
+ if response_body["Code"] && !response_body["Code"].to_s.empty? && !self.codes.include?(response_body["Code"])
63
+ raise StandardError, "Code: #{response_body['Code']}, Message: #{response_body['Message']}, URL: #{uri}"
56
64
  end
57
- request.headers["User-Agent"] = DEFAULT_UA
58
- end
59
65
 
60
- # 解析接口响应
61
- response_body = JSON.parse(response.body)
62
- if response_body["Code"] && !response_body["Code"].to_s.empty? && !self.codes.include?(response_body["Code"])
63
- raise StandardError, "Code: #{response_body['Code']}, Message: #{response_body['Message']}, URL: #{uri}"
66
+ response_body
64
67
  end
65
68
 
66
- response_body
67
- end
68
-
69
- private
70
- def connection(adapter = Faraday.default_adapter)
71
- Faraday.new(url: self.endpoint) { |f| f.adapter adapter }
72
- end
69
+ private
70
+ def connection(adapter = Faraday.default_adapter)
71
+ Faraday.new(url: self.endpoint) { |f| f.adapter adapter }
72
+ end
73
73
 
74
- # 设置缺省参数
75
- def default_params
76
- default_params = {
77
- "Format" => "JSON",
78
- "SignatureMethod" => "HMAC-SHA1",
79
- "SignatureNonce" => SecureRandom.hex(16),
80
- "SignatureVersion" => "1.0",
81
- "Timestamp" => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
82
- "AccessKeyId" => self.access_key_id,
83
- "Version" => self.api_version,
84
- }
85
- default_params["SecurityToken"] = self.security_token if self.security_token
86
- default_params
87
- end
74
+ # 设置缺省参数
75
+ def default_params
76
+ default_params = {
77
+ "Format" => "JSON",
78
+ "SignatureMethod" => "HMAC-SHA1",
79
+ "SignatureNonce" => SecureRandom.hex(16),
80
+ "SignatureVersion" => "1.0",
81
+ "Timestamp" => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
82
+ "AccessKeyId" => self.access_key_id,
83
+ "Version" => self.api_version,
84
+ }
85
+ default_params["SecurityToken"] = self.security_token if self.security_token
86
+ default_params
87
+ end
88
88
 
89
- # 消息签名需要
90
- def encode(string)
91
- ERB::Util.url_encode(string)
92
- end
89
+ # 消息签名需要
90
+ def encode(string)
91
+ ERB::Util.url_encode(string)
92
+ end
93
93
 
94
- # 转换 HASH key 样式
95
- def format_params(param_hash)
96
- param_hash.keys.each { |key| param_hash[(key.to_s.upcase_first).to_sym] = param_hash.delete key }
97
- param_hash
98
- end
94
+ # 转换 HASH key 样式
95
+ def format_params(param_hash)
96
+ param_hash.keys.each { |key| param_hash[(key.to_s.upcase_first).to_sym] = param_hash.delete key }
97
+ param_hash
98
+ end
99
99
 
100
- def replace_repeat_list(target, key, repeat)
101
- repeat.each_with_index do |item, index|
102
- if item && item.instance_of?(Hash)
103
- item.each_key { |k| target["#{key}.#{index.next}.#{k}"] = item[k] }
104
- else
105
- target["#{key}.#{index.next}"] = item
100
+ def replace_repeat_list(target, key, repeat)
101
+ repeat.each_with_index do |item, index|
102
+ if item && item.instance_of?(Hash)
103
+ item.each_key { |k| target["#{key}.#{index.next}.#{k}"] = item[k] }
104
+ else
105
+ target["#{key}.#{index.next}"] = item
106
+ end
106
107
  end
108
+ target
107
109
  end
108
- target
109
- end
110
110
 
111
- def flat_params(params)
112
- target = {}
113
- params.each do |key, value|
114
- if value.instance_of?(Array)
115
- replace_repeat_list(target, key, value)
116
- else
117
- target[key.to_s] = value
111
+ def flat_params(params)
112
+ target = {}
113
+ params.each do |key, value|
114
+ if value.instance_of?(Array)
115
+ replace_repeat_list(target, key, value)
116
+ else
117
+ target[key.to_s] = value
118
+ end
118
119
  end
120
+ target
119
121
  end
120
- target
121
- end
122
-
123
- def normalize(params)
124
- flat_params(params).sort.to_h.map { |key, value| [encode(key), encode(value)] }
125
- end
126
-
127
- def canonicalize(normalized)
128
- normalized.map { |element| "#{element.first}=#{element.last}" }.join("&")
129
- end
130
122
 
131
- def validate(config = {})
132
- config.with_indifferent_access
133
- raise ArgumentError, 'must pass "config"' unless config
134
- raise ArgumentError, 'must pass "config[:endpoint]"' unless config[:endpoint]
135
- unless config[:endpoint].match?(/^http[s]?:/i)
136
- raise ArgumentError, '"config.endpoint" must starts with \'https://\' or \'http://\'.'
123
+ def normalize(params)
124
+ flat_params(params).sort.to_h.map { |key, value| [encode(key), encode(value)] }
137
125
  end
138
- raise ArgumentError, 'must pass "config[:api_version]"' unless config[:api_version]
139
- unless config[:access_key_id] || Aliyun.access_key_id
140
- raise ArgumentError, 'must pass "config[:access_key_id]" or define "Aliyun.access_key_id"'
126
+
127
+ def canonicalize(normalized)
128
+ normalized.map { |element| "#{element.first}=#{element.last}" }.join("&")
141
129
  end
142
- unless config[:access_key_secret] || Aliyun.access_key_secret
143
- raise ArgumentError, 'must pass "config[:access_key_secret]" or define "Aliyun.access_key_secret"'
130
+
131
+ def validate(config = {})
132
+ config.with_indifferent_access
133
+ raise ArgumentError, 'must pass "config"' unless config
134
+ raise ArgumentError, 'must pass "config[:endpoint]"' unless config[:endpoint]
135
+ unless config[:endpoint].match?(/^http[s]?:/i)
136
+ raise ArgumentError, '"config.endpoint" must starts with \'https://\' or \'http://\'.'
137
+ end
138
+ raise ArgumentError, 'must pass "config[:api_version]"' unless config[:api_version]
139
+ unless config[:access_key_id] || Aliyun.access_key_id
140
+ raise ArgumentError, 'must pass "config[:access_key_id]" or define "Aliyun.access_key_id"'
141
+ end
142
+ unless config[:access_key_secret] || Aliyun.access_key_secret
143
+ raise ArgumentError, 'must pass "config[:access_key_secret]" or define "Aliyun.access_key_secret"'
144
+ end
144
145
  end
145
- end
146
+ end
146
147
  end
147
148
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Aliyun
4
- VERSION = "0.1.19"
4
+ VERSION = "0.1.20"
5
5
  DEFAULT_UA = "AlibabaCloud (#{Gem::Platform.local.os}; " +
6
6
  "#{Gem::Platform.local.cpu}) Ruby/#{RUBY_VERSION} Core/#{VERSION}"
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aliyun-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.19
4
+ version: 0.1.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - WENWU.YAN
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-03 00:00:00.000000000 Z
11
+ date: 2022-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport