Class: Aliyun::Connector::RPCClient

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

Direct Known Subclasses

Dysms, Dyvms

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, verbose = false) ⇒ RPCClient

对象初始化属性



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/aliyun/connector/rpc_client.rb', line 15

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] || Aliyun.access_key_id
  self.access_key_secret = config[:access_key_secret] || Aliyun.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.



11
12
13
# File 'lib/aliyun/connector/rpc_client.rb', line 11

def access_key_id
  @access_key_id
end

#access_key_secretObject

Returns the value of attribute access_key_secret.



11
12
13
# File 'lib/aliyun/connector/rpc_client.rb', line 11

def access_key_secret
  @access_key_secret
end

#api_versionObject

Returns the value of attribute api_version.



11
12
13
# File 'lib/aliyun/connector/rpc_client.rb', line 11

def api_version
  @api_version
end

#codesObject

Returns the value of attribute codes.



11
12
13
# File 'lib/aliyun/connector/rpc_client.rb', line 11

def codes
  @codes
end

#endpointObject

Returns the value of attribute endpoint.



11
12
13
# File 'lib/aliyun/connector/rpc_client.rb', line 11

def endpoint
  @endpoint
end

#optsObject

Returns the value of attribute opts.



11
12
13
# File 'lib/aliyun/connector/rpc_client.rb', line 11

def opts
  @opts
end

#security_tokenObject

Returns the value of attribute security_token.



11
12
13
# File 'lib/aliyun/connector/rpc_client.rb', line 11

def security_token
  @security_token
end

#verboseObject

Returns the value of attribute verbose.



11
12
13
# File 'lib/aliyun/connector/rpc_client.rb', line 11

def verbose
  @verbose
end

Instance Method Details

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

通用请求接口



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/aliyun/connector/rpc_client.rb', line 33

def request(action:, params: {}, opts: {})
  opts           = self.opts.merge(opts)
  action         = action.upcase_first 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)}"
  secret         = "#{self.access_key_secret}&"
  signature      = Base64.encode64(OpenSSL::HMAC.digest("sha1", secret, string_to_sign)).strip
  normalized.push(["Signature", encode(signature)])

  # 转换为 query 样式
  querystring = canonicalize(normalized)

  # 特殊处理 POST
  uri = opts[:method] == "POST" ? "/" : "/?#{querystring}"

  # 初始化会话
  response = connection.send(method.downcase, uri) do |request|
    if opts[:method] == "POST"
      request.headers["Content-Type"] = "application/x-www-form-urlencoded"
      request.body                    = querystring
    end
    request.headers["User-Agent"] = DEFAULT_UA
  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