Method: CloudstackClient::Connection#send_request
- Defined in:
- lib/cloudstack_client/connection.rb
#send_request(params) ⇒ Object
Sends a synchronous request to the CloudStack API and returns the response as a Hash.
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 |
# File 'lib/cloudstack_client/connection.rb', line 38 def send_request(params) params['response'] = 'json' params['apiKey'] = @api_key print_debug_output JSON.pretty_generate(params) if @debug data = params_to_data(params) uri = URI.parse "#{@api_url}?#{data}&signature=#{create_signature(data)}" http = Net::HTTP.new(uri.host, uri.port) if uri.scheme == 'https' http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end http.read_timeout = @read_timeout begin req = Net::HTTP::Get.new(uri.request_uri) req['Host'] = host if host.present? response = http.request(req) rescue raise ConnectionError, "API URL \'#{@api_url}\' is not reachable." end begin body = JSON.parse(response.body, symbolize_names: @symbolize_keys).values.first rescue JSON::ParserError raise ParseError, "Response from server is not readable. Check if the API endpoint (#{@api_url}) is valid and accessible." end if response.is_a?(Net::HTTPOK) return body unless body.respond_to?(:keys) if body.size == 2 && body.key?(k('count')) return body.reject { |key, _| key == k('count') }.values.first elsif body.size == 1 && body.values.first.respond_to?(:keys) item = body.values.first return (item.is_a?(Array) || item.is_a?(Hash)) ? item : [] else body.reject! { |key, _| key == k('count') } if body.key?(k('count')) body.size == 0 ? [] : body end else = body[k('errortext')] rescue body raise ApiError, "Status #{response.code}: #{}." end end |