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



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.



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.

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.



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.

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.



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.



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.



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.

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.

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.

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.



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.

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.

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.



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

def url
  uri.to_s
end