Class: AliyunSDKCore::ROAClient
- Inherits:
-
Object
- Object
- AliyunSDKCore::ROAClient
- Defined in:
- lib/aliyunsdkcore/roa_client.rb
Defined Under Namespace
Classes: ACSError
Instance Attribute Summary collapse
-
#access_key_id ⇒ Object
Returns the value of attribute access_key_id.
-
#access_key_secret ⇒ Object
Returns the value of attribute access_key_secret.
-
#api_version ⇒ Object
Returns the value of attribute api_version.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#hostname ⇒ Object
Returns the value of attribute hostname.
-
#opts ⇒ Object
Returns the value of attribute opts.
-
#security_token ⇒ Object
Returns the value of attribute security_token.
Instance Method Summary collapse
- #connection(adapter = Faraday.default_adapter) ⇒ Object
- #default_headers ⇒ Object
- #delete(uri: '', headers: {}, params: {}, options: {}) ⇒ Object
- #get(uri: '', headers: {}, params: {}, options: {}) ⇒ Object
-
#initialize(config) ⇒ ROAClient
constructor
A new instance of ROAClient.
- #post(uri: '', headers: {}, params: {}, body: {}, options: {}) ⇒ Object
- #put(uri: '', headers: {}, params: {}, body: {}, options: {}) ⇒ Object
- #request(method:, uri:, params: {}, body: {}, headers: {}, options: {}) ⇒ Object
Constructor Details
#initialize(config) ⇒ ROAClient
Returns a new instance of ROAClient.
20 21 22 23 24 25 26 27 28 |
# File 'lib/aliyunsdkcore/roa_client.rb', line 20 def initialize(config) validate config self.endpoint = config[:endpoint] self.api_version = config[:api_version] self.access_key_id = config[:access_key_id] self.access_key_secret = config[:access_key_secret] self.security_token = config[:security_token] end |
Instance Attribute Details
#access_key_id ⇒ Object
Returns the value of attribute access_key_id.
17 18 19 |
# File 'lib/aliyunsdkcore/roa_client.rb', line 17 def access_key_id @access_key_id end |
#access_key_secret ⇒ Object
Returns the value of attribute access_key_secret.
17 18 19 |
# File 'lib/aliyunsdkcore/roa_client.rb', line 17 def access_key_secret @access_key_secret end |
#api_version ⇒ Object
Returns the value of attribute api_version.
17 18 19 |
# File 'lib/aliyunsdkcore/roa_client.rb', line 17 def api_version @api_version end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
17 18 19 |
# File 'lib/aliyunsdkcore/roa_client.rb', line 17 def endpoint @endpoint end |
#hostname ⇒ Object
Returns the value of attribute hostname.
17 18 19 |
# File 'lib/aliyunsdkcore/roa_client.rb', line 17 def hostname @hostname end |
#opts ⇒ Object
Returns the value of attribute opts.
17 18 19 |
# File 'lib/aliyunsdkcore/roa_client.rb', line 17 def opts @opts end |
#security_token ⇒ Object
Returns the value of attribute security_token.
17 18 19 |
# File 'lib/aliyunsdkcore/roa_client.rb', line 17 def security_token @security_token end |
Instance Method Details
#connection(adapter = Faraday.default_adapter) ⇒ Object
72 73 74 |
# File 'lib/aliyunsdkcore/roa_client.rb', line 72 def connection(adapter = Faraday.default_adapter) Faraday.new(:url => self.endpoint) { |faraday| faraday.adapter adapter } end |
#default_headers ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/aliyunsdkcore/roa_client.rb', line 92 def default_headers default_headers = { 'accept' => 'application/json', 'date' => Time.now.httpdate, 'host' => URI(self.endpoint).host, 'x-acs-signature-nonce' => SecureRandom.hex(16), 'x-acs-signature-method' => 'HMAC-SHA1', 'x-acs-signature-version' => '1.0', 'x-acs-version' => self.api_version, 'x-sdk-client' => "RUBY(#{RUBY_VERSION})", # FIXME: 如何获取Gem的名称和版本号 'user-agent' => DEFAULT_UA } if self.security_token default_headers.merge!( 'x-acs-accesskey-id' => self.access_key_id, 'x-acs-security-token' => self.security_token ) end default_headers end |
#delete(uri: '', headers: {}, params: {}, options: {}) ⇒ Object
88 89 90 |
# File 'lib/aliyunsdkcore/roa_client.rb', line 88 def delete(uri: '', headers: {}, params: {}, options: {}) request(method: :delete, uri: uri, params: params, body: {}, headers: headers, options: ) end |
#get(uri: '', headers: {}, params: {}, options: {}) ⇒ Object
76 77 78 |
# File 'lib/aliyunsdkcore/roa_client.rb', line 76 def get(uri: '', headers: {}, params: {}, options: {}) request(method: :get, uri: uri, params: params, body: {}, headers: headers, options: ) end |
#post(uri: '', headers: {}, params: {}, body: {}, options: {}) ⇒ Object
80 81 82 |
# File 'lib/aliyunsdkcore/roa_client.rb', line 80 def post(uri: '', headers: {}, params: {}, body: {}, options: {}) request(method: :post, uri: uri, params: params, body: body, headers: headers, options: ) end |
#put(uri: '', headers: {}, params: {}, body: {}, options: {}) ⇒ Object
84 85 86 |
# File 'lib/aliyunsdkcore/roa_client.rb', line 84 def put(uri: '', headers: {}, params: {}, body: {}, options: {}) request(method: :put, uri: uri, params: params, body: body, headers: headers, options: ) end |
#request(method:, uri:, params: {}, body: {}, headers: {}, options: {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/aliyunsdkcore/roa_client.rb', line 30 def request(method:, uri:, params: {}, body: {}, headers: {}, options: {}) # :"Content-Type" => "application/json" to "content-type" => "application/json" headers.deep_transform_keys! { |key| key.to_s.downcase } mix_headers = default_headers.merge(headers) response = connection.send(method.downcase) do |request| request.url uri, params if body if mix_headers['content-type'].start_with? 'application/json' request_body = body.to_json elsif mix_headers['content-type'].start_with? 'application/x-www-form-urlencoded' request_body = URI.encode_www_form(body) else request_body = body end mix_headers['content-md5'] = Digest::MD5.base64digest request_body mix_headers['content-length'] = request_body.length.to_s request.body = request_body end string2sign = string_to_sign(method, uri, mix_headers, params) mix_headers.merge!(authorization: (string2sign)) mix_headers.each { |key, value| request.headers[key] = value } end return response if .has_key? :raw_body response_content_type = response.headers['Content-Type'] || '' if response_content_type.start_with?('application/json') if response.status >= 400 result = JSON.parse(response.body) raise StandardError, "code: #{response.status}, #{result['Message']} requestid: #{result['RequestId']}" end end if response_content_type.start_with?('text/xml') result = Hash.from_xml(response.body) raise ACSError, result['Error'] if result['Error'] end response end |