Class: QingCloud::SDK::Client::Connector
- Inherits:
-
Object
- Object
- QingCloud::SDK::Client::Connector
- Defined in:
- lib/qingcloud/sdk/client/connector.rb
Instance Attribute Summary collapse
-
#access_key ⇒ Object
Returns the value of attribute access_key.
-
#secret_key ⇒ Object
Returns the value of attribute secret_key.
Class Method Summary collapse
Instance Method Summary collapse
- #fetch(action, params = {}) ⇒ Object
-
#initialize(access_key, secret_key) ⇒ Connector
constructor
A new instance of Connector.
Constructor Details
#initialize(access_key, secret_key) ⇒ Connector
Returns a new instance of Connector.
15 16 17 18 19 20 21 |
# File 'lib/qingcloud/sdk/client/connector.rb', line 15 def initialize(access_key, secret_key) raise Error::ParameterError, 'Load API Key' unless access_key && access_key.length > 0 raise Error::ParameterError, 'Load API Key' unless secret_key && secret_key.length > 0 self.access_key = access_key self.secret_key = secret_key end |
Instance Attribute Details
#access_key ⇒ Object
Returns the value of attribute access_key.
12 13 14 |
# File 'lib/qingcloud/sdk/client/connector.rb', line 12 def access_key @access_key end |
#secret_key ⇒ Object
Returns the value of attribute secret_key.
13 14 15 |
# File 'lib/qingcloud/sdk/client/connector.rb', line 13 def secret_key @secret_key end |
Class Method Details
.init(access_key, secret_key) ⇒ Object
23 24 25 |
# File 'lib/qingcloud/sdk/client/connector.rb', line 23 def self.init(access_key, secret_key) Connector.new access_key, secret_key end |
.init_with_config_file ⇒ Object
27 28 29 30 31 32 |
# File 'lib/qingcloud/sdk/client/connector.rb', line 27 def self.init_with_config_file config_map = {} content = Utility.file_manager.read_config_file config_map = Utility.json_parser.decode content if content Connector.new config_map['qy_access_key_id'], config_map['qy_secret_access_key'] end |
Instance Method Details
#fetch(action, params = {}) ⇒ Object
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/qingcloud/sdk/client/connector.rb', line 34 def fetch(action, params={}) raise Error::ParameterError, 'Check API Request' unless action && action.length > 0 params.update( action: action, time_stamp: Time.now.utc.strftime('%Y-%m-%dT%H:%M:%SZ'), access_key_id: access_key, version: 1, signature_method: 'HmacSHA256', signature_version: 1, ) request_body = params.sort.map { |key, value| if value.is_a? Array if key.to_s.include? '_N_' parts = key.to_s.split '_N_' value.map { |v| "#{CGI.escape parts[0].to_s}.#{value.index(v)+1}.#{CGI.escape parts[1].to_s}=#{CGI.escape v.to_s}" }.join('&') elsif key.to_s.include? '_N' value.map { |v| "#{CGI.escape key.to_s.gsub('_N', '').to_s}.#{value.index(v)+1}=#{CGI.escape v.to_s}" }.join('&') end else "#{CGI.escape key.to_s}=#{CGI.escape value.to_s}" end }.join('&') signature = Base64.encode64( OpenSSL::HMAC.digest( OpenSSL::Digest.new('sha256'), self.secret_key, "GET\n/iaas/\n#{request_body}" ) ).strip request_url = "#{Contract::API_URL}#{request_body}&signature=#{CGI.escape signature}" # Log Utility.logger.info request_url response = Net::HTTPResponse.new 1.1, 500, 'Error' begin response = Net::HTTP.get_response URI(request_url) rescue raise Error::NetworkError end raise Error::ServerError, response.code unless response.code == '200' # Log Utility.logger.info response.body Utility.json_parser.decode response.body end |