use of software.amazon.awssdk.awscore.eventstream.RestEventStreamAsyncResponseTransformer in project aws-sdk-java-v2 by aws.
the class DefaultXmlAsyncClient method eventStreamOperation.
/**
* Invokes the EventStreamOperation operation asynchronously.
*
* @param eventStreamOperationRequest
* @return A Java Future containing the result of the EventStreamOperation operation returned by the service.<br/>
* The CompletableFuture returned by this method can be completed exceptionally with the following
* exceptions.
* <ul>
* <li>SdkException Base class for all exceptions that can be thrown by the SDK (both service and client).
* Can be used for catch all scenarios.</li>
* <li>SdkClientException If any client side error occurs such as an IO related failure, failure to get
* credentials, etc.</li>
* <li>XmlException Base class for all service exceptions. Unknown exceptions will be thrown as an instance
* of this type.</li>
* </ul>
* @sample XmlAsyncClient.EventStreamOperation
* @see <a href="https://docs.aws.amazon.com/goto/WebAPI/xml-service-2010-05-08/EventStreamOperation"
* target="_top">AWS API Documentation</a>
*/
@Override
public CompletableFuture<Void> eventStreamOperation(EventStreamOperationRequest eventStreamOperationRequest, EventStreamOperationResponseHandler asyncResponseHandler) {
List<MetricPublisher> metricPublishers = resolveMetricPublishers(clientConfiguration, eventStreamOperationRequest.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Xml Service");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EventStreamOperation");
HttpResponseHandler<EventStreamOperationResponse> responseHandler = protocolFactory.createResponseHandler(EventStreamOperationResponse::builder, XmlOperationMetadata.builder().hasStreamingSuccessResponse(true).build());
HttpResponseHandler<AwsServiceException> errorResponseHandler = protocolFactory.createErrorResponseHandler();
HttpResponseHandler<? extends EventStream> eventResponseHandler = protocolFactory.createResponseHandler(EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventPayloadEvent", EventStream::eventPayloadEventBuilder).putSdkPojoSupplier("NonEventPayloadEvent", EventStream::nonEventPayloadEventBuilder).putSdkPojoSupplier("SecondEventPayloadEvent", EventStream::secondEventPayloadEventBuilder).defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build(), XmlOperationMetadata.builder().hasStreamingSuccessResponse(false).build());
CompletableFuture<Void> eventStreamTransformFuture = new CompletableFuture<>();
EventStreamAsyncResponseTransformer<EventStreamOperationResponse, EventStream> asyncResponseTransformer = EventStreamAsyncResponseTransformer.<EventStreamOperationResponse, EventStream>builder().eventStreamResponseHandler(asyncResponseHandler).eventResponseHandler(eventResponseHandler).initialResponseHandler(responseHandler).exceptionResponseHandler(errorResponseHandler).future(eventStreamTransformFuture).executor(executor).serviceName(serviceName()).build();
RestEventStreamAsyncResponseTransformer<EventStreamOperationResponse, EventStream> restAsyncResponseTransformer = RestEventStreamAsyncResponseTransformer.<EventStreamOperationResponse, EventStream>builder().eventStreamAsyncResponseTransformer(asyncResponseTransformer).eventStreamResponseHandler(asyncResponseHandler).build();
CompletableFuture<Void> executeFuture = clientHandler.execute(new ClientExecutionParams<EventStreamOperationRequest, EventStreamOperationResponse>().withOperationName("EventStreamOperation").withMarshaller(new EventStreamOperationRequestMarshaller(protocolFactory)).withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler).withMetricCollector(apiCallMetricCollector).withInput(eventStreamOperationRequest), restAsyncResponseTransformer);
CompletableFuture<Void> whenCompleteFuture = null;
whenCompleteFuture = executeFuture.whenComplete((r, e) -> {
if (e != null) {
runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", () -> asyncResponseHandler.exceptionOccurred(e));
eventStreamTransformFuture.completeExceptionally(e);
}
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
});
CompletableFutureUtils.forwardExceptionTo(whenCompleteFuture, executeFuture);
return CompletableFutureUtils.forwardExceptionTo(eventStreamTransformFuture, executeFuture);
} catch (Throwable t) {
runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", () -> asyncResponseHandler.exceptionOccurred(t));
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
return CompletableFutureUtils.failedFuture(t);
}
}
use of software.amazon.awssdk.awscore.eventstream.RestEventStreamAsyncResponseTransformer in project aws-sdk-java-v2 by aws.
the class XmlProtocolSpec method eventStreamResponseTransformers.
private CodeBlock eventStreamResponseTransformers(OperationModel opModel, String eventTransformerFutureName) {
ShapeModel shapeModel = EventStreamUtils.getEventStreamInResponse(opModel.getOutputShape());
ClassName pojoResponseType = poetExtensions.getModelClass(opModel.getReturnType().getReturnType());
ClassName eventStreamBaseClass = poetExtensions.getModelClassFromShape(shapeModel);
CodeBlock.Builder builder = CodeBlock.builder();
ParameterizedTypeName transformerType = ParameterizedTypeName.get(ClassName.get(EventStreamAsyncResponseTransformer.class), pojoResponseType, eventStreamBaseClass);
builder.addStatement("$1T<$2T> $3N = new $1T<>()", ClassName.get(CompletableFuture.class), ClassName.get(Void.class), eventTransformerFutureName).add("$T asyncResponseTransformer = $T.<$T, $T>builder()\n", transformerType, ClassName.get(EventStreamAsyncResponseTransformer.class), pojoResponseType, eventStreamBaseClass).add(".eventStreamResponseHandler(asyncResponseHandler)\n").add(".eventResponseHandler(eventResponseHandler)\n").add(".initialResponseHandler(responseHandler)\n").add(".exceptionResponseHandler(errorResponseHandler)\n").add(".future($N)\n", eventTransformerFutureName).add(".executor(executor)\n").add(".serviceName(serviceName())\n").addStatement(".build()");
ParameterizedTypeName restTransformType = ParameterizedTypeName.get(ClassName.get(RestEventStreamAsyncResponseTransformer.class), pojoResponseType, eventStreamBaseClass);
// Wrap the event transformer with this so that the caller's response handler's onResponse() method is invoked. See
// docs for RestEventStreamAsyncResponseTransformer for more info on why it's needed
builder.addStatement("$T restAsyncResponseTransformer = $T.<$T, $T>builder()\n" + ".eventStreamAsyncResponseTransformer(asyncResponseTransformer)\n" + ".eventStreamResponseHandler(asyncResponseHandler)\n" + ".build()", restTransformType, RestEventStreamAsyncResponseTransformer.class, pojoResponseType, eventStreamBaseClass);
return builder.build();
}
Aggregations