Class: AliyunSDKCore::RPCClient

Inherits:
Object
  • Object
show all
Defined in:
lib/aliyunsdkcore/rpc_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, verbose = false) ⇒ RPCClient

Returns a new instance of RPCClient.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/aliyunsdkcore/rpc_client.rb', line 12

def initialize(config, verbose = false)

  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]
  self.opts              = config[:opts] || {}
  self.verbose           = verbose.instance_of?(TrueClass) && verbose
  self.codes             = Set.new [200, '200', 'OK', 'Success']
  self.codes.merge config[:codes] if config[:codes]
end

Instance Attribute Details

#access_key_idObject

Returns the value of attribute access_key_id.



10
11
12
# File 'lib/aliyunsdkcore/rpc_client.rb', line 10

def access_key_id
  @access_key_id
end

#access_key_secretObject

Returns the value of attribute access_key_secret.



10
11
12
# File 'lib/aliyunsdkcore/rpc_client.rb', line 10

def access_key_secret
  @access_key_secret
end

#api_versionObject

Returns the value of attribute api_version.



10
11
12
# File 'lib/aliyunsdkcore/rpc_client.rb', line 10

def api_version
  @api_version
end

#codesObject

Returns the value of attribute codes.



10
11
12
# File 'lib/aliyunsdkcore/rpc_client.rb', line 10

def codes
  @codes
end

#endpointObject

Returns the value of attribute endpoint.



10
11
12
# File 'lib/aliyunsdkcore/rpc_client.rb', line 10

def endpoint
  @endpoint
end

#optsObject

Returns the value of attribute opts.



10
11
12
# File 'lib/aliyunsdkcore/rpc_client.rb', line 10

def opts
  @opts
end

#security_tokenObject

Returns the value of attribute security_token.



10
11
12
# File 'lib/aliyunsdkcore/rpc_client.rb', line 10

def security_token
  @security_token
end

#verboseObject

Returns the value of attribute verbose.



10
11
12
# File 'lib/aliyunsdkcore/rpc_client.rb', line 10

def verbose
  @verbose
end

Instance Method Details

#request(action:, params: {}, opts: {}) ⇒ Object



27
28
29
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
# File 'lib/aliyunsdkcore/rpc_client.rb', line 27

def request(action:, params: {}, opts: {})
  opts           = self.opts.merge(opts)
  action         = upcase_first(action) if opts[:format_action]
  params         = format_params(params) unless opts[:format_params]
  defaults       = default_params
  params         = { Action: action }.merge(defaults).merge(params)
  method         = (opts[:method] || 'GET').upcase
  normalized     = normalize(params)
  canonicalized  = canonicalize(normalized)
  string_to_sign = "#{method}&#{encode('/')}&#{encode(canonicalized)}"
  key            = self.access_key_secret + '&'
  signature      = Base64.encode64(OpenSSL::HMAC.digest('sha1', key, string_to_sign)).strip
  normalized.push(['Signature', encode(signature)])

  querystring = canonicalize(normalized)

  uri           = opts[:method] == 'POST' ? '/' : "/?#{querystring}"

  response      = connection.send(method.downcase, uri) do |request|
    request.headers['User-Agent'] = DEFAULT_UA
    if opts[:method] == 'POST'
      request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
      request.body                    = querystring
    end
  end

  response_body = JSON.parse(response.body)
  if response_body['Code'] && !response_body['Code'].to_s.empty? && !self.codes.include?(response_body['Code'])
    raise StandardError, "Code: #{response_body['Code']}, Message: #{response_body['Message']}, URL: #{uri}"
  end

  response_body
end