You are viewing documentation for version 1 of the AWS SDK for Ruby. Version 2 documentation can be found here.

Class: AWS::Core::Response

Inherits:
Object
  • Object
show all
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:

Given the example and response object from above:

response.request_type #=> :describe_instances
response.request_options #=> { :instance_ids => ['i-12345678'] }
response.http_request #=> #<AWS::Core::Http::Request>
response.http_response #=> #<AWS::Core::Http::Response>

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:



107
108
109
110
111
112
113
114
115
# File 'lib/aws/core/response.rb', line 107

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)

Note:

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.

See Also:



187
188
189
# File 'lib/aws/core/response.rb', line 187

def method_missing *args, &block
  Core::Data.new(data).send(*args, &block)
end

Instance Attribute Details

#api_versionString

Returns The API version of the request/client.

Returns:

  • (String)

    The API version of the request/client.



74
75
76
# File 'lib/aws/core/response.rb', line 74

def api_version
  @api_version
end

#cachedBoolean Also known as: cached?

Returns true if the response was generated from a another cached response.

Returns:

  • (Boolean)

    true if the response was generated from a another cached response.



88
89
90
# File 'lib/aws/core/response.rb', line 88

def cached
  @cached
end

#dataHash

Returns the response data as a hash.

Returns:

  • (Hash)

    Returns the response data as a hash.



64
65
66
# File 'lib/aws/core/response.rb', line 64

def data
  @data
end

#durationFloat

Returns The total number of seconds taken to make the request and return the response.

Returns:

  • (Float)

    The total number of seconds taken to make the request and return the response.



103
104
105
# File 'lib/aws/core/response.rb', line 103

def duration
  @duration
end

#errorAWS::Error?

Returns nil unless the request failed. Normally this will be nil unless you are using the Asynchronous interface.

Returns:

  • (AWS::Error, nil)

    Returns nil unless the request failed. Normally this will be nil unless you are using the Asynchronous interface.



95
96
97
# File 'lib/aws/core/response.rb', line 95

def error
  @error
end

#http_requestCore::Http::Request

Returns:



81
82
83
# File 'lib/aws/core/response.rb', line 81

def http_request
  @http_request
end

#http_responseCore::Http::Response



84
85
86
# File 'lib/aws/core/response.rb', line 84

def http_response
  @http_response
end

#request_optionsHash

Returns the hash of options passed to the client request method that generated this response.

Returns:

  • (Hash)

    Returns the hash of options passed to the client request method that generated this response.



78
79
80
# File 'lib/aws/core/response.rb', line 78

def request_options
  @request_options
end

#request_typeSymbol

The name of the client request method that returned this response.

Returns:

  • (Symbol)

    The name of the client request method that returned this response.



71
72
73
# File 'lib/aws/core/response.rb', line 71

def request_type
  @request_type
end

#retry_countInteger

Returns the number of times the request was retried.

Returns:

  • (Integer)

    Returns the number of times the request was retried.



99
100
101
# File 'lib/aws/core/response.rb', line 99

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].

Parameters:

  • key (Symbol, String)

Returns:

  • (Hash, nil)


122
123
124
# File 'lib/aws/core/response.rb', line 122

def [] key
  data[key]
end

#network_error?Boolean

Returns true if the http request failed due to a networking issue.

Returns:

  • (Boolean)

    Returns true if the http request failed due to a networking issue.



133
134
135
# File 'lib/aws/core/response.rb', line 133

def network_error?
  http_response.network_error?
end

#safe_to_retry?Boolean

Returns false if it is not safe to retry a request. This happens when the http request body is an IO object that can not be rewound and re-streamed.

Returns:

  • (Boolean)

    Returns false if it is not safe to retry a request. This happens when the http request body is an IO object that can not be rewound and re-streamed.



167
168
169
170
# File 'lib/aws/core/response.rb', line 167

def safe_to_retry?
  @http_request.body_stream.nil? or
  @http_request.body_stream.respond_to?(:rewind)
end

#successful?Boolean

Returns true if there is no response error.

Returns:

  • (Boolean)

    Returns true if there is no response error.



127
128
129
# File 'lib/aws/core/response.rb', line 127

def successful?
  error.nil?
end