|
| 1 | +package software.amazon.awssdk.services.jsonprotocoltests.paginators; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.Iterator; |
| 5 | +import java.util.concurrent.CompletableFuture; |
| 6 | +import java.util.function.Function; |
| 7 | +import org.reactivestreams.Subscriber; |
| 8 | +import software.amazon.awssdk.annotations.Generated; |
| 9 | +import software.amazon.awssdk.core.async.SdkPublisher; |
| 10 | +import software.amazon.awssdk.core.pagination.async.AsyncPageFetcher; |
| 11 | +import software.amazon.awssdk.core.pagination.async.PaginatedItemsPublisher; |
| 12 | +import software.amazon.awssdk.core.pagination.async.ResponsesSubscription; |
| 13 | +import software.amazon.awssdk.services.jsonprotocoltests.JsonProtocolTestsAsyncClient; |
| 14 | +import software.amazon.awssdk.services.jsonprotocoltests.internal.UserAgentUtils; |
| 15 | +import software.amazon.awssdk.services.jsonprotocoltests.model.PaginatedOperationWithResultKeyAndMoreResultsRequest; |
| 16 | +import software.amazon.awssdk.services.jsonprotocoltests.model.PaginatedOperationWithResultKeyAndMoreResultsResponse; |
| 17 | +import software.amazon.awssdk.services.jsonprotocoltests.model.SimpleStruct; |
| 18 | + |
| 19 | +/** |
| 20 | + * <p> |
| 21 | + * Represents the output for the |
| 22 | + * {@link software.amazon.awssdk.services.jsonprotocoltests.JsonProtocolTestsAsyncClient#paginatedOperationWithResultKeyAndMoreResultsPaginator(software.amazon.awssdk.services.jsonprotocoltests.model.PaginatedOperationWithResultKeyAndMoreResultsRequest)} |
| 23 | + * operation which is a paginated operation. This class is a type of {@link org.reactivestreams.Publisher} which can be |
| 24 | + * used to provide a sequence of |
| 25 | + * {@link software.amazon.awssdk.services.jsonprotocoltests.model.PaginatedOperationWithResultKeyAndMoreResultsResponse} |
| 26 | + * response pages as per demand from the subscriber. |
| 27 | + * </p> |
| 28 | + * <p> |
| 29 | + * When the operation is called, an instance of this class is returned. At this point, no service calls are made yet and |
| 30 | + * so there is no guarantee that the request is valid. If there are errors in your request, you will see the failures |
| 31 | + * only after you start streaming the data. The subscribe method should be called as a request to start streaming data. |
| 32 | + * For more info, see {@link org.reactivestreams.Publisher#subscribe(org.reactivestreams.Subscriber)}. Each call to the |
| 33 | + * subscribe method will result in a new {@link org.reactivestreams.Subscription} i.e., a new contract to stream data |
| 34 | + * from the starting request. |
| 35 | + * </p> |
| 36 | + * |
| 37 | + * <p> |
| 38 | + * The following are few ways to use the response class: |
| 39 | + * </p> |
| 40 | + * 1) Using the subscribe helper method |
| 41 | + * |
| 42 | + * <pre> |
| 43 | + * {@code |
| 44 | + * software.amazon.awssdk.services.jsonprotocoltests.paginators.PaginatedOperationWithResultKeyAndMoreResultsPublisher publisher = client.paginatedOperationWithResultKeyAndMoreResultsPaginator(request); |
| 45 | + * CompletableFuture<Void> future = publisher.subscribe(res -> { // Do something with the response }); |
| 46 | + * future.get(); |
| 47 | + * } |
| 48 | + * </pre> |
| 49 | + * |
| 50 | + * 2) Using a custom subscriber |
| 51 | + * |
| 52 | + * <pre> |
| 53 | + * {@code |
| 54 | + * software.amazon.awssdk.services.jsonprotocoltests.paginators.PaginatedOperationWithResultKeyAndMoreResultsPublisher publisher = client.paginatedOperationWithResultKeyAndMoreResultsPaginator(request); |
| 55 | + * publisher.subscribe(new Subscriber<software.amazon.awssdk.services.jsonprotocoltests.model.PaginatedOperationWithResultKeyAndMoreResultsResponse>() { |
| 56 | + * |
| 57 | + * public void onSubscribe(org.reactivestreams.Subscriber subscription) { //... }; |
| 58 | + * |
| 59 | + * |
| 60 | + * public void onNext(software.amazon.awssdk.services.jsonprotocoltests.model.PaginatedOperationWithResultKeyAndMoreResultsResponse response) { //... }; |
| 61 | + * });} |
| 62 | + * </pre> |
| 63 | + * |
| 64 | + * As the response is a publisher, it can work well with third party reactive streams implementations like RxJava2. |
| 65 | + * <p> |
| 66 | + * <b>Please notice that the configuration of MaxResults won't limit the number of results you get with the paginator. |
| 67 | + * It only limits the number of results in each page.</b> |
| 68 | + * </p> |
| 69 | + * <p> |
| 70 | + * <b>Note: If you prefer to have control on service calls, use the |
| 71 | + * {@link #paginatedOperationWithResultKeyAndMoreResults(software.amazon.awssdk.services.jsonprotocoltests.model.PaginatedOperationWithResultKeyAndMoreResultsRequest)} |
| 72 | + * operation.</b> |
| 73 | + * </p> |
| 74 | + */ |
| 75 | +@Generated("software.amazon.awssdk:codegen") |
| 76 | +public class PaginatedOperationWithResultKeyAndMoreResultsPublisher implements |
| 77 | + SdkPublisher<PaginatedOperationWithResultKeyAndMoreResultsResponse> { |
| 78 | + private final JsonProtocolTestsAsyncClient client; |
| 79 | + |
| 80 | + private final PaginatedOperationWithResultKeyAndMoreResultsRequest firstRequest; |
| 81 | + |
| 82 | + private final AsyncPageFetcher nextPageFetcher; |
| 83 | + |
| 84 | + private boolean isLastPage; |
| 85 | + |
| 86 | + public PaginatedOperationWithResultKeyAndMoreResultsPublisher(JsonProtocolTestsAsyncClient client, |
| 87 | + PaginatedOperationWithResultKeyAndMoreResultsRequest firstRequest) { |
| 88 | + this(client, firstRequest, false); |
| 89 | + } |
| 90 | + |
| 91 | + private PaginatedOperationWithResultKeyAndMoreResultsPublisher(JsonProtocolTestsAsyncClient client, |
| 92 | + PaginatedOperationWithResultKeyAndMoreResultsRequest firstRequest, boolean isLastPage) { |
| 93 | + this.client = client; |
| 94 | + this.firstRequest = UserAgentUtils.applyPaginatorUserAgent(firstRequest); |
| 95 | + this.isLastPage = isLastPage; |
| 96 | + this.nextPageFetcher = new PaginatedOperationWithResultKeyAndMoreResultsResponseFetcher(); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void subscribe(Subscriber<? super PaginatedOperationWithResultKeyAndMoreResultsResponse> subscriber) { |
| 101 | + subscriber.onSubscribe(ResponsesSubscription.builder().subscriber(subscriber).nextPageFetcher(nextPageFetcher).build()); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Returns a publisher that can be used to get a stream of data. You need to subscribe to the publisher to request |
| 106 | + * the stream of data. The publisher has a helper forEach method that takes in a {@link java.util.function.Consumer} |
| 107 | + * and then applies that consumer to each response returned by the service. |
| 108 | + */ |
| 109 | + public final SdkPublisher<SimpleStruct> items() { |
| 110 | + Function<PaginatedOperationWithResultKeyAndMoreResultsResponse, Iterator<SimpleStruct>> getIterator = response -> { |
| 111 | + if (response != null && response.items() != null) { |
| 112 | + return response.items().iterator(); |
| 113 | + } |
| 114 | + return Collections.emptyIterator(); |
| 115 | + }; |
| 116 | + return PaginatedItemsPublisher.builder() |
| 117 | + .nextPageFetcher(new PaginatedOperationWithResultKeyAndMoreResultsResponseFetcher()) |
| 118 | + .iteratorFunction(getIterator).isLastPage(isLastPage).build(); |
| 119 | + } |
| 120 | + |
| 121 | + private class PaginatedOperationWithResultKeyAndMoreResultsResponseFetcher implements |
| 122 | + AsyncPageFetcher<PaginatedOperationWithResultKeyAndMoreResultsResponse> { |
| 123 | + @Override |
| 124 | + public boolean hasNextPage(final PaginatedOperationWithResultKeyAndMoreResultsResponse previousPage) { |
| 125 | + return previousPage.truncated() != null && previousPage.truncated().booleanValue(); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public CompletableFuture<PaginatedOperationWithResultKeyAndMoreResultsResponse> nextPage( |
| 130 | + final PaginatedOperationWithResultKeyAndMoreResultsResponse previousPage) { |
| 131 | + if (previousPage == null) { |
| 132 | + return client.paginatedOperationWithResultKeyAndMoreResults(firstRequest); |
| 133 | + } |
| 134 | + return client.paginatedOperationWithResultKeyAndMoreResults(firstRequest.toBuilder() |
| 135 | + .nextToken(previousPage.nextToken()).build()); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments