diff --git a/sdk/core/azure-core/src/samples/java/com/azure/core/http/rest/PagedFluxBaseJavaDocCodeSnippets.java b/sdk/core/azure-core/src/samples/java/com/azure/core/http/rest/PagedFluxBaseJavaDocCodeSnippets.java index 2f08fa7dd1d83..1d05c5af77801 100644 --- a/sdk/core/azure-core/src/samples/java/com/azure/core/http/rest/PagedFluxBaseJavaDocCodeSnippets.java +++ b/sdk/core/azure-core/src/samples/java/com/azure/core/http/rest/PagedFluxBaseJavaDocCodeSnippets.java @@ -3,13 +3,20 @@ package com.azure.core.http.rest; +import com.azure.core.http.HttpHeaders; +import com.azure.core.http.HttpMethod; +import com.azure.core.http.HttpRequest; import org.reactivestreams.Subscription; import reactor.core.CoreSubscriber; import reactor.core.publisher.BaseSubscriber; import reactor.core.publisher.Mono; +import java.util.List; +import java.util.UUID; import java.util.function.Function; import java.util.function.Supplier; +import java.util.stream.Collectors; +import java.util.stream.IntStream; /** * Code snippets for {@link PagedFluxBase} @@ -24,18 +31,19 @@ public void classDocSnippet() { // BEGIN: com.azure.core.http.rest.pagedfluxbase.items pagedFluxBase .log() - .subscribe(item -> System.out.println("Processing item " + item), - error -> System.err.println("Error occurred " + error), - () -> System.out.println("Completed processing.")); + .subscribe(item -> System.out.println("Processing item with value: " + item), + error -> System.err.println("An error occurred: " + error), + () -> System.out.println("Processing complete.")); // END: com.azure.core.http.rest.pagedfluxbase.items // BEGIN: com.azure.core.http.rest.pagedfluxbase.pages pagedFluxBase .byPage() .log() - .subscribe(page -> System.out.println("Processing page containing " + page.getItems()), - error -> System.err.println("Error occurred " + error), - () -> System.out.println("Completed processing.")); + .subscribe(page -> System.out.printf("Processing page containing item values: %s%n", + page.getElements().stream().map(String::valueOf).collect(Collectors.joining(", "))), + error -> System.err.println("An error occurred: " + error), + () -> System.out.println("Processing complete.")); // END: com.azure.core.http.rest.pagedfluxbase.pages // BEGIN: com.azure.core.http.rest.pagedfluxbase.pagesWithContinuationToken @@ -45,9 +53,10 @@ public void classDocSnippet() { .log() .doOnSubscribe(ignored -> System.out.println( "Subscribed to paged flux processing pages starting from: " + continuationToken)) - .subscribe(page -> System.out.println("Processing page containing " + page.getItems()), - error -> System.err.println("Error occurred " + error), - () -> System.out.println("Completed processing.")); + .subscribe(page -> System.out.printf("Processing page containing item values: %s%n", + page.getElements().stream().map(String::valueOf).collect(Collectors.joining(", "))), + error -> System.err.println("An error occurred: " + error), + () -> System.out.println("Processing complete.")); // END: com.azure.core.http.rest.pagedfluxbase.pagesWithContinuationToken } @@ -91,9 +100,10 @@ public void byPageSnippet() { .log() .doOnSubscribe(ignoredVal -> System.out.println( "Subscribed to paged flux processing pages starting from first page")) - .subscribe(page -> System.out.println("Processing page containing " + page.getItems()), - error -> System.err.println("Error occurred " + error), - () -> System.out.println("Completed processing.")); + .subscribe(page -> System.out.printf("Processing page containing item values: %s%n", + page.getElements().stream().map(String::valueOf).collect(Collectors.joining(", "))), + error -> System.err.println("An error occurred: " + error), + () -> System.out.println("Processing complete.")); // END: com.azure.core.http.rest.pagedfluxbase.bypage // BEGIN: com.azure.core.http.rest.pagedfluxbase.bypage#String @@ -103,9 +113,10 @@ public void byPageSnippet() { .log() .doOnSubscribe(ignoredVal -> System.out.println( "Subscribed to paged flux processing page starting from " + continuationToken)) - .subscribe(page -> System.out.println("Processing page containing " + page.getItems()), - error -> System.err.println("Error occurred " + error), - () -> System.out.println("Completed processing.")); + .subscribe(page -> System.out.printf("Processing page containing item values: %s%n", + page.getElements().stream().map(String::valueOf).collect(Collectors.joining(", "))), + error -> System.err.println("An error occurred: " + error), + () -> System.out.println("Processing complete.")); // END: com.azure.core.http.rest.pagedfluxbase.bypage#String } @@ -142,25 +153,50 @@ protected void hookOnComplete() { * @return A continuation token */ private String getContinuationToken() { - return null; + return UUID.randomUUID().toString(); } /** - * Implementation not provided + * Retrieves the next page from a paged API. * * @param continuationToken Token to fetch the next page * @return A {@link Mono} of {@link PagedResponse} containing items of type {@code Integer} */ private Mono> getNextPage(String continuationToken) { - return null; + return getPage(continuationToken); } /** - * Implementation not provided + * Retrieves the initial page from a paged API. * * @return A {@link Mono} of {@link PagedResponse} containing items of type {@code Integer} */ private Mono> getFirstPage() { - return null; + return getPage(null); + } + + /** + * Retrieves a page from a paged API. + * + * @param continuationToken Token to fetch the next page, if {@code null} the first page is retrieved. + * @return A {@link Mono} of {@link PagedResponse} containing items of type {@code Integer} + */ + private Mono> getPage(String continuationToken) { + // Given this isn't calling an actual API we will arbitrarily generate a continuation token or end paging. + boolean lastPage = Math.random() > 0.5; + + // If it is the last page there should be no additional continuation tokens returned. + String nextContinuationToken = lastPage ? null : UUID.randomUUID().toString(); + + // Arbitrarily begin the next page of integers. + int elementCount = (int) Math.ceil(Math.random() * 15); + List elements = IntStream.range(elementCount, elementCount + elementCount) + .map(val -> (int) (Math.random() * val)) + .boxed() + .collect(Collectors.toList()); + + // This is a rough approximation of a service response. + return Mono.just(new PagedResponseBase(new HttpRequest(HttpMethod.GET, "https://requestUrl.com"), + 200, new HttpHeaders(), elements, nextContinuationToken, null)); } }