Class Hooks

java.lang.Object
com.styra.opa.openapi.utils.Hooks
All Implemented Interfaces:
Hook.AfterError, Hook.AfterSuccess, Hook.BeforeRequest, Hook.SdkInit

Registers hooks for use at runtime by an end-user or for use by a customer that may edit the SDKHooks.java file.

For example, this code will add a transaction id header to every request:

 hooks.registerBeforeRequest((context, request) -> {
     request.headers().map().put("acme-transaction-id", nextTransactionId());
     return request;
 });
 
  • Constructor Details

    • Hooks

      public Hooks()
      Constructor.
  • Method Details

    • registerBeforeRequest

      public Hooks registerBeforeRequest(Hook.BeforeRequest beforeRequest)
      Registers a Hook.BeforeRequest hook to be applied in order of registration. The result of the first BeforeRequest hook will be passed to the second BeforeRequest hook and processed similarly for the rest of the registered hooks. If a BeforeRequest hook throws then that Exception will not be passed to the Hook.AfterError hooks.
      Parameters:
      beforeRequest - hook to be registered
      Returns:
      this
    • registerAfterSuccess

      public Hooks registerAfterSuccess(Hook.AfterSuccess afterSuccess)
      Registers an Hook.AfterSuccess hook to be applied in order of registration (multiple can be registered). The result of the first AfterSuccess hook will be passed to the second AfterSuccess hook and processed similarly for the rest of the registered hooks. If an AfterSuccess hook throws then that Exception will not be passed to the Hook.AfterError hooks.
      Parameters:
      afterSuccess - hook to be registered
      Returns:
      this
    • registerAfterError

      public Hooks registerAfterError(Hook.AfterError afterError)
      Registers an Hook.AfterError hook to be applied in order of registration (multiple can be registered). If the first AfterError hook throws then the second hook will be called with that exception (and no response object) and so on for the rest of the registered hooks. If an AfterError hook returns normally then its result will be passed through to the next AfterError hook with the latest thrown Exception.
      Parameters:
      afterError - hook to be registered
      Returns:
      this
    • registerSdkInit

      public Hooks registerSdkInit(Hook.SdkInit SdkInit)
      Registers a Hook.SdkInit hook to be applied in order of registration (multiple can be registered).
      Parameters:
      SdkInit - hook to be registered
      Returns:
      this
    • beforeRequest

      public HttpRequest beforeRequest(Hook.BeforeRequestContext context, HttpRequest request) throws Exception
      Description copied from interface: Hook.BeforeRequest
      Transforms the given HttpRequest before sending.

      Note that HttpRequest is immutable. To modify the request you can use HttpRequest#newBuilder(HttpRequest, BiPredicate<String, String>) with JDK 16 and later (which will copy the request for modification in a builder). If that method is not available then use Helpers.copy(java.net.http.HttpRequest) (which also returns a builder).

      Specified by:
      beforeRequest in interface Hook.BeforeRequest
      Parameters:
      context - context for the hook call
      request - request to be transformed
      Returns:
      transformed request
      Throws:
      Exception - on error
    • afterSuccess

      public HttpResponse<InputStream> afterSuccess(Hook.AfterSuccessContext context, HttpResponse<InputStream> response) throws Exception
      Description copied from interface: Hook.AfterSuccess
      Transforms the given response before response processing occurs.
      Specified by:
      afterSuccess in interface Hook.AfterSuccess
      Parameters:
      context - context for the hook call
      response - response to be transformed
      Returns:
      transformed response
      Throws:
      Exception - on error
    • afterError

      public HttpResponse<InputStream> afterError(Hook.AfterErrorContext context, Optional<HttpResponse<InputStream>> response, Optional<Exception> error) throws Exception
      Description copied from interface: Hook.AfterError
      Either returns an HttpResponse or throws an Exception. Must be passed either a response or an error (both can't be absent).
      Specified by:
      afterError in interface Hook.AfterError
      Parameters:
      context - context for the error
      response - 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
      Throws:
      Exception - if error to be propagated
    • sdkInit

      public Hook.SdkInitData sdkInit(Hook.SdkInitData data)
      Description copied from interface: Hook.SdkInit
      Returns a transformed HTTPClient and baseUrl for use in requests.
      Specified by:
      sdkInit in interface Hook.SdkInit
      Parameters:
      data - data to transform
      Returns:
      the transformed data