Skip to content
Merged
Show file tree
Hide file tree
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 @@ -392,7 +392,7 @@

<!-- Don't enforce non-static ClientLogger instances in azure-core-mgmt PollerFactory and PollingState types-->
<suppress checks="com\.azure\.tools\.checkstyle\.checks\.(ThrowFromClientLoggerCheck|GoodLoggingCheck)"
files="com.azure.core.management.implementation.polling.PollerFactory.java"/>
files="com.azure.core.management.implementation.polling.PollOperation.java"/>
<suppress checks="com\.azure\.tools\.checkstyle\.checks\.(ThrowFromClientLoggerCheck|GoodLoggingCheck)"
files="com.azure.core.management.implementation.polling.PollingState.java"/>

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,142 +7,28 @@
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpRequest;
import com.azure.core.http.HttpResponse;
import com.azure.core.http.rest.Response;
import com.azure.core.management.polling.PollResult;
import com.azure.core.util.FluxUtil;
import com.azure.core.util.logging.ClientLogger;
import com.azure.core.util.polling.LongRunningOperationStatus;
import com.azure.core.util.polling.PollResponse;
import com.azure.core.util.polling.PollerFlux;
import com.azure.core.util.polling.PollingContext;
import com.azure.core.util.serializer.SerializerAdapter;
import com.azure.core.util.serializer.SerializerEncoding;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
* Factory to create PollerFlux for Azure resource manager (ARM) long-running-operation (LRO).
* Poll operation for Azure resource manager (ARM) long-running-operation (LRO).
*/
public final class PollerFactory {
private static final ClientLogger LOGGER = new ClientLogger(PollerFactory.class);
public final class PollOperation {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The remaining public method (used by polling.PollerFactory) in the class is for pollingOperation, cancelOperation and fetchResultOperation, hence changing the class name to PollOperation.

Let me know if any suggestion.

private static final ClientLogger LOGGER = new ClientLogger(PollOperation.class);
private static final LongRunningOperationStatus LRO_CANCELLED = LongRunningOperationStatus.fromString("Cancelled",
true);

/**
* Creates a PollerFlux with default ARM LRO init operation.
*
* @param serializerAdapter the serializer for any encoding and decoding
* @param pipeline the HttpPipeline for making any Http request (e.g. poll)
* @param pollResultType the type of the poll result, if no result is expecting then this should be Void.class
* @param finalResultType the type of the final result, if no result is expecting then this should be Void.class
* @param defaultPollInterval the default poll interval to use if service does not return retry-after
* @param lroInitMono the Mono on subscribe send the service request to initiate the long-running-operation
* @param <T> the type of poll result
* @param <U> the type of final result
* @return PollerFlux
*/
public static <T, U> PollerFlux<PollResult<T>, U> create(
SerializerAdapter serializerAdapter,
HttpPipeline pipeline,
Type pollResultType,
Type finalResultType,
Duration defaultPollInterval,
Mono<Response<Flux<ByteBuffer>>> lroInitMono) {
Objects.requireNonNull(serializerAdapter, "'serializerAdapter' cannot be null.");
Objects.requireNonNull(pipeline, "'pipeline' cannot be null.");
Objects.requireNonNull(pollResultType, "'pollResultType' cannot be null.");
Objects.requireNonNull(finalResultType, "'finalResultType' cannot be null.");
Objects.requireNonNull(defaultPollInterval, "'defaultPollInterval' cannot be null.");
Objects.requireNonNull(lroInitMono, "'lroInitMono' cannot be null.");
Function<PollingContext<PollResult<T>>, Mono<PollResponse<PollResult<T>>>> defaultLroInitOperation =
context -> lroInitMono.flatMap(response -> FluxUtil.collectBytesInByteBufferStream(response.getValue())
.map(contentBytes -> {
String content = new String(contentBytes, StandardCharsets.UTF_8);
PollingState state = PollingState.create(serializerAdapter,
response.getRequest(),
response.getStatusCode(),
response.getHeaders(),
content);
state.store(context);
T result = deserialize(serializerAdapter, content, pollResultType);
return new PollResponse<>(state.getOperationStatus(), new PollResult<>(result));
}));
return PollerFlux.create(defaultPollInterval,
defaultLroInitOperation,
pollFunction(serializerAdapter, pipeline, pollResultType),
cancelFunction(),
fetchResultFunction(serializerAdapter, pipeline, finalResultType));
}

/**
* Creates a PollerFlux.
*
* @param serializerAdapter the serializer for any encoding and decoding
* @param pipeline the HttpPipeline for making any Http request (e.g. poll)
* @param pollResultType the type of the poll result, if no result is expecting then this should be Void.class
* @param finalResultType the type of the final result, if no result is expecting then this should be Void.class
* @param defaultPollInterval the default poll interval to use if service does not return retry-after
* @param lroInitOperation the function upon invoking should initiate the long-running-operation
* @param <T> the type of poll result
* @param <U> the type of final result
* @return PollerFlux
*/
public static <T, U> PollerFlux<PollResult<T>, U> create(
SerializerAdapter serializerAdapter,
HttpPipeline pipeline,
Type pollResultType,
Type finalResultType,
Duration defaultPollInterval,
Function<PollingContext<PollResult<T>>, Mono<PollResult<T>>> lroInitOperation) {
Objects.requireNonNull(serializerAdapter, "'serializerAdapter' cannot be null.");
Objects.requireNonNull(pipeline, "'pipeline' cannot be null.");
Objects.requireNonNull(pollResultType, "'pollResultType' cannot be null.");
Objects.requireNonNull(finalResultType, "'finalResultType' cannot be null.");
Objects.requireNonNull(defaultPollInterval, "'defaultPollInterval' cannot be null.");
Objects.requireNonNull(lroInitOperation, "'lroInitOperation' cannot be null.");

return new PollerFlux<>(defaultPollInterval,
lroInitOperation,
pollFunction(serializerAdapter, pipeline, pollResultType),
cancelFunction(),
fetchResultFunction(serializerAdapter, pipeline, finalResultType));
}

/**
* Dehydrate a PollerFlux from a string.
*
* @param serializerAdapter the serializer for any encoding and decoding
* @param pipeline the HttpPipeline for making any Http request (e.g. poll)
* @param pollResultType the type of the poll result, if no result is expecting then this should be Void.class
* @param finalResultType the type of the final result, if no result is expecting then this should be Void.class
* @param defaultPollInterval the default poll interval to use if service does not return retry-after
* @param pollingStateStr the string to dehydrate PollerFlux from
* @param <T> the type of poll result
* @param <U> the type of final result
* @return PollerFlux
*/
public static <T, U> PollerFlux<PollResult<T>, U> create(SerializerAdapter serializerAdapter,
HttpPipeline pipeline,
Type pollResultType,
Type finalResultType,
Duration defaultPollInterval,
String pollingStateStr) {
return create(serializerAdapter, pipeline, pollResultType, finalResultType, defaultPollInterval,
context -> {
PollingState.from(serializerAdapter, pollingStateStr).store(context);
return Mono.empty();
});
}

/**
* Get a Function that polls provisioning state of ARM resource.
*
Expand All @@ -152,7 +38,7 @@ public static <T, U> PollerFlux<PollResult<T>, U> create(SerializerAdapter seria
* @param <T> the type of poll result type
* @return the ARM poll function
*/
private static <T> Function<PollingContext<PollResult<T>>, Mono<PollResponse<PollResult<T>>>> pollFunction(
public static <T> Function<PollingContext<PollResult<T>>, Mono<PollResponse<PollResult<T>>>> pollFunction(
SerializerAdapter serializerAdapter,
HttpPipeline pipeline,
Type pollResultType) {
Expand Down Expand Up @@ -198,7 +84,7 @@ private static <T> Function<PollingContext<PollResult<T>>, Mono<PollResponse<Pol
* @param <T> the type of poll result type
* @return cancel Function
*/
private static <T>
public static <T>
BiFunction<PollingContext<PollResult<T>>, PollResponse<PollResult<T>>, Mono<PollResult<T>>> cancelFunction() {
return new BiFunction<PollingContext<PollResult<T>>, PollResponse<PollResult<T>>, Mono<PollResult<T>>>() {
@Override
Expand All @@ -219,7 +105,7 @@ public Mono<PollResult<T>> apply(PollingContext<PollResult<T>> context,
* @param <U> the poll result type
* @return retrieve final LRO result Function
*/
private static <T, U> Function<PollingContext<PollResult<T>>, Mono<U>> fetchResultFunction(
public static <T, U> Function<PollingContext<PollResult<T>>, Mono<U>> fetchResultFunction(
SerializerAdapter serializerAdapter,
HttpPipeline pipeline,
Type finalResultType) {
Expand Down Expand Up @@ -307,7 +193,7 @@ private static Mono<PollingState> doSinglePoll(HttpPipeline pipeline, PollingSta
* @return decoded value
*/
@SuppressWarnings("unchecked")
private static <U> U deserialize(SerializerAdapter serializerAdapter, String value, Type type) {
public static <U> U deserialize(SerializerAdapter serializerAdapter, String value, Type type) {
if (value == null || value.equalsIgnoreCase("")) {
LOGGER.info("Ignoring decoding of null or empty value to:" + type.getTypeName());
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public <T> void store(PollingContext<T> context) {
/**
* @return the current status of the long-running-operation.
*/
LongRunningOperationStatus getOperationStatus() {
public LongRunningOperationStatus getOperationStatus() {
switch (this.pollingType) {
case AZURE_ASYNC_OPERATION_POLL:
return toLongRunningOperationStatus(this.azureAsyncOperationData.getProvisioningState());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public PollResult(Error error) {
*
* @return the value
*/
public T value() {
public T getValue() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change according to java sdk guideline.

return this.value;
}

Expand All @@ -47,7 +47,7 @@ public T value() {
*
* @return the error
*/
public Error error() {
public Error getError() {
return this.error;
}

Expand Down
Loading