class QingCloudServer::Client::Connector

Constants

APIAddress

Attributes

public_key[RW]
secret_key[RW]

Public Class Methods

init(public_key, secret_key) click to toggle source
# File lib/client/connector.rb, line 18
def self.init(public_key, secret_key)
  Connector.new(public_key, secret_key)
end
init_with_config() click to toggle source
# File lib/client/connector.rb, line 22
def self.init_with_config
  config_hash_file = Utility.read_config_file
  Connector.new(config_hash_file["public_key"], config_hash_file["private_key"])
end
new(public_key, secret_key) click to toggle source
# File lib/client/connector.rb, line 13
def initialize(public_key, secret_key)
  @public_key = public_key
  @secret_key = secret_key
end

Public Instance Methods

fetch_server_condition(action, params) click to toggle source
# File lib/client/connector.rb, line 27
def fetch_server_condition(action, params)

  params.update(
                :action => action,
                :time_stamp => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
                :access_key_id => self.public_key,
                :version => 1,
                :signature_method => "HmacSHA256",
                :signature_version => 1
                )

  request_body = params.sort.map do |k, v| # 这里的k是symbol,要先转化成为string,然后再使用
    if k.to_s.include?("_N")
      key_part = k.to_s.split("_")[0]
      v.map {|e| "#{CGI.escape(key_part)}.#{v.index(e) + 1}=#{CGI.escape(e)}"}
    else
      "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
    end
  end.join("&")

  puts "request_body #{request_body.inspect}"
  request_url = "GET\n/iaas/\n#{request_body}"

  sha_256_digest = OpenSSL::Digest.new("sha256")
  signature = OpenSSL::HMAC.digest(sha_256_digest, self.secret_key, request_url)
  signature = Base64.encode64(signature).strip
  puts "signature is #{signature}"

  uri = URI("#{APIAddress}#{request_body}&signature=#{CGI.escape(signature)}")
  # Net::HTTP.start(uri.host, uri.port, :user_ssl => uri.scheme == "https") do |http|
  #   req = Net::HTTP::Get.new(uri)
  #   res = http.request(req)
  # end
  response_body = Net::HTTP.get_response(uri).body
end