Class: AWS::Core::Response
- Inherits:
-
Object
- Object
- AWS::Core::Response
- Includes:
- AsyncHandle
- Defined in:
- lib/aws/core/response.rb
Overview
Response
Each Service has a Client class. There is one method per service operation defined on the client. These methods all return a Response object.
In addition to the response data, these responses provide metadata about the HTTP request made and the HTTP response received.
Response Data
You can access the response data for a client request using the #data method or the #[] method. Response data is a hash and #[] is a shortcut for accessing this hash.
# make a request to describe one instance
ec2 = AWS::EC2.new
response = ec2.client.describe_instances(:instance_ids => ['i-12345678'])
# find the instance in the response data (2 ways to get the data)
instance = response[:reservation_set].first[:instance_set].first
instance = response.data[:reservation_set].first[:instance_set].first
instance[:status] #=> 'running'
Response Metadata
In addition to the response data, there is additional information available with the response, including:
-
#request_type - the name of the client request method
-
#request_options - the hash of options passed to the client method
-
#http_request - The HTTP request made
-
#http_response - the HTTP response received
Given the example and response object from above:
response.request_type #=> :describe_instances
response. #=> { :instance_ids => ['i-12345678'] }
response.http_request #=> #<AWS::Core::Http::Request>
response.http_response #=> #<AWS::Core::Http::Response>
Instance Attribute Summary collapse
-
#cached ⇒ Boolean
(also: #cached?)
True if the response was generated from a another cached response.
-
#data ⇒ Hash
Returns the response data as a hash.
-
#duration ⇒ Float
The total number of seconds taken to make the request and return the response.
-
#error ⇒ AWS::Error?
Returns nil unless the request failed.
- #http_request ⇒ Core::Http::Request
- #http_response ⇒ Core::Http::Response
-
#request_options ⇒ Hash
Returns the hash of options passed to the client request method that generated this response.
-
#request_type ⇒ Symbol
The name of the client request method that returned this response.
-
#retry_count ⇒ Integer
Returns the number of times the request was retried.
Instance Method Summary collapse
-
#[](key) ⇒ Hash?
Provides access to the response data.
-
#initialize(http_request = nil, http_response = nil, &block) ⇒ Response
constructor
A new instance of Response.
-
#network_error? ⇒ Boolean
Returns
trueif the http request failed due to a networking issue. -
#safe_to_retry? ⇒ Boolean
Returns
falseif it is not safe to retry a request. -
#successful? ⇒ Boolean
Returns true if there is no response error.
Methods included from AsyncHandle
#on_complete, #on_failure, #on_success, #signal_failure, #signal_success
Constructor Details
#initialize(http_request = nil, http_response = nil, &block) ⇒ Response
101 102 103 104 105 106 107 108 109 |
# File 'lib/aws/core/response.rb', line 101 def initialize http_request = nil, http_response = nil, &block @http_request = http_request @http_response = http_response @request_builder = block @data = {} @retry_count = 0 @duration = 0 build_request if @request_builder && !http_request end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args, &block) ⇒ Object (protected)
The prefered method to get as response data is to use #[].
This provides a backwards-compat layer to the old response objects where each response value had a method extended onto this object. Now all response data is accessible as a hash.
180 181 182 |
# File 'lib/aws/core/response.rb', line 180 def method_missing *args, &block Core::Data.new(data).send(*args, &block) end |
Instance Attribute Details
#cached ⇒ Boolean Also known as: cached?
82 83 84 |
# File 'lib/aws/core/response.rb', line 82 def cached @cached end |
#data ⇒ Hash
64 65 66 |
# File 'lib/aws/core/response.rb', line 64 def data @data end |
#duration ⇒ Float
97 98 99 |
# File 'lib/aws/core/response.rb', line 97 def duration @duration end |
#error ⇒ AWS::Error?
89 90 91 |
# File 'lib/aws/core/response.rb', line 89 def error @error end |
#http_request ⇒ Core::Http::Request
75 76 77 |
# File 'lib/aws/core/response.rb', line 75 def http_request @http_request end |
#http_response ⇒ Core::Http::Response
78 79 80 |
# File 'lib/aws/core/response.rb', line 78 def http_response @http_response end |
#request_options ⇒ Hash
72 73 74 |
# File 'lib/aws/core/response.rb', line 72 def @request_options end |
#request_type ⇒ Symbol
68 69 70 |
# File 'lib/aws/core/response.rb', line 68 def request_type @request_type end |
#retry_count ⇒ Integer
93 94 95 |
# File 'lib/aws/core/response.rb', line 93 def retry_count @retry_count end |
Instance Method Details
#[](key) ⇒ Hash?
Provides access to the response data. This is a short-cut for calling response.data[key].
116 117 118 |
# File 'lib/aws/core/response.rb', line 116 def [] key data[key] end |
#network_error? ⇒ Boolean
127 128 129 |
# File 'lib/aws/core/response.rb', line 127 def network_error? http_response.network_error? end |
#safe_to_retry? ⇒ Boolean
160 161 162 163 |
# File 'lib/aws/core/response.rb', line 160 def safe_to_retry? @http_request.body_stream.nil? or @http_request.body_stream.respond_to?(:rewind) end |
#successful? ⇒ Boolean
121 122 123 |
# File 'lib/aws/core/response.rb', line 121 def successful? error.nil? end |