Method: CloudstackClient::Connection#send_request
- Defined in:
- lib/cloudstack_client/client.rb
#send_request(params) ⇒ Object
Sends a synchronous request to the CloudStack API and returns the response as a Hash.
The wrapper element of the response (e.g. mycommandresponse) is discarded and the contents of that element are returned.
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 91 92 93 94 95 96 97 |
# File 'lib/cloudstack_client/client.rb', line 40 def send_request(params) params['response'] = 'json' params['apiKey'] = @api_key params_arr = [] params.sort.each { |elem| params_arr << elem[0].to_s + '=' + CGI.escape(elem[1].to_s).gsub('+', '%20').gsub(' ','%20') } debug_output JSON.pretty_generate(params) if @debug data = params_arr.join('&') signature = OpenSSL::HMAC.digest('sha1', @secret_key, data.downcase) signature = Base64.encode64(signature).chomp signature = CGI.escape(signature) url = "#{@api_url}?#{data}&signature=#{signature}" uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) if uri.scheme == 'https' http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end begin response = http.request(Net::HTTP::Get.new(uri.request_uri)) rescue puts "Error connecting to API:" puts "#{@api_url} is not reachable" exit 1 end if response.is_a? Net::HTTPOK begin json = JSON.parse(response.body) json = json[params['command'].downcase + 'response'] rescue JSON::ParserError puts "Error parsing response from server:" puts response.body exit 2 end else begin json = JSON.parse(response.body) puts "Error executing command..." puts json if @debug json = json[params['command'].downcase + 'response'] puts "#{json['errorcode']}: #{json['errortext']}" rescue puts "Error parsing response from server..." puts "#{response.code}: #{response.body}" end exit 3 end end |