Class: IBM::Cloud::SDKHTTP::SDKResponse
- Inherits:
-
Object
- Object
- IBM::Cloud::SDKHTTP::SDKResponse
- Defined in:
- lib/ibm/cloud/sdk_http/sdk_response.rb
Overview
Encapsulate the HTTP response.
Instance Attribute Summary collapse
-
#response ⇒ HTTP::Response
readonly
The raw HTTP response.
Instance Method Summary collapse
-
#array_response ⇒ Array
Verify that the json response is an array.
-
#body ⇒ String?
Return the raw response string.
-
#check_object(obj) ⇒ Object
Check to see if the returned object is the expected object.
-
#code ⇒ Integer?
(also: #status)
Return the response code.
-
#connection ⇒ HTTP::Connection?
Return the raw connection object.
-
#content_type ⇒ String?
Return the content type of the response.
-
#hash_response ⇒ Hash
Verify that the json response is a hash.
-
#initialize(response) ⇒ SDKResponse
constructor
A new instance of SDKResponse.
-
#json ⇒ Hash, Array
Return the response in a hash or array.
-
#raise_for_status! ⇒ Response
Chainable method to verify the status code.
-
#reason ⇒ String?
Return the textual reason.
-
#subkey(key) ⇒ Any
Find a subkey within the returned response.
-
#uri ⇒ HTTP::URI?
Return the sent url as a URI class.
-
#url ⇒ String?
Return the sent url as a string.
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
#response ⇒ HTTP::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_response ⇒ Array
Verify that the json response is an array.
102 103 104 |
# File 'lib/ibm/cloud/sdk_http/sdk_response.rb', line 102 def array_response check_object(Array) end |
#body ⇒ String?
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.
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 |
#code ⇒ Integer? 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 |
#connection ⇒ HTTP::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_type ⇒ String?
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_response ⇒ Hash
Verify that the json response is a hash.
95 96 97 |
# File 'lib/ibm/cloud/sdk_http/sdk_response.rb', line 95 def hash_response check_object(Hash) end |
#json ⇒ Hash, Array
Return the response in a hash or array.
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.
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 |
#reason ⇒ String?
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.
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 |
#uri ⇒ HTTP::URI?
Return the sent url as a URI class.
88 89 90 |
# File 'lib/ibm/cloud/sdk_http/sdk_response.rb', line 88 def uri connection&.uri end |
#url ⇒ String?
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 |