Module: Aws::PageableResponse
- Defined in:
- gems/aws-sdk-core/lib/aws-sdk-core/pageable_response.rb
Overview
Methods such as to_json will enumerate all of the responses before
returning the full response as JSON.
Decorates a Seahorse::Client::Response with paging convenience methods.
Some AWS calls provide paged responses to limit the amount of data returned
with each response. To optimize for latency, some APIs may return an
inconsistent number of responses per page. You should rely on the values of
the next_page? method or using enumerable methods such as each rather
than the number of items returned to iterate through results. See below for
examples.
Paged Responses Are Enumerable
The simplest way to handle paged response data is to use the built-in enumerator in the response object, as shown in the following example.
s3 = Aws::S3::Client.new
s3.list_objects(bucket:'aws-sdk').each do |response|
puts response.contents.map(&:key)
end
This yields one response object per API call made, and enumerates objects in the named bucket. The SDK retrieves additional pages of data to complete the request.
Handling Paged Responses Manually
To handle paging yourself, use the response’s next_page? method to verify
there are more pages to retrieve, or use the last_page? method to verify
there are no more pages to retrieve.
If there are more pages, use the next_page method to retrieve the
next page of results, as shown in the following example.
s3 = Aws::S3::Client.new
# Get the first page of data
response = s3.list_objects(bucket:'aws-sdk')
# Get additional pages
while response.next_page? do
response = response.next_page
# Use the response data here...
puts response.contents.map(&:key)
end
Every client response supports these methods. A response from an
operation that does not paginate has a single page, so last_page?
returns true and each yields only that response.
Defined Under Namespace
Classes: LastPageError
Instance Attribute Summary collapse
Class Method Summary collapse
-
.apply(base) ⇒ Object
Adds paging methods when needed and resets cached paging state.
Instance Method Summary collapse
-
#each {|response| ... } ⇒ Enumerable?
(also: #each_page)
Yields the current and each following response to the given block.
-
#last_page? ⇒ Boolean
Returns
trueif there are no more results. -
#next_page(params = {}) ⇒ Seahorse::Client::Response
-
#next_page? ⇒ Boolean
Returns
trueif there are more results.
Instance Attribute Details
#pager ⇒ Paging::Pager
65 66 67 |
# File 'gems/aws-sdk-core/lib/aws-sdk-core/pageable_response.rb', line 65 def pager @pager end |
Class Method Details
.apply(base) ⇒ Object
Adds paging methods when needed and resets cached paging state. An Response already has the methods through its class.
57 58 59 60 61 62 |
# File 'gems/aws-sdk-core/lib/aws-sdk-core/pageable_response.rb', line 57 def self.apply(base) base.extend Extension unless Extension === base base.instance_variable_set(:@last_page, nil) base.instance_variable_set(:@more_results, nil) base end |
Instance Method Details
#each {|response| ... } ⇒ Enumerable? Also known as: each_page
Yields the current and each following response to the given block.
89 90 91 |
# File 'gems/aws-sdk-core/lib/aws-sdk-core/pageable_response.rb', line 89 def each(&block) # Actual implementation is in PageableResponse::Extension end |
#last_page? ⇒ Boolean
Returns true if there are no more results. Calling #next_page
when this method returns false will raise an error.
70 71 72 |
# File 'gems/aws-sdk-core/lib/aws-sdk-core/pageable_response.rb', line 70 def last_page? # Actual implementation is in PageableResponse::Extension end |
#next_page(params = {}) ⇒ Seahorse::Client::Response
82 83 84 |
# File 'gems/aws-sdk-core/lib/aws-sdk-core/pageable_response.rb', line 82 def next_page(params = {}) # Actual implementation is in PageableResponse::Extension end |
#next_page? ⇒ Boolean
Returns true if there are more results. Calling #next_page will
return the next response.
77 78 79 |
# File 'gems/aws-sdk-core/lib/aws-sdk-core/pageable_response.rb', line 77 def next_page? # Actual implementation is in PageableResponse::Extension end |