class IBM::Cloud::SDKHTTP::SDKResponse

Encapsulate the HTTP response. @param response [HTTP::Response] The HTTP response object.

Attributes

response[R]

The raw HTTP response. @return [HTTP::Response]

Public Class Methods

new(response) click to toggle source
# File lib/ibm/cloud/sdk_http/sdk_response.rb, line 12
def initialize(response)
  @response = response
end

Public Instance Methods

array_response() click to toggle source

Verify that the json response is an array. @return [Array] Response from JSON @raise [RuntimeError] JSON object is not a Array.

# File lib/ibm/cloud/sdk_http/sdk_response.rb, line 102
def array_response
  check_object(Array)
end
body() click to toggle source

Return the raw response string. @return [String] @return [nil] Response does not have body method.

# File lib/ibm/cloud/sdk_http/sdk_response.rb, line 33
def body
  response&.body.to_s
end
check_object(obj) click to toggle source

Check to see if the returned object is the expected object. @param obj [Object] The object to test the response against. @raise [Exceptions::ExceptionWithResponse] Parsed JSON is not the expecte class.

# File lib/ibm/cloud/sdk_http/sdk_response.rb, line 122
def check_object(obj)
  ret = json
  return ret if ret.instance_of?(obj)

  msg = "Expected #{obj} in response for #{url}. The returned object is a #{ret.class}."
  raise Exceptions::ExceptionWithResponse.new(msg, self)
end
code() click to toggle source

Return the response code. @return [Integer] Response has code method. @return [nil] Response does not have code method.

# File lib/ibm/cloud/sdk_http/sdk_response.rb, line 40
def code
  response&.code
end
Also aliased as: status
connection() click to toggle source

Return the raw connection object. @return [HTTP::Connection] @return [nil] Response does not have a connection method.

# File lib/ibm/cloud/sdk_http/sdk_response.rb, line 49
def connection
  response&.request
end
content_type() click to toggle source

Return the content type of the response. @return [String] The mimetype of the response. @return [nil] Response does not have response method that responds to mime_type.

# File lib/ibm/cloud/sdk_http/sdk_response.rb, line 66
def content_type
  response&.response&.content_type
end
hash_response() click to toggle source

Verify that the json response is a hash. @return [Hash] Response from JSON @raise [RuntimeError] JSON object is not a Hash.

# File lib/ibm/cloud/sdk_http/sdk_response.rb, line 95
def hash_response
  check_object(Hash)
end
json() click to toggle source

Return the response in a hash or array. @return [Hash] When response is a hash. @return [Array] When response is an array. @raise [Exceptions::ExceptionWithResponse] Contents of body is not properly formatted json.

# File lib/ibm/cloud/sdk_http/sdk_response.rb, line 24
def json
  JSON.parse(body, symbolize_names: true)
rescue StandardError
  raise Exceptions::ExceptionWithResponse.new("#{url} Error while parsing response body. #{response.body}", self)
end
raise_for_status!() click to toggle source

Chainable method to verify the status code. Raise an exception for non 200-series or 404 status codes. @return [Response] Allows for method to be chainable. @raise [Exceptions::HttpStatusError] Raise if status checks failed.

# File lib/ibm/cloud/sdk_http/sdk_response.rb, line 56
def raise_for_status!
  return self if (200..299).include?(code)
  return self if code == 404

  raise Exceptions::HttpStatusError.new(self)
end
reason() click to toggle source

Return the textual reason. @return [String] HTTP Reason @return [nil] Response does not have reaspn method that responds.

# File lib/ibm/cloud/sdk_http/sdk_response.rb, line 73
def reason
  response&.response&.msg
end
status()
Alias for: code
subkey(key) click to toggle source

Find a subkey within the returned response. @param key [String] Name of a first level key. @return [Any] Response from JSON @raise [RuntimeError] JSON object is not a Array.

# File lib/ibm/cloud/sdk_http/sdk_response.rb, line 110
def subkey(key)
  ret = hash_response
  sym_key = key.to_sym
  return ret.fetch(sym_key) if ret.key?(sym_key)

  msg = "Key #{key} not found in #{ret}."
  raise Exceptions::ExceptionWithResponse.new(msg, self)
end
uri() click to toggle source

Return the sent url as a URI class. @see github.com/httprb/http/blob/master/lib/http/uri.rb URI Class doc. @return [HTTP::URI] @return [nil] Response does not have response method that responds to mime_type.

# File lib/ibm/cloud/sdk_http/sdk_response.rb, line 88
def uri
  connection&.uri
end
url() click to toggle source

Return the sent url as a string. @return [String] Full URL sent @return [nil] Response does not have response method that responds to mime_type.

# File lib/ibm/cloud/sdk_http/sdk_response.rb, line 80
def url
  uri.to_s
end