Package com.google.genai.gaos.utils
Class AsyncHooks
java.lang.Object
com.google.genai.gaos.utils.AsyncHooks
- All Implemented Interfaces:
AsyncHook.AfterError,AsyncHook.AfterSuccess,AsyncHook.BeforeRequest
public class AsyncHooks
extends Object
implements AsyncHook.BeforeRequest, AsyncHook.AfterSuccess, AsyncHook.AfterError
Async hook registry for runtime request/response processing.
Example usage:
asyncHooks.registerBeforeRequest((context, request) ->
CompletableFuture.completedFuture(
request.toBuilder()
.header("transaction-id", UUID.randomUUID().toString())
.build()));
-
Field Summary
Fields inherited from interface com.google.genai.gaos.utils.AsyncHook.AfterError
DEFAULTFields inherited from interface com.google.genai.gaos.utils.AsyncHook.AfterSuccess
DEFAULTFields inherited from interface com.google.genai.gaos.utils.AsyncHook.BeforeRequest
DEFAULT -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionafterError(Hook.AfterErrorContext context, HttpResponse<InputStream> response, Throwable error) Either returns an HttpResponse or throws an Exception.afterSuccess(Hook.AfterSuccessContext context, HttpResponse<InputStream> response) Transforms the given response before response processing occurs.beforeRequest(Hook.BeforeRequestContext context, HttpRequest request) Transforms the givenHttpRequestbefore sending.registerAfterError(AsyncHook.AfterError afterError) Registers an async after-error hook.registerAfterSuccess(AsyncHook.AfterSuccess afterSuccess) Registers an async after-success hook.registerBeforeRequest(AsyncHook.BeforeRequest beforeRequest) Registers an async before-request hook.
-
Constructor Details
-
AsyncHooks
public AsyncHooks()
-
-
Method Details
-
registerBeforeRequest
Registers an async before-request hook. Hooks are chained in registration order.- Parameters:
beforeRequest- async hook returningCompletableFuture<HttpRequest>- Returns:
- this
-
registerAfterSuccess
Registers an async after-success hook. Hooks are chained in registration order.- Parameters:
afterSuccess- async hook returningCompletableFuture<HttpResponse>- Returns:
- this
-
registerAfterError
Registers an async after-error hook. Hooks are chained in registration order.- Parameters:
afterError- async hook for error handling- Returns:
- this
-
beforeRequest
public CompletableFuture<HttpRequest> beforeRequest(Hook.BeforeRequestContext context, HttpRequest request) Description copied from interface:AsyncHook.BeforeRequestTransforms the givenHttpRequestbefore sending.Note that
HttpRequestis immutable. To modify the request useHttpRequest.toBuilder()which copies the request into a builder.- Specified by:
beforeRequestin interfaceAsyncHook.BeforeRequest- Parameters:
context- context for the hook callrequest- request to be transformed- Returns:
- transformed request
-
afterSuccess
public CompletableFuture<HttpResponse<InputStream>> afterSuccess(Hook.AfterSuccessContext context, HttpResponse<InputStream> response) Description copied from interface:AsyncHook.AfterSuccessTransforms the given response before response processing occurs.- Specified by:
afterSuccessin interfaceAsyncHook.AfterSuccess- Parameters:
context- context for the hook callresponse- response to be transformed- Returns:
- transformed response
-
afterError
public CompletableFuture<HttpResponse<InputStream>> afterError(Hook.AfterErrorContext context, HttpResponse<InputStream> response, Throwable error) Description copied from interface:AsyncHook.AfterErrorEither returns an HttpResponse or throws an Exception. Must be passed either a response or an error (both can't be absent).- Specified by:
afterErrorin interfaceAsyncHook.AfterError- Parameters:
context- context for the errorresponse- response information if available.error- the optional exception. If response present then the error is for-info only, it was the last error in the chain of AfterError hook calls leading to this one- Returns:
- HTTP response if method decides that an exception is not to be thrown
-