Class: IBM::Cloud::SDKHTTP::SDKResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/ibm/cloud/sdk_http/sdk_response.rb

Overview

Encapsulate the HTTP response.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ SDKResponse

Returns a new instance of SDKResponse.



12
13
14
# File 'lib/ibm/cloud/sdk_http/sdk_response.rb', line 12

def initialize(response)
  @response = response
end

Instance Attribute Details

#responseHTTP::Response (readonly)

The raw HTTP response.

Returns:

  • (HTTP::Response)


18
19
20
# File 'lib/ibm/cloud/sdk_http/sdk_response.rb', line 18

def response
  @response
end

Instance Method Details

#array_responseArray

Verify that the json response is an array.

Returns:

  • (Array)

    Response from JSON

Raises:

  • (RuntimeError)

    JSON object is not a Array.



102
103
104
# File 'lib/ibm/cloud/sdk_http/sdk_response.rb', line 102

def array_response
  check_object(Array)
end

#bodyString?

Return the raw response string.

Returns:

  • (String)
  • (nil)

    Response does not have body method.



33
34
35
# File 'lib/ibm/cloud/sdk_http/sdk_response.rb', line 33

def body
  response&.body.to_s
end

#check_object(obj) ⇒ Object

Check to see if the returned object is the expected object.

Parameters:

  • obj (Object)

    The object to test the response against.

Raises:



122
123
124
125
126
127
128
# 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

#codeInteger? Also known as: status

Return the response code.

Returns:

  • (Integer)

    Response has code method.

  • (nil)

    Response does not have code method.



40
41
42
# File 'lib/ibm/cloud/sdk_http/sdk_response.rb', line 40

def code
  response&.code
end

#connectionHTTP::Connection?

Return the raw connection object.

Returns:

  • (HTTP::Connection)
  • (nil)

    Response does not have a connection method.



49
50
51
# File 'lib/ibm/cloud/sdk_http/sdk_response.rb', line 49

def connection
  response&.request
end

#content_typeString?

Return the content type of the response.

Returns:

  • (String)

    The mimetype of the response.

  • (nil)

    Response does not have response method that responds to mime_type.



66
67
68
# File 'lib/ibm/cloud/sdk_http/sdk_response.rb', line 66

def content_type
  response&.response&.content_type
end

#hash_responseHash

Verify that the json response is a hash.

Returns:

  • (Hash)

    Response from JSON

Raises:

  • (RuntimeError)

    JSON object is not a Hash.



95
96
97
# File 'lib/ibm/cloud/sdk_http/sdk_response.rb', line 95

def hash_response
  check_object(Hash)
end

#jsonHash, Array

Return the response in a hash or array.

Returns:

  • (Hash)

    When response is a hash.

  • (Array)

    When response is an array.

Raises:



24
25
26
27
28
# 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!Response

Chainable method to verify the status code. Raise an exception for non 200-series or 404 status codes.

Returns:

  • (Response)

    Allows for method to be chainable.

Raises:



56
57
58
59
60
61
# 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

#reasonString?

Return the textual reason.

Returns:

  • (String)

    HTTP Reason

  • (nil)

    Response does not have reaspn method that responds.



73
74
75
# File 'lib/ibm/cloud/sdk_http/sdk_response.rb', line 73

def reason
  response&.response&.msg
end

#subkey(key) ⇒ Any

Find a subkey within the returned response.

Parameters:

  • key (String)

    Name of a first level key.

Returns:

  • (Any)

    Response from JSON

Raises:

  • (RuntimeError)

    JSON object is not a Array.



110
111
112
113
114
115
116
117
# 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

#uriHTTP::URI?

Return the sent url as a URI class.

Returns:

  • (HTTP::URI)
  • (nil)

    Response does not have response method that responds to mime_type.

See Also:



88
89
90
# File 'lib/ibm/cloud/sdk_http/sdk_response.rb', line 88

def uri
  connection&.uri
end

#urlString?

Return the sent url as a string.

Returns:

  • (String)

    Full URL sent

  • (nil)

    Response does not have response method that responds to mime_type.



80
81
82
# File 'lib/ibm/cloud/sdk_http/sdk_response.rb', line 80

def url
  uri.to_s
end