Skip to content

Commit 845f2c7

Browse files
authored
Update core v2 poller types (Azure#45282)
1 parent c82aa36 commit 845f2c7

File tree

13 files changed

+114
-25
lines changed

13 files changed

+114
-25
lines changed

eng/versioning/version_client.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ io.clientcore:annotation-processor-test;1.0.0-beta.1;1.0.0-beta.1
507507
# In the pom, the version update tag after the version should name the unreleased package and the dependency version:
508508
# <!-- {x-version-update;unreleased_com.azure:azure-core;dependency} -->
509509

510-
unreleased_io.clientcore:core;1.0.0-beta.9
510+
unreleased_io.clientcore:core;1.0.0-beta.10
511511
unreleased_io.clientcore:annotation-processor;1.0.0-beta.3
512512
unreleased_com.azure.v2:azure-core;2.0.0-beta.1
513513

sdk/clientcore/annotation-processor-test/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
<dependency>
5656
<groupId>io.clientcore</groupId>
5757
<artifactId>core</artifactId>
58-
<version>1.0.0-beta.9</version> <!-- {x-version-update;unreleased_io.clientcore:core;dependency} -->
58+
<version>1.0.0-beta.10</version> <!-- {x-version-update;unreleased_io.clientcore:core;dependency} -->
5959
</dependency>
6060
<dependency>
6161
<groupId>io.clientcore</groupId>
@@ -67,7 +67,7 @@
6767
<dependency>
6868
<groupId>io.clientcore</groupId>
6969
<artifactId>core</artifactId>
70-
<version>1.0.0-beta.9</version> <!-- {x-version-update;unreleased_io.clientcore:core;dependency} -->
70+
<version>1.0.0-beta.10</version> <!-- {x-version-update;unreleased_io.clientcore:core;dependency} -->
7171
<type>test-jar</type>
7272
<scope>test</scope>
7373
</dependency>

sdk/clientcore/annotation-processor/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
<dependency>
114114
<groupId>io.clientcore</groupId>
115115
<artifactId>core</artifactId>
116-
<version>1.0.0-beta.9</version> <!-- {x-version-update;unreleased_io.clientcore:core;dependency} -->
116+
<version>1.0.0-beta.10</version> <!-- {x-version-update;unreleased_io.clientcore:core;dependency} -->
117117
</dependency>
118118

119119
<!-- Unit Test -->

sdk/core-v2/azure-core-test/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
<dependency>
7272
<groupId>io.clientcore</groupId>
7373
<artifactId>core</artifactId>
74-
<version>1.0.0-beta.9</version> <!-- {x-version-update;unreleased_io.clientcore:core;dependency} -->
74+
<version>1.0.0-beta.10</version> <!-- {x-version-update;unreleased_io.clientcore:core;dependency} -->
7575
</dependency>
7676
<dependency>
7777
<groupId>com.azure.v2</groupId>

sdk/core-v2/azure-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
<dependency>
7676
<groupId>io.clientcore</groupId>
7777
<artifactId>core</artifactId>
78-
<version>1.0.0-beta.9</version> <!-- {x-version-update;unreleased_io.clientcore:core;dependency} -->
78+
<version>1.0.0-beta.10</version> <!-- {x-version-update;unreleased_io.clientcore:core;dependency} -->
7979
</dependency>
8080

8181
<dependency>

sdk/core-v2/azure-core/src/main/java/com/azure/v2/core/http/polling/ChainedPollingStrategy.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import io.clientcore.core.http.models.Response;
77
import io.clientcore.core.instrumentation.logging.ClientLogger;
8-
import io.clientcore.core.models.binarydata.BinaryData;
98

109
import java.lang.reflect.Type;
1110
import java.util.Collections;
@@ -42,7 +41,7 @@ public ChainedPollingStrategy(List<PollingStrategy<T, U>> strategies) {
4241
}
4342

4443
@Override
45-
public boolean canPoll(Response<BinaryData> initialResponse) {
44+
public boolean canPoll(Response<T> initialResponse) {
4645
// Find the first strategy that can poll in series so that
4746
// pollableStrategy is only set once
4847
for (PollingStrategy<T, U> strategy : pollingStrategies) {
@@ -71,7 +70,7 @@ public U getResult(PollingContext<T> context, Type resultType) {
7170
* @throws NullPointerException if {@link #canPoll(Response)} is not called prior to this, or if it returns false.
7271
*/
7372
@Override
74-
public PollResponse<T> onInitialResponse(Response<BinaryData> response, PollingContext<T> pollingContext,
73+
public PollResponse<T> onInitialResponse(Response<T> response, PollingContext<T> pollingContext,
7574
Type pollResponseType) {
7675
return pollableStrategy.onInitialResponse(response, pollingContext, pollResponseType);
7776
}

sdk/core-v2/azure-core/src/main/java/com/azure/v2/core/http/polling/DefaultPollingStrategy.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import io.clientcore.core.http.models.Response;
77
import io.clientcore.core.http.models.RequestContext;
88
import io.clientcore.core.http.pipeline.HttpPipeline;
9-
import io.clientcore.core.models.binarydata.BinaryData;
109
import io.clientcore.core.serialization.json.JsonSerializer;
1110

1211
import java.lang.reflect.Type;
@@ -106,12 +105,12 @@ public U getResult(PollingContext<T> pollingContext, Type resultType) {
106105
}
107106

108107
@Override
109-
public boolean canPoll(Response<BinaryData> initialResponse) {
108+
public boolean canPoll(Response<T> initialResponse) {
110109
return chainedPollingStrategy.canPoll(initialResponse);
111110
}
112111

113112
@Override
114-
public PollResponse<T> onInitialResponse(Response<BinaryData> response, PollingContext<T> pollingContext,
113+
public PollResponse<T> onInitialResponse(Response<T> response, PollingContext<T> pollingContext,
115114
Type pollResponseType) {
116115
return chainedPollingStrategy.onInitialResponse(response, pollingContext, pollResponseType);
117116
}

sdk/core-v2/azure-core/src/main/java/com/azure/v2/core/http/polling/LocationPollingStrategy.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ public LocationPollingStrategy(PollingStrategyOptions pollingStrategyOptions) {
118118
}
119119

120120
@Override
121-
public boolean canPoll(Response<BinaryData> initialResponse) {
121+
public boolean canPoll(Response<T> initialResponse) {
122122
return locationCanPoll(initialResponse, endpoint, LOGGER);
123123
}
124124

125125
@Override
126-
public PollResponse<T> onInitialResponse(Response<BinaryData> response, PollingContext<T> pollingContext,
126+
public PollResponse<T> onInitialResponse(Response<T> response, PollingContext<T> pollingContext,
127127
Type pollResponseType) {
128128
HttpHeader locationHeader = response.getHeaders().get(HttpHeaderName.LOCATION);
129129
if (locationHeader != null) {
@@ -141,12 +141,15 @@ public PollResponse<T> onInitialResponse(Response<BinaryData> response, PollingC
141141
return new PollResponse<>(LongRunningOperationStatus.IN_PROGRESS,
142142
PollingUtils.convertResponse(response.getValue(), serializer, pollResponseType), retryAfter);
143143
}
144+
Response<BinaryData> binaryDataResponse = new Response<>(response.getRequest(), response.getStatusCode(),
145+
response.getHeaders(), BinaryData.fromObject(response.getValue()));
144146

145147
throw LOGGER.throwableAtError()
146148
.addKeyValue("http.response.status_code", response.getStatusCode())
147149
.addKeyValue("http.response.header.location", locationHeader == null ? null : locationHeader.getValue())
148150
.addKeyValue("http.response.body.content", serializeResponse(response.getValue(), serializer).toString())
149-
.log("Operation failed or cancelled", message -> new HttpResponseException(message, response, null));
151+
.log("Operation failed or cancelled",
152+
message -> new HttpResponseException(message, binaryDataResponse, null));
150153
}
151154

152155
@Override

sdk/core-v2/azure-core/src/main/java/com/azure/v2/core/http/polling/OperationResourcePollingStrategy.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ public OperationResourcePollingStrategy(HttpHeaderName operationLocationHeaderNa
129129
}
130130

131131
@Override
132-
public boolean canPoll(Response<BinaryData> initialResponse) {
132+
public boolean canPoll(Response<T> initialResponse) {
133133
return operationResourceCanPoll(initialResponse, operationLocationHeaderName, endpoint, LOGGER);
134134
}
135135

136136
@Override
137-
public PollResponse<T> onInitialResponse(Response<BinaryData> response, PollingContext<T> pollingContext,
137+
public PollResponse<T> onInitialResponse(Response<T> response, PollingContext<T> pollingContext,
138138
Type pollResponseType) {
139139
HttpHeader operationLocationHeader = response.getHeaders().get(operationLocationHeaderName);
140140
HttpHeader locationHeader = response.getHeaders().get(HttpHeaderName.LOCATION);
@@ -160,13 +160,17 @@ public PollResponse<T> onInitialResponse(Response<BinaryData> response, PollingC
160160
PollingUtils.convertResponse(response.getValue(), serializer, pollResponseType), retryAfter);
161161
}
162162

163+
Response<BinaryData> binaryDataResponse = new Response<>(response.getRequest(), response.getStatusCode(),
164+
response.getHeaders(), BinaryData.fromObject(response.getValue()));
165+
163166
throw LOGGER.throwableAtError()
164167
.addKeyValue("http.response.status_code", response.getStatusCode())
165168
.addKeyValue("http.response.body.content", serializeResponse(response.getValue(), serializer).toString())
166169
.addKeyValue("operationLocationHeaderName", operationLocationHeaderName.getValue())
167170
.addKeyValue("operationLocationHeaderValue",
168171
operationLocationHeader == null ? null : operationLocationHeader.getValue())
169-
.log("Operation failed or cancelled", message -> new HttpResponseException(message, response, null));
172+
.log("Operation failed or cancelled",
173+
message -> new HttpResponseException(message, binaryDataResponse, null));
170174
}
171175

172176
@Override

sdk/core-v2/azure-core/src/main/java/com/azure/v2/core/http/polling/Poller.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33

44
package com.azure.v2.core.http.polling;
55

6+
import io.clientcore.core.http.models.Response;
7+
8+
import java.lang.reflect.Type;
69
import java.time.Duration;
710
import java.util.concurrent.TimeoutException;
11+
import java.util.function.BiFunction;
12+
import java.util.function.Function;
13+
import java.util.function.Supplier;
814

915
/**
1016
* 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) {
159165
return this;
160166
}
161167

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+
162244
}

0 commit comments

Comments
 (0)