use of software.amazon.awssdk.core.protocol.VoidSdkResponse in project aws-sdk-java-v2 by aws.
the class DefaultJsonAsyncClient 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>JsonException Base class for all service exceptions. Unknown exceptions will be thrown as an instance
* of this type.</li>
* </ul>
* @sample JsonAsyncClient.EventStreamOperation
* @see <a href="https://docs.aws.amazon.com/goto/WebAPI/json-service-2010-05-08/EventStreamOperation"
* target="_top">AWS API Documentation</a>
*/
@Override
public CompletableFuture<Void> eventStreamOperation(EventStreamOperationRequest eventStreamOperationRequest, Publisher<InputEventStream> requestStream, 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, "Json Service");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EventStreamOperation");
eventStreamOperationRequest = applySignerOverride(eventStreamOperationRequest, EventStreamAws4Signer.create());
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false).isPayloadJson(true).build();
HttpResponseHandler<EventStreamOperationResponse> responseHandler = new AttachHttpMetadataResponseHandler(protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationResponse::builder));
HttpResponseHandler<SdkResponse> voidResponseHandler = protocolFactory.createResponseHandler(JsonOperationMetadata.builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder);
HttpResponseHandler<? extends EventStream> eventResponseHandler = protocolFactory.createResponseHandler(JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder).putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder).putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder).putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder).defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build());
HttpResponseHandler<AwsServiceException> errorResponseHandler = createErrorResponseHandler(protocolFactory, operationMetadata);
EventStreamTaggedUnionJsonMarshaller eventMarshaller = EventStreamTaggedUnionJsonMarshaller.builder().putMarshaller(DefaultInputEvent.class, new InputEventMarshaller(protocolFactory)).build();
SdkPublisher<InputEventStream> eventPublisher = SdkPublisher.adapt(requestStream);
Publisher<ByteBuffer> adapted = eventPublisher.map(event -> eventMarshaller.marshall(event)).map(AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer);
CompletableFuture<Void> future = new CompletableFuture<>();
EventStreamAsyncResponseTransformer<EventStreamOperationResponse, EventStream> asyncResponseTransformer = EventStreamAsyncResponseTransformer.<EventStreamOperationResponse, EventStream>builder().eventStreamResponseHandler(asyncResponseHandler).eventResponseHandler(eventResponseHandler).initialResponseHandler(responseHandler).exceptionResponseHandler(errorResponseHandler).future(future).executor(executor).serviceName(serviceName()).build();
CompletableFuture<Void> executeFuture = clientHandler.execute(new ClientExecutionParams<EventStreamOperationRequest, SdkResponse>().withOperationName("EventStreamOperation").withMarshaller(new EventStreamOperationRequestMarshaller(protocolFactory)).withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withFullDuplex(true).withInitialRequestEvent(true).withResponseHandler(voidResponseHandler).withErrorResponseHandler(errorResponseHandler).withMetricCollector(apiCallMetricCollector).withInput(eventStreamOperationRequest), asyncResponseTransformer);
CompletableFuture<Void> whenCompleted = executeFuture.whenComplete((r, e) -> {
if (e != null) {
try {
asyncResponseHandler.exceptionOccurred(e);
} finally {
future.completeExceptionally(e);
}
}
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
});
executeFuture = CompletableFutureUtils.forwardExceptionTo(whenCompleted, executeFuture);
return CompletableFutureUtils.forwardExceptionTo(future, 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.core.protocol.VoidSdkResponse in project aws-sdk-java-v2 by aws.
the class DefaultJsonAsyncClient method eventStreamOperationWithOnlyOutput.
/**
* Invokes the EventStreamOperationWithOnlyOutput operation asynchronously.
*
* @param eventStreamOperationWithOnlyOutputRequest
* @return A Java Future containing the result of the EventStreamOperationWithOnlyOutput 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>JsonException Base class for all service exceptions. Unknown exceptions will be thrown as an instance
* of this type.</li>
* </ul>
* @sample JsonAsyncClient.EventStreamOperationWithOnlyOutput
* @see <a href="https://docs.aws.amazon.com/goto/WebAPI/json-service-2010-05-08/EventStreamOperationWithOnlyOutput"
* target="_top">AWS API Documentation</a>
*/
@Override
public CompletableFuture<Void> eventStreamOperationWithOnlyOutput(EventStreamOperationWithOnlyOutputRequest eventStreamOperationWithOnlyOutputRequest, EventStreamOperationWithOnlyOutputResponseHandler asyncResponseHandler) {
List<MetricPublisher> metricPublishers = resolveMetricPublishers(clientConfiguration, eventStreamOperationWithOnlyOutputRequest.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EventStreamOperationWithOnlyOutput");
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false).isPayloadJson(true).build();
HttpResponseHandler<EventStreamOperationWithOnlyOutputResponse> responseHandler = new AttachHttpMetadataResponseHandler(protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationWithOnlyOutputResponse::builder));
HttpResponseHandler<SdkResponse> voidResponseHandler = protocolFactory.createResponseHandler(JsonOperationMetadata.builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder);
HttpResponseHandler<? extends EventStream> eventResponseHandler = protocolFactory.createResponseHandler(JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder).putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder).putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder).putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder).defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build());
HttpResponseHandler<AwsServiceException> errorResponseHandler = createErrorResponseHandler(protocolFactory, operationMetadata);
CompletableFuture<Void> future = new CompletableFuture<>();
EventStreamAsyncResponseTransformer<EventStreamOperationWithOnlyOutputResponse, EventStream> asyncResponseTransformer = EventStreamAsyncResponseTransformer.<EventStreamOperationWithOnlyOutputResponse, EventStream>builder().eventStreamResponseHandler(asyncResponseHandler).eventResponseHandler(eventResponseHandler).initialResponseHandler(responseHandler).exceptionResponseHandler(errorResponseHandler).future(future).executor(executor).serviceName(serviceName()).build();
CompletableFuture<Void> executeFuture = clientHandler.execute(new ClientExecutionParams<EventStreamOperationWithOnlyOutputRequest, SdkResponse>().withOperationName("EventStreamOperationWithOnlyOutput").withMarshaller(new EventStreamOperationWithOnlyOutputRequestMarshaller(protocolFactory)).withResponseHandler(voidResponseHandler).withErrorResponseHandler(errorResponseHandler).withMetricCollector(apiCallMetricCollector).withInput(eventStreamOperationWithOnlyOutputRequest), asyncResponseTransformer);
CompletableFuture<Void> whenCompleted = executeFuture.whenComplete((r, e) -> {
if (e != null) {
try {
asyncResponseHandler.exceptionOccurred(e);
} finally {
future.completeExceptionally(e);
}
}
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
});
executeFuture = CompletableFutureUtils.forwardExceptionTo(whenCompleted, executeFuture);
return CompletableFutureUtils.forwardExceptionTo(future, 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);
}
}
Aggregations