Class: AliyunSDK::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/aliyun_sdk/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



9
10
11
# File 'lib/aliyun_sdk/client.rb', line 9

def initialize(config)
  @config = config
end

Instance Method Details

#request(api) ⇒ Object

返回统一的格式:

正常返回: { code: 0, data: “world”, message: “”}

异常返回: { code: 500, data: nil, message: “internal error” }



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/aliyun_sdk/client.rb', line 20

def request(api)
  public_params = {
    "Format" => "JSON",
    "Version" => api.request_version,
    "AccessKeyId" => @config.access_key_id,
    "SignatureMethod" => "HMAC-SHA1",
    "Timestamp" => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
    "SignatureVersion" => "1.0",
    "SignatureNonce" => SecureRandom.uuid,
    "Action" => api.request_action,
    "RegionId" => @config.region_id
  }

  method = api.request_method
  all_params = public_params.merge(api.request_params)
  signature = Signer.sign(all_params, @config.access_key_secret, method)
  all_params["Signature"] = signature

  endpoint = api.request_endpoint
  response = if method == "GET"
               RestClient.get("https://#{endpoint}", { params: all_params })
             else
               RestClient.post("https://#{endpoint}", all_params)
             end
  api.handle_response(JSON.parse(response.body))
rescue RestClient::ExceptionWithResponse => e
  { code: 500, data: nil, message: e.response }
end