Class: AWS::Core::Response
- Inherits:
-
Object
- Object
- AWS::Core::Response
- Includes:
- AsyncHandle
- Defined in:
- lib/aws/core/response.rb
Overview
Response
Each service request returns a response object. Responses provide access to response data and request/response metadata.
Response Data
Each response has a hash of data that represents the data returned by the service. You can get at this data by calling #data (you can also use the #[] method as a shortcut)
# 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:
-
the name of the client request method called
-
the hash of options passed to the client request
-
the HTTP request object (useful for debugging)
-
the HTTP response object (useful for debugging)
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.
- #cache_key ⇒ String
-
#initialize(http_request = nil, http_response = nil, &block) ⇒ Response
constructor
A new instance of Response.
- #inspect ⇒ String
-
#rebuild_request ⇒ Object
Rebuilds the HTTP request using the block passed to the initializer.
-
#successful? ⇒ Boolean
Returns true if there is no response error.
-
#throttled? ⇒ Boolean
Returns true if the http request was throttled by AWS.
-
#timeout? ⇒ Boolean
Returns true if the http request timed out.
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
Returns a new instance of Response.
97 98 99 100 101 102 103 104 105 |
# File 'lib/aws/core/response.rb', line 97 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 rebuild_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.
174 175 176 |
# File 'lib/aws/core/response.rb', line 174 def method_missing *args, &block Core::Data.new(data).send(*args, &block) end |
Instance Attribute Details
#cached ⇒ Boolean Also known as: cached?
Returns true if the response was generated from a another cached response.
78 79 80 |
# File 'lib/aws/core/response.rb', line 78 def cached @cached end |
#data ⇒ Hash
Returns the response data as a hash.
60 61 62 |
# File 'lib/aws/core/response.rb', line 60 def data @data end |
#duration ⇒ Float
Returns The total number of seconds taken to make the request and return the response.
93 94 95 |
# File 'lib/aws/core/response.rb', line 93 def duration @duration end |
#error ⇒ AWS::Error?
Returns nil unless the request failed. Normally this will be nil unless you are using the Asynchronous interface.
85 86 87 |
# File 'lib/aws/core/response.rb', line 85 def error @error end |
#http_request ⇒ Core::Http::Request
71 72 73 |
# File 'lib/aws/core/response.rb', line 71 def http_request @http_request end |
#http_response ⇒ Core::Http::Response
74 75 76 |
# File 'lib/aws/core/response.rb', line 74 def http_response @http_response end |
#request_options ⇒ Hash
Returns the hash of options passed to the client request method that generated this response.
68 69 70 |
# File 'lib/aws/core/response.rb', line 68 def @request_options end |
#request_type ⇒ Symbol
The name of the client request method that returned this response.
64 65 66 |
# File 'lib/aws/core/response.rb', line 64 def request_type @request_type end |
#retry_count ⇒ Integer
Returns the number of times the request was retried.
89 90 91 |
# File 'lib/aws/core/response.rb', line 89 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]
.
112 113 114 |
# File 'lib/aws/core/response.rb', line 112 def [] key data[key] end |
#cache_key ⇒ String
146 147 148 149 150 151 152 153 |
# File 'lib/aws/core/response.rb', line 146 def cache_key [ http_request.access_key_id, http_request.host, request_type, ].join(":") end |
#inspect ⇒ String
140 141 142 |
# File 'lib/aws/core/response.rb', line 140 def inspect data.inspect end |
#rebuild_request ⇒ Object
Rebuilds the HTTP request using the block passed to the initializer. This is primarily used by the client when a request must be retried (throttling, server errors, socket errors, etc).
159 160 161 |
# File 'lib/aws/core/response.rb', line 159 def rebuild_request @http_request = @request_builder.call end |
#successful? ⇒ Boolean
Returns true if there is no response error.
117 118 119 |
# File 'lib/aws/core/response.rb', line 117 def successful? error.nil? end |
#throttled? ⇒ Boolean
Returns true if the http request was throttled by AWS.
123 124 125 126 127 128 129 130 131 |
# File 'lib/aws/core/response.rb', line 123 def throttled? if !successful? and http_response.body error = XML::Parser.new.parse(http_response.body) error = error[:error] if error[:error] error[:code] == "Throttling" else false end end |
#timeout? ⇒ Boolean
Returns true if the http request timed out.
134 135 136 |
# File 'lib/aws/core/response.rb', line 134 def timeout? http_response.timeout? end |