|
3 | 3 |
|
4 | 4 | package com.azure.v2.core.http.polling; |
5 | 5 |
|
| 6 | +import io.clientcore.core.http.models.Response; |
| 7 | + |
| 8 | +import java.lang.reflect.Type; |
6 | 9 | import java.time.Duration; |
7 | 10 | import java.util.concurrent.TimeoutException; |
| 11 | +import java.util.function.BiFunction; |
| 12 | +import java.util.function.Function; |
| 13 | +import java.util.function.Supplier; |
8 | 14 |
|
9 | 15 | /** |
10 | 16 | * A type that offers API that simplifies the task of executing long-running operations against an Azure service. |
@@ -159,4 +165,80 @@ default Poller<T, U> setPollInterval(Duration pollInterval) { |
159 | 165 | return this; |
160 | 166 | } |
161 | 167 |
|
| 168 | + /** |
| 169 | + * Creates default SyncPoller. |
| 170 | + * |
| 171 | + * @param pollInterval the polling interval. |
| 172 | + * @param syncActivationOperation the operation to synchronously activate (start) the long-running operation, this |
| 173 | + * operation will be called with a new {@link PollingContext}. |
| 174 | + * @param pollOperation the operation to poll the current state of long-running operation, this parameter is |
| 175 | + * required and the operation will be called with current {@link PollingContext}. |
| 176 | + * @param cancelOperation a {@link Function} that represents the operation to cancel the long-running operation if |
| 177 | + * service supports cancellation, this parameter is required and if service does not support cancellation then the |
| 178 | + * implementer should throw an exception with an error message indicating absence of cancellation support, the |
| 179 | + * operation will be called with current {@link PollingContext}. |
| 180 | + * @param fetchResultOperation a {@link Function} that represents the operation to retrieve final result of the |
| 181 | + * long-running operation if service support it, this parameter is required and operation will be called current |
| 182 | + * {@link PollingContext}, if service does not have an api to fetch final result and if final result is same as |
| 183 | + * final poll response value then implementer can choose to simply return value from provided final poll response. |
| 184 | + * @param <T> The type of poll response value. |
| 185 | + * @param <U> The type of the final result of long-running operation. |
| 186 | + * @return new {@link Poller} instance. |
| 187 | + * @throws NullPointerException if {@code pollInterval}, {@code syncActivationOperation}, {@code pollOperation}, |
| 188 | + * {@code cancelOperation} or {@code fetchResultOperation} is {@code null}. |
| 189 | + * @throws IllegalArgumentException if {@code pollInterval} is zero or negative. |
| 190 | + */ |
| 191 | + static <T, U> Poller<T, U> createPoller(Duration pollInterval, |
| 192 | + Function<PollingContext<T>, PollResponse<T>> syncActivationOperation, |
| 193 | + Function<PollingContext<T>, PollResponse<T>> pollOperation, |
| 194 | + BiFunction<PollingContext<T>, PollResponse<T>, T> cancelOperation, |
| 195 | + Function<PollingContext<T>, U> fetchResultOperation) { |
| 196 | + return new SimplePoller<>(pollInterval, syncActivationOperation, pollOperation, cancelOperation, |
| 197 | + fetchResultOperation); |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * Creates PollerFlux. |
| 202 | + * <p> |
| 203 | + * This method uses a {@link PollingStrategy} to poll the status of a long-running operation after the |
| 204 | + * activation operation is invoked. See {@link PollingStrategy} for more details of known polling strategies and |
| 205 | + * how to create a custom strategy. |
| 206 | + * |
| 207 | + * @param pollInterval the polling interval |
| 208 | + * @param initialOperation the activation operation to activate (start) the long-running operation. This operation |
| 209 | + * will be invoked at most once across all subscriptions. This parameter is required. If there is no specific |
| 210 | + * activation work to be done then invocation should return null, this operation will be called with a new |
| 211 | + * {@link PollingContext}. |
| 212 | + * @param strategy a known synchronous strategy for polling a long-running operation in Azure |
| 213 | + * @param pollResponseType the {@link Type} of the response type from a polling call, or BinaryData if raw |
| 214 | + * response body should be kept. This should match the generic parameter {@link U}. |
| 215 | + * @param resultType the {@link Type} of the final result object to deserialize into, or BinaryData if raw |
| 216 | + * response body should be kept. This should match the generic parameter {@link U}. |
| 217 | + * @param <T> The type of poll response value. |
| 218 | + * @param <U> The type of the final result of long-running operation. |
| 219 | + * @return new {@link Poller} instance. |
| 220 | + * @throws NullPointerException if {@code pollInterval}, {@code initialOperation}, {@code strategy}, |
| 221 | + * {@code pollResponseType} or {@code resultType} is {@code null}. |
| 222 | + * @throws IllegalArgumentException if {@code pollInterval} is zero or negative. |
| 223 | + */ |
| 224 | + static <T, U> Poller<T, U> createPoller(Duration pollInterval, Supplier<Response<T>> initialOperation, |
| 225 | + PollingStrategy<T, U> strategy, Type pollResponseType, Type resultType) { |
| 226 | + Function<PollingContext<T>, PollResponse<T>> syncActivationOperation = pollingContext -> { |
| 227 | + Response<T> response = initialOperation.get(); |
| 228 | + if (!strategy.canPoll(response)) { |
| 229 | + throw new IllegalStateException("Cannot poll with strategy " + strategy.getClass().getSimpleName()); |
| 230 | + } |
| 231 | + |
| 232 | + return strategy.onInitialResponse(response, pollingContext, pollResponseType); |
| 233 | + }; |
| 234 | + |
| 235 | + Function<PollingContext<T>, PollResponse<T>> pollOperation |
| 236 | + = context -> strategy.poll(context, pollResponseType); |
| 237 | + BiFunction<PollingContext<T>, PollResponse<T>, T> cancelOperation = strategy::cancel; |
| 238 | + Function<PollingContext<T>, U> fetchResultOperation = context -> strategy.getResult(context, resultType); |
| 239 | + |
| 240 | + return createPoller(pollInterval, syncActivationOperation, pollOperation, cancelOperation, |
| 241 | + fetchResultOperation); |
| 242 | + } |
| 243 | + |
162 | 244 | } |
0 commit comments