|
26 | 26 | import com.squareup.javapoet.WildcardTypeName;
|
27 | 27 | import java.util.List;
|
28 | 28 | import java.util.Optional;
|
| 29 | +import java.util.concurrent.CompletableFuture; |
29 | 30 | import java.util.stream.Collectors;
|
30 | 31 | import javax.lang.model.element.Modifier;
|
31 | 32 |
|
@@ -228,38 +229,59 @@ public CodeBlock asyncExecutionHandler(OperationModel opModel) {
|
228 | 229 | : "";
|
229 | 230 | CodeBlock.Builder builder = CodeBlock.builder();
|
230 | 231 | if (opModel.hasEventStreamOutput()) {
|
231 |
| - builder.add("$T<$T, $T> asyncResponseTransformer = new $T<>(\n" |
232 |
| - + "asyncResponseHandler, responseHandler, eventResponseHandler, exceptionHandler);\n", |
233 |
| - ClassName.get(AsyncResponseTransformer.class), |
234 |
| - ClassName.get(SdkResponse.class), |
235 |
| - ClassName.get(Void.class), |
236 |
| - ClassName.get(EventStreamAsyncResponseTransformer.class)); |
| 232 | + ClassName eventStreamBaseClass = EventStreamUtils.create(poetExtensions, opModel).eventStreamBaseClass(); |
| 233 | + ParameterizedTypeName transformerType = ParameterizedTypeName.get(ClassName.get(EventStreamAsyncResponseTransformer.class), |
| 234 | + |
| 235 | + pojoResponseType, eventStreamBaseClass); |
| 236 | + builder.addStatement("$1T<$2T> future = new $1T<>()", |
| 237 | + ClassName.get(CompletableFuture.class), |
| 238 | + ClassName.get(Void.class)); |
| 239 | + builder.add("$T asyncResponseTransformer = $T.<$T, $T>builder()\n" + |
| 240 | + " .eventStreamResponseTransformer(asyncResponseHandler)\n" |
| 241 | + + " .eventUnmarshaller(eventResponseHandler)\n" |
| 242 | + + " .initialResponseUnmarshaller(responseHandler)\n" |
| 243 | + + " .exceptionUnmarshaller(exceptionHandler)\n" |
| 244 | + + " .future(future)\n" |
| 245 | + + " .executor(executor)\n" |
| 246 | + + " .build();", |
| 247 | + transformerType, |
| 248 | + ClassName.get(EventStreamAsyncResponseTransformer.class), |
| 249 | + pojoResponseType, |
| 250 | + eventStreamBaseClass); |
237 | 251 | }
|
238 | 252 | boolean isStreaming = opModel.hasStreamingOutput() || opModel.hasEventStreamOutput();
|
239 | 253 | String protocolFactory = opModel.hasEventStreamOutput() ? "jsonProtocolFactory" : "protocolFactory";
|
240 | 254 | String customerResponseHandler = opModel.hasEventStreamOutput() ? "asyncResponseHandler" : "asyncResponseTransformer";
|
241 |
| - return builder.add("\n\nreturn clientHandler.execute(new $T<$T, $T>()\n" + |
242 |
| - ".withMarshaller(new $T($L))\n" + |
243 |
| - ".withResponseHandler($L)\n" + |
244 |
| - ".withErrorResponseHandler(errorResponseHandler)\n" + |
245 |
| - asyncRequestBody + |
246 |
| - ".withInput($L)$L)$L;", |
247 |
| - ClientExecutionParams.class, |
248 |
| - requestType, |
249 |
| - opModel.hasEventStreamOutput() ? SdkResponse.class : pojoResponseType, |
250 |
| - marshaller, |
251 |
| - protocolFactory, |
252 |
| - opModel.hasEventStreamOutput() ? "voidResponseHandler" : "responseHandler", |
253 |
| - opModel.getInput().getVariableName(), |
254 |
| - isStreaming ? ", asyncResponseTransformer" : "", |
255 |
| - // If it's a streaming operation we also need to notify the handler on exception. |
256 |
| - isStreaming ? String.format(".whenComplete((r, e) -> {%n" |
257 |
| - + " if (e != null) {%n" |
258 |
| - + " %s.exceptionOccurred(e);%n" |
259 |
| - + " }%n" |
260 |
| - + "})", customerResponseHandler) |
261 |
| - : "") |
262 |
| - .build(); |
| 255 | + builder.add("\n\n$L clientHandler.execute(new $T<$T, $T>()\n" + |
| 256 | + ".withMarshaller(new $T($L))\n" + |
| 257 | + ".withResponseHandler($L)\n" + |
| 258 | + ".withErrorResponseHandler(errorResponseHandler)\n" + |
| 259 | + asyncRequestBody + |
| 260 | + ".withInput($L)$L)$L;", |
| 261 | + // If the operation has an event stream output we use a different future so we don't return the one |
| 262 | + // from the client. |
| 263 | + opModel.hasEventStreamOutput() ? "" : "return", |
| 264 | + ClientExecutionParams.class, |
| 265 | + requestType, |
| 266 | + opModel.hasEventStreamOutput() ? SdkResponse.class : pojoResponseType, |
| 267 | + marshaller, |
| 268 | + protocolFactory, |
| 269 | + opModel.hasEventStreamOutput() ? "voidResponseHandler" : "responseHandler", |
| 270 | + opModel.getInput().getVariableName(), |
| 271 | + isStreaming ? ", asyncResponseTransformer" : "", |
| 272 | + // If it's a streaming operation we also need to notify the handler on exception. |
| 273 | + isStreaming ? String.format(".whenComplete((r, e) -> {%n" |
| 274 | + + " if (e != null) {%n" |
| 275 | + + " %s.exceptionOccurred(e);%n" |
| 276 | + + " %s" |
| 277 | + + " }%n" |
| 278 | + + "})", customerResponseHandler, |
| 279 | + opModel.hasEventStreamOutput() ? "future.completeExceptionally(e);" : "") |
| 280 | + : ""); |
| 281 | + if (opModel.hasEventStreamOutput()) { |
| 282 | + builder.addStatement("return future"); |
| 283 | + } |
| 284 | + return builder.build(); |
263 | 285 | }
|
264 | 286 |
|
265 | 287 | private ClassName getUnmarshallerType(OperationModel opModel) {
|
|
0 commit comments