Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PagedFluxCore Javadoc Code Snippets #13393

Merged
merged 3 commits into from
Jul 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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<PagedResponse<Integer>> 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<PagedResponse<Integer>> 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<PagedResponse<Integer>> 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<Integer> 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<Void, Integer>(new HttpRequest(HttpMethod.GET, "https://requestUrl.com"),
200, new HttpHeaders(), elements, nextContinuationToken, null));
}
}