@@ -50,12 +50,14 @@ public final class PollOperation {
5050 return pollingContext -> {
5151 PollingState pollingState = PollingState .from (serializerAdapter , pollingContext );
5252 if (pollingState .getOperationStatus ().isComplete ()) {
53- return pollResponseMonoFromPollingState (serializerAdapter , pollResultType , pollingState );
53+ return Mono .defer (
54+ () -> Mono .just (pollResponseFromPollingState (serializerAdapter , pollResultType , pollingState )));
5455 } else {
5556 // InProgress|NonTerminal-Status
5657 return doSinglePoll (pipeline , pollingState , context ).flatMap (updatedState -> {
5758 updatedState .store (pollingContext );
58- return pollResponseMonoFromPollingState (serializerAdapter , pollResultType , updatedState );
59+ return Mono .defer (
60+ () -> Mono .just (pollResponseFromPollingState (serializerAdapter , pollResultType , updatedState )));
5961 });
6062 }
6163 };
@@ -116,37 +118,6 @@ public static <T, U> Function<PollingContext<PollResult<T>>, Mono<U>> fetchResul
116118 };
117119 }
118120
119- /**
120- * Create a PollResponse indicating service error.
121- *
122- * @param opStatus the long-running-operation errored status
123- * @param error the error description
124- * @param <T> the poll result type
125- * @return PollResponse
126- */
127- private static <T > Mono <PollResponse <PollResult <T >>> errorPollResponseMono (LongRunningOperationStatus opStatus ,
128- Error error ) {
129- PollResult <T > pollResult = new PollResult <>(new PollResult .Error (error .getMessage (),
130- error .getResponseStatusCode (), new HttpHeaders (error .getResponseHeaders ()), error .getResponseBody ()));
131- return Mono .just (new PollResponse <>(opStatus , pollResult ));
132- }
133-
134- /**
135- * Create a PollResponse indicating succeeded or in-progress LRO.
136- *
137- * @param serializerAdapter the serializer for any encoding and decoding
138- * @param opStatus the long-running-operation succeeded or in-progress status
139- * @param pollResponseBody the poll response body
140- * @param pollResultType the poll result type
141- * @param <T> the poll result type
142- * @return PollResponse
143- */
144- private static <T > Mono <PollResponse <PollResult <T >>> pollResponseMono (SerializerAdapter serializerAdapter ,
145- LongRunningOperationStatus opStatus , String pollResponseBody , Type pollResultType , Duration pollDelay ) {
146- T result = deserialize (serializerAdapter , pollResponseBody , pollResultType );
147- return Mono .just (new PollResponse <>(opStatus , new PollResult <T >(result ), pollDelay ));
148- }
149-
150121 /**
151122 * Do a poll to retrieve the LRO status.
152123 *
@@ -170,34 +141,72 @@ private static Mono<PollingState> doSinglePoll(HttpPipeline pipeline, PollingSta
170141 .fromSupplier (() -> pollingState .update (response .getStatusCode (), response .getHeaders (), null ))));
171142 }
172143
173- private static <T > Mono <PollResponse <PollResult <T >>> pollResponseMonoFromPollingState (
174- SerializerAdapter serializerAdapter , Type pollResultType , PollingState pollingState ) {
144+ /**
145+ * Gets the latest poll response from PollingState.
146+ *
147+ * @param serializerAdapter the serializer for any encoding and decoding
148+ * @param pollResultType the poll result type
149+ * @param pollingState the polling state
150+ * @param <T> the poll result type
151+ * @return the latest poll response
152+ */
153+ static <T > PollResponse <PollResult <T >> pollResponseFromPollingState (SerializerAdapter serializerAdapter ,
154+ Type pollResultType , PollingState pollingState ) {
175155 if (pollingState .getOperationStatus ().isComplete ()) {
176156 if (pollingState .getOperationStatus () == LongRunningOperationStatus .FAILED
177157 || pollingState .getOperationStatus () == LRO_CANCELLED ) {
178158 // Failed|Cancelled
179159 Error lroInitError = pollingState .getSynchronouslyFailedLroError ();
180160 if (lroInitError != null ) {
181- return errorPollResponseMono (pollingState .getOperationStatus (), lroInitError );
161+ return errorPollResponse (pollingState .getOperationStatus (), lroInitError );
182162 }
183163 Error pollError = pollingState .getPollError ();
184164 if (pollError != null ) {
185- return errorPollResponseMono (pollingState .getOperationStatus (), pollError );
165+ return errorPollResponse (pollingState .getOperationStatus (), pollError );
186166 }
187167 throw new IllegalStateException (
188168 "Either LroError or PollError must" + "be set when OperationStatus is in Failed|Cancelled State." );
189169 } else {
190170 // Succeeded
191- return pollResponseMono (serializerAdapter , pollingState .getOperationStatus (),
171+ return pollResponse (serializerAdapter , pollingState .getOperationStatus (),
192172 pollingState .getLastResponseBody (), pollResultType , pollingState .getPollDelay ());
193173 }
194174 } else {
195175 // InProgress|NonTerminal-Status
196- return pollResponseMono (serializerAdapter , pollingState .getOperationStatus (),
176+ return pollResponse (serializerAdapter , pollingState .getOperationStatus (),
197177 pollingState .getLastResponseBody (), pollResultType , pollingState .getPollDelay ());
198178 }
199179 }
200180
181+ /**
182+ * Create a PollResponse indicating succeeded or in-progress LRO.
183+ *
184+ * @param serializerAdapter the serializer for any encoding and decoding
185+ * @param operationStatus the long-running-operation succeeded or in-progress status
186+ * @param pollResponseBody the poll response body
187+ * @param pollResultType the poll result type
188+ * @param <T> the poll result type
189+ * @return PollResponse
190+ */
191+ static <T > PollResponse <PollResult <T >> pollResponse (SerializerAdapter serializerAdapter ,
192+ LongRunningOperationStatus operationStatus , String pollResponseBody , Type pollResultType , Duration pollDelay ) {
193+ T result = deserialize (serializerAdapter , pollResponseBody , pollResultType );
194+ return new PollResponse <>(operationStatus , new PollResult <>(result ), pollDelay );
195+ }
196+
197+ /**
198+ * Create a PollResponse indicating service error.
199+ *
200+ * @param operationStatus the long-running-operation errored status
201+ * @param error the error description
202+ * @param <T> the poll result type
203+ * @return PollResponse
204+ */
205+ static <T > PollResponse <PollResult <T >> errorPollResponse (LongRunningOperationStatus operationStatus , Error error ) {
206+ return new PollResponse <>(operationStatus , new PollResult <>(new PollResult .Error (error .getMessage (),
207+ error .getResponseStatusCode (), new HttpHeaders (error .getResponseHeaders ()), error .getResponseBody ())));
208+ }
209+
201210 /**
202211 * Decorate the request.
203212 *
0 commit comments