use of software.amazon.awssdk.core.http.HttpResponseHandler in project aws-sdk-java-v2 by aws.
the class DefaultJsonAsyncClient method streamingInputOutputOperation.
/**
* Some operation with streaming input and streaming output
*
* @param streamingInputOutputOperationRequest
* @param requestBody
* Functional interface that can be implemented to produce the request content in a non-blocking manner. The
* size of the content is expected to be known up front. See {@link AsyncRequestBody} for specific details on
* implementing this interface as well as links to precanned implementations for common scenarios like
* uploading from a file. The service documentation for the request content is as follows 'This be a stream'
* @param asyncResponseTransformer
* The response transformer for processing the streaming response in a non-blocking manner. See
* {@link AsyncResponseTransformer} for details on how this callback should be implemented and for links to
* precanned implementations for common scenarios like downloading to a file. The service documentation for
* the response content is as follows 'This be a stream'.
* @return A future to the transformed result of the AsyncResponseTransformer.<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.StreamingInputOutputOperation
* @see <a href="https://docs.aws.amazon.com/goto/WebAPI/json-service-2010-05-08/StreamingInputOutputOperation"
* target="_top">AWS API Documentation</a>
*/
@Override
public <ReturnT> CompletableFuture<ReturnT> streamingInputOutputOperation(StreamingInputOutputOperationRequest streamingInputOutputOperationRequest, AsyncRequestBody requestBody, AsyncResponseTransformer<StreamingInputOutputOperationResponse, ReturnT> asyncResponseTransformer) {
List<MetricPublisher> metricPublishers = resolveMetricPublishers(clientConfiguration, streamingInputOutputOperationRequest.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, "StreamingInputOutputOperation");
Pair<AsyncResponseTransformer<StreamingInputOutputOperationResponse, ReturnT>, CompletableFuture<Void>> pair = AsyncResponseTransformerUtils.wrapWithEndOfStreamFuture(asyncResponseTransformer);
asyncResponseTransformer = pair.left();
CompletableFuture<Void> endOfStreamFuture = pair.right();
streamingInputOutputOperationRequest = applySignerOverride(streamingInputOutputOperationRequest, Aws4UnsignedPayloadSigner.create());
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true).isPayloadJson(false).build();
HttpResponseHandler<StreamingInputOutputOperationResponse> responseHandler = protocolFactory.createResponseHandler(operationMetadata, StreamingInputOutputOperationResponse::builder);
HttpResponseHandler<AwsServiceException> errorResponseHandler = createErrorResponseHandler(protocolFactory, operationMetadata);
CompletableFuture<ReturnT> executeFuture = clientHandler.execute(new ClientExecutionParams<StreamingInputOutputOperationRequest, StreamingInputOutputOperationResponse>().withOperationName("StreamingInputOutputOperation").withMarshaller(AsyncStreamingRequestMarshaller.builder().delegateMarshaller(new StreamingInputOutputOperationRequestMarshaller(protocolFactory)).asyncRequestBody(requestBody).transferEncoding(true).build()).withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler).withMetricCollector(apiCallMetricCollector).withAsyncRequestBody(requestBody).withInput(streamingInputOutputOperationRequest), asyncResponseTransformer);
AsyncResponseTransformer<StreamingInputOutputOperationResponse, ReturnT> finalAsyncResponseTransformer = asyncResponseTransformer;
CompletableFuture<ReturnT> whenCompleted = executeFuture.whenComplete((r, e) -> {
if (e != null) {
runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", () -> finalAsyncResponseTransformer.exceptionOccurred(e));
}
endOfStreamFuture.whenComplete((r2, e2) -> {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
});
});
executeFuture = CompletableFutureUtils.forwardExceptionTo(whenCompleted, executeFuture);
return executeFuture;
} catch (Throwable t) {
AsyncResponseTransformer<StreamingInputOutputOperationResponse, ReturnT> finalAsyncResponseTransformer = asyncResponseTransformer;
runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", () -> finalAsyncResponseTransformer.exceptionOccurred(t));
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
return CompletableFutureUtils.failedFuture(t);
}
}
use of software.amazon.awssdk.core.http.HttpResponseHandler in project aws-sdk-java-v2 by aws.
the class DefaultJsonAsyncClient method paginatedOperationWithoutResultKey.
/**
* Some paginated operation without result_key in paginators.json file
*
* @param paginatedOperationWithoutResultKeyRequest
* @return A Java Future containing the result of the PaginatedOperationWithoutResultKey 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.PaginatedOperationWithoutResultKey
* @see <a href="https://docs.aws.amazon.com/goto/WebAPI/json-service-2010-05-08/PaginatedOperationWithoutResultKey"
* target="_top">AWS API Documentation</a>
*/
@Override
public CompletableFuture<PaginatedOperationWithoutResultKeyResponse> paginatedOperationWithoutResultKey(PaginatedOperationWithoutResultKeyRequest paginatedOperationWithoutResultKeyRequest) {
List<MetricPublisher> metricPublishers = resolveMetricPublishers(clientConfiguration, paginatedOperationWithoutResultKeyRequest.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, "PaginatedOperationWithoutResultKey");
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false).isPayloadJson(true).build();
HttpResponseHandler<PaginatedOperationWithoutResultKeyResponse> responseHandler = protocolFactory.createResponseHandler(operationMetadata, PaginatedOperationWithoutResultKeyResponse::builder);
HttpResponseHandler<AwsServiceException> errorResponseHandler = createErrorResponseHandler(protocolFactory, operationMetadata);
CompletableFuture<PaginatedOperationWithoutResultKeyResponse> executeFuture = clientHandler.execute(new ClientExecutionParams<PaginatedOperationWithoutResultKeyRequest, PaginatedOperationWithoutResultKeyResponse>().withOperationName("PaginatedOperationWithoutResultKey").withMarshaller(new PaginatedOperationWithoutResultKeyRequestMarshaller(protocolFactory)).withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler).withMetricCollector(apiCallMetricCollector).withInput(paginatedOperationWithoutResultKeyRequest));
CompletableFuture<PaginatedOperationWithoutResultKeyResponse> whenCompleted = executeFuture.whenComplete((r, e) -> {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
});
executeFuture = CompletableFutureUtils.forwardExceptionTo(whenCompleted, executeFuture);
return executeFuture;
} catch (Throwable t) {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
return CompletableFutureUtils.failedFuture(t);
}
}
use of software.amazon.awssdk.core.http.HttpResponseHandler in project aws-sdk-java-v2 by aws.
the class DefaultJsonAsyncClient method eventStreamOperationWithOnlyInput.
/**
* Invokes the EventStreamOperationWithOnlyInput operation asynchronously.
*
* @param eventStreamOperationWithOnlyInputRequest
* @return A Java Future containing the result of the EventStreamOperationWithOnlyInput 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.EventStreamOperationWithOnlyInput
* @see <a href="https://docs.aws.amazon.com/goto/WebAPI/json-service-2010-05-08/EventStreamOperationWithOnlyInput"
* target="_top">AWS API Documentation</a>
*/
@Override
public CompletableFuture<EventStreamOperationWithOnlyInputResponse> eventStreamOperationWithOnlyInput(EventStreamOperationWithOnlyInputRequest eventStreamOperationWithOnlyInputRequest, Publisher<InputEventStreamTwo> requestStream) {
List<MetricPublisher> metricPublishers = resolveMetricPublishers(clientConfiguration, eventStreamOperationWithOnlyInputRequest.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, "EventStreamOperationWithOnlyInput");
eventStreamOperationWithOnlyInputRequest = applySignerOverride(eventStreamOperationWithOnlyInputRequest, EventStreamAws4Signer.create());
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false).isPayloadJson(true).build();
HttpResponseHandler<EventStreamOperationWithOnlyInputResponse> responseHandler = protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationWithOnlyInputResponse::builder);
HttpResponseHandler<AwsServiceException> errorResponseHandler = createErrorResponseHandler(protocolFactory, operationMetadata);
EventStreamTaggedUnionJsonMarshaller eventMarshaller = EventStreamTaggedUnionJsonMarshaller.builder().putMarshaller(DefaultInputEventOne.class, new InputEventMarshaller(protocolFactory)).putMarshaller(DefaultInputEventTwo.class, new InputEventTwoMarshaller(protocolFactory)).build();
SdkPublisher<InputEventStreamTwo> eventPublisher = SdkPublisher.adapt(requestStream);
Publisher<ByteBuffer> adapted = eventPublisher.map(event -> eventMarshaller.marshall(event)).map(AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer);
CompletableFuture<EventStreamOperationWithOnlyInputResponse> executeFuture = clientHandler.execute(new ClientExecutionParams<EventStreamOperationWithOnlyInputRequest, EventStreamOperationWithOnlyInputResponse>().withOperationName("EventStreamOperationWithOnlyInput").withMarshaller(new EventStreamOperationWithOnlyInputRequestMarshaller(protocolFactory)).withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withInitialRequestEvent(true).withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler).withMetricCollector(apiCallMetricCollector).withInput(eventStreamOperationWithOnlyInputRequest));
CompletableFuture<EventStreamOperationWithOnlyInputResponse> whenCompleted = executeFuture.whenComplete((r, e) -> {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
});
executeFuture = CompletableFutureUtils.forwardExceptionTo(whenCompleted, executeFuture);
return executeFuture;
} catch (Throwable t) {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
return CompletableFutureUtils.failedFuture(t);
}
}
use of software.amazon.awssdk.core.http.HttpResponseHandler in project aws-sdk-java-v2 by aws.
the class DefaultQueryAsyncClient method aPostOperation.
/**
* <p>
* Performs a post operation to the query service and has no output
* </p>
*
* @param aPostOperationRequest
* @return A Java Future containing the result of the APostOperation operation returned by the service.<br/>
* The CompletableFuture returned by this method can be completed exceptionally with the following
* exceptions.
* <ul>
* <li>InvalidInputException The request was rejected because an invalid or out-of-range value was supplied
* for an input parameter.</li>
* <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>QueryException Base class for all service exceptions. Unknown exceptions will be thrown as an
* instance of this type.</li>
* </ul>
* @sample QueryAsyncClient.APostOperation
* @see <a href="https://docs.aws.amazon.com/goto/WebAPI/query-service-2010-05-08/APostOperation" target="_top">AWS
* API Documentation</a>
*/
@Override
public CompletableFuture<APostOperationResponse> aPostOperation(APostOperationRequest aPostOperationRequest) {
List<MetricPublisher> metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationRequest.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Query Service");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "APostOperation");
HttpResponseHandler<APostOperationResponse> responseHandler = protocolFactory.createResponseHandler(APostOperationResponse::builder);
HttpResponseHandler<AwsServiceException> errorResponseHandler = protocolFactory.createErrorResponseHandler();
String hostPrefix = "foo-";
String resolvedHostExpression = "foo-";
CompletableFuture<APostOperationResponse> executeFuture = clientHandler.execute(new ClientExecutionParams<APostOperationRequest, APostOperationResponse>().withOperationName("APostOperation").withMarshaller(new APostOperationRequestMarshaller(protocolFactory)).withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler).withMetricCollector(apiCallMetricCollector).hostPrefixExpression(resolvedHostExpression).withInput(aPostOperationRequest));
CompletableFuture<APostOperationResponse> whenCompleteFuture = null;
whenCompleteFuture = executeFuture.whenComplete((r, e) -> {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
});
return CompletableFutureUtils.forwardExceptionTo(whenCompleteFuture, executeFuture);
} catch (Throwable t) {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
return CompletableFutureUtils.failedFuture(t);
}
}
use of software.amazon.awssdk.core.http.HttpResponseHandler in project aws-sdk-java-v2 by aws.
the class DefaultQueryAsyncClient method aPostOperationWithOutput.
/**
* <p>
* Performs a post operation to the query service and has modelled output
* </p>
*
* @param aPostOperationWithOutputRequest
* @return A Java Future containing the result of the APostOperationWithOutput operation returned by the service.<br/>
* The CompletableFuture returned by this method can be completed exceptionally with the following
* exceptions.
* <ul>
* <li>InvalidInputException The request was rejected because an invalid or out-of-range value was supplied
* for an input parameter.</li>
* <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>QueryException Base class for all service exceptions. Unknown exceptions will be thrown as an
* instance of this type.</li>
* </ul>
* @sample QueryAsyncClient.APostOperationWithOutput
* @see <a href="https://docs.aws.amazon.com/goto/WebAPI/query-service-2010-05-08/APostOperationWithOutput"
* target="_top">AWS API Documentation</a>
*/
@Override
public CompletableFuture<APostOperationWithOutputResponse> aPostOperationWithOutput(APostOperationWithOutputRequest aPostOperationWithOutputRequest) {
List<MetricPublisher> metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationWithOutputRequest.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Query Service");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "APostOperationWithOutput");
HttpResponseHandler<APostOperationWithOutputResponse> responseHandler = protocolFactory.createResponseHandler(APostOperationWithOutputResponse::builder);
HttpResponseHandler<AwsServiceException> errorResponseHandler = protocolFactory.createErrorResponseHandler();
CompletableFuture<APostOperationWithOutputResponse> executeFuture = clientHandler.execute(new ClientExecutionParams<APostOperationWithOutputRequest, APostOperationWithOutputResponse>().withOperationName("APostOperationWithOutput").withMarshaller(new APostOperationWithOutputRequestMarshaller(protocolFactory)).withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler).withMetricCollector(apiCallMetricCollector).withInput(aPostOperationWithOutputRequest));
CompletableFuture<APostOperationWithOutputResponse> whenCompleteFuture = null;
whenCompleteFuture = executeFuture.whenComplete((r, e) -> {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
});
return CompletableFutureUtils.forwardExceptionTo(whenCompleteFuture, executeFuture);
} catch (Throwable t) {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
return CompletableFutureUtils.failedFuture(t);
}
}
Aggregations