class QcloudRuby::Client

Attributes

service_type[RW]

Public Class Methods

new(&block) click to toggle source
# File lib/qcloud_ruby/client.rb, line 13
def initialize(&block)
  self.instance_eval(&block)
end

Public Instance Methods

default_params(region, action) click to toggle source
# File lib/qcloud_ruby/client.rb, line 17
def default_params(region, action)
  {
    Region: region,
    Action: action,
    SecretId: secret_id,
    Timestamp: timestamp,
    Nonce: nonce,
    RequestClient: identity
  }
end
gen_data(method, region, action, other_params) click to toggle source
# File lib/qcloud_ruby/client.rb, line 28
def gen_data(method, region, action, other_params)
  params = default_params(region, action)
            .merge!(other_params)
            .sort
            .to_h

  query_str = URI.encode_www_form(params)

  params.merge!(Signature: sign(method, query_str))
end
host() click to toggle source
# File lib/qcloud_ruby/client.rb, line 65
def host
  "#{service_type}.#{base_host}"
end
identity() click to toggle source
# File lib/qcloud_ruby/client.rb, line 61
def identity
  "SDK_RUBY_#{::QcloudRuby::VERSION}"
end
nonce() click to toggle source
# File lib/qcloud_ruby/client.rb, line 57
def nonce
  (rand() * 65535).round
end
request(method: 'POST', region: nil, action: nil, **other_params) click to toggle source
# File lib/qcloud_ruby/client.rb, line 39
def request(method: 'POST', region: nil, action: nil, **other_params)
  data = gen_data(method, region, action, other_params)
  uri = URI(url_with_protocol)

  resp = if method == 'GET'
          uri.query = URI.encode_www_form(data)
          Net::HTTP.get_response(uri)
        else
          Net::HTTP.post_form(uri, data)
        end

  resp
end
sign(method, query) click to toggle source
# File lib/qcloud_ruby/client.rb, line 77
def sign(method, query)
  source = method + url + '?' + query

  Base64.encode64(OpenSSL::HMAC.digest(
    OpenSSL::Digest.new('sha1'),
    secret_key, source))
  .strip
end
timestamp() click to toggle source
# File lib/qcloud_ruby/client.rb, line 53
def timestamp
  Time.now.to_i
end
url() click to toggle source
# File lib/qcloud_ruby/client.rb, line 69
def url
  "#{host}#{path}"
end
url_with_protocol() click to toggle source
# File lib/qcloud_ruby/client.rb, line 73
def url_with_protocol
  "#{protocol}://#{url}"
end