T - The type of the wrapped TransformingAsyncResponseHandlerR - The type of the object used to determine scope.@ThreadSafe public class IdempotentAsyncResponseHandler<T,R> extends Object implements TransformingAsyncResponseHandler<T>
TransformingAsyncResponseHandler that allows attachment to an external scope and given a way
of evaluating whether that scope has changed or not will only allow prepare() to be called on its delegate once
per state change and will cache and reserve the future that is returned by the delegate the rest of the time.
One application of this wrapper is to ensure that prepare() is not called on the underlying response handler more than once per retry no matter where or how many times its invoked.
This class is asserted to be thread-safe and it should be fine to have multiple threads call prepare() simultaneously.
| Modifier and Type | Method and Description |
|---|---|
static <T,R> IdempotentAsyncResponseHandler<T,R> |
create(TransformingAsyncResponseHandler<T> wrappedHandler,
Supplier<R> preparedScopeSupplier,
BiPredicate<R,R> scopeInRangePredicate)
Creates a new wrapped
TransformingAsyncResponseHandler |
void |
onError(Throwable error)
Called when there is an error making the request or receiving the response.
|
void |
onHeaders(SdkHttpResponse headers)
Called when the headers have been received.
|
void |
onStream(org.reactivestreams.Publisher<ByteBuffer> stream)
Called when the streaming body is ready.
|
CompletableFuture<T> |
prepare()
Return the future holding the transformed response.
|
public static <T,R> IdempotentAsyncResponseHandler<T,R> create(TransformingAsyncResponseHandler<T> wrappedHandler, Supplier<R> preparedScopeSupplier, BiPredicate<R,R> scopeInRangePredicate)
TransformingAsyncResponseHandlerT - The type of the wrapped TransformingAsyncResponseHandlerR - The type of the object used to determine scope.wrappedHandler - A different handler to wrap.preparedScopeSupplier - A function that retrieves the current scope to determine if a new future can be
prepared.scopeInRangePredicate - A predicate that can be used to test the cached scope against the newly retrieved
scope to determine if a new future can be prepared.public CompletableFuture<T> prepare()
TransformingAsyncResponseHandler
This method is guaranteed to be called before the request is executed, and before SdkAsyncHttpResponseHandler.onHeaders(com.ibm.cos.v2.http.SdkHttpResponse) is signaled.
prepare in interface TransformingAsyncResponseHandler<T>public void onHeaders(SdkHttpResponse headers)
SdkAsyncHttpResponseHandleronHeaders in interface SdkAsyncHttpResponseHandlerheaders - The headers.public void onStream(org.reactivestreams.Publisher<ByteBuffer> stream)
SdkAsyncHttpResponseHandlerThis method is always called. If the response does not have a body, then the publisher will complete the subscription without signalling any elements.
onStream in interface SdkAsyncHttpResponseHandlerstream - The streaming body.public void onError(Throwable error)
SdkAsyncHttpResponseHandlerSubscriber.onError in interface SdkAsyncHttpResponseHandlererror - The error.Copyright © 2026. All rights reserved.