|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Smithy |
| 4 | + module Client |
| 5 | + # Decorates a {Smithy::Client::Output} with paging convenience methods. |
| 6 | + # Most API calls provide paged responses to limit the amount of data returned |
| 7 | + # with each response. To optimize for latency, some APIs may return an |
| 8 | + # inconsistent number of responses per page. You should rely on the values of |
| 9 | + # the `next_page?` method or using enumerable methods such as `each_page` rather |
| 10 | + # than the number of items returned to iterate through results. See below for |
| 11 | + # examples. |
| 12 | + # |
| 13 | + # # Enumerator Methods |
| 14 | + # The simplest way to handle paged response data is to use the built-in |
| 15 | + # `each_page` enumerator on the output object: |
| 16 | + # |
| 17 | + # weather = Weather::Client.new |
| 18 | + # weather.list_cities.each_page do |page| |
| 19 | + # puts page.items.map(&:name) |
| 20 | + # end |
| 21 | + # |
| 22 | + # This yields one output object per API call made. The SDK retrieves additional |
| 23 | + # pages of data to complete the request. |
| 24 | + # |
| 25 | + # If the operation allows for it, a selected item can be enumerated using |
| 26 | + # `each_item`: |
| 27 | + # |
| 28 | + # weather = Weather::Client.new |
| 29 | + # weather.list_cities.each_item do |item| |
| 30 | + # puts item.name |
| 31 | + # end |
| 32 | + # |
| 33 | + # # Handling Paged Output Manually |
| 34 | + # To handle paging yourself, use the output's `next_page?` method to verify |
| 35 | + # there are more pages to retrieve, or use the `last_page?` method to verify |
| 36 | + # there are no more pages to retrieve. |
| 37 | + # |
| 38 | + # If there are more pages, use the `next_page` method to retrieve the |
| 39 | + # next page of results, as shown in the following example. |
| 40 | + # |
| 41 | + # weather = Weather::Client.new |
| 42 | + # |
| 43 | + # # Get the first page of data |
| 44 | + # output = weather.list_cities |
| 45 | + # |
| 46 | + # # Get additional pages |
| 47 | + # while output.next_page? |
| 48 | + # output = output.next_page |
| 49 | + # # Use the response data here... |
| 50 | + # puts output.items.map(&:name) |
| 51 | + # end |
| 52 | + # |
| 53 | + module PageableOutput |
| 54 | + # @api private |
| 55 | + attr_accessor :paginator |
| 56 | + |
| 57 | + # Returns `true` if there are no more results. Calling {#next_page} |
| 58 | + # when this method returns `false` will raise an error. |
| 59 | + # @return [Boolean] |
| 60 | + def last_page? |
| 61 | + return @last_page if @last_page |
| 62 | + |
| 63 | + @last_page = !truncated? |
| 64 | + end |
| 65 | + |
| 66 | + # Returns `true` if there are more results. Calling {#next_page} will |
| 67 | + # return the next response. |
| 68 | + # @return [Boolean] |
| 69 | + def next_page? |
| 70 | + return @next_page if @next_page |
| 71 | + |
| 72 | + @next_page = truncated? |
| 73 | + end |
| 74 | + |
| 75 | + # @param [Hash] params A hash of additional request params. |
| 76 | + # @return [Output] Returns the next page of results. |
| 77 | + def next_page(params = {}) |
| 78 | + raise Errors::LastPageError, self if last_page? |
| 79 | + |
| 80 | + params = next_page_params(params) |
| 81 | + context.client.send(context.operation_name, params) |
| 82 | + end |
| 83 | + |
| 84 | + # Yields the current and each following output to the given block. |
| 85 | + # @yieldparam [Output] output |
| 86 | + # @return [Enumerable, nil] Returns a new Enumerable if no block is given. |
| 87 | + def each_page(&) |
| 88 | + output = self |
| 89 | + yield(output) |
| 90 | + until output.last_page? |
| 91 | + output = output.next_page |
| 92 | + yield(output) |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + # Yields the current and each following item to the given block. |
| 97 | + # @yieldparam [Object] item |
| 98 | + # @return [Enumerable, nil] Returns a new Enumerable if no block is given. |
| 99 | + def each_item(&) |
| 100 | + output = self |
| 101 | + @paginator.items(output.data).each(&) |
| 102 | + until output.last_page? |
| 103 | + output = output.next_page |
| 104 | + @paginator.items(output.data).each(&) |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + private |
| 109 | + |
| 110 | + def truncated? |
| 111 | + next_t = @paginator.next_tokens(data) |
| 112 | + !(next_t.empty? || next_t == @paginator.prev_tokens(context.params)) |
| 113 | + end |
| 114 | + |
| 115 | + def next_page_params(params) |
| 116 | + prev_tokens = @paginator.prev_tokens(context.params) |
| 117 | + # Remove all previous tokens from original params |
| 118 | + # Sometimes a token can be nil and merge would not include it. |
| 119 | + new_params = context.params.except(*prev_tokens) |
| 120 | + new_params.merge!(@paginator.next_tokens(data).merge(params)) |
| 121 | + end |
| 122 | + end |
| 123 | + end |
| 124 | +end |
0 commit comments