Class Hooks

    • Constructor Detail

      • Hooks

        public Hooks()
        Constructor.
    • Method Detail

      • 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 java.net.http.HttpRequest beforeRequest​(Hook.BeforeRequestContext context,
                                                       java.net.http.HttpRequest request)
                                                throws java.lang.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) 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:
        java.lang.Exception - on error
      • afterSuccess

        public java.net.http.HttpResponse<java.io.InputStream> afterSuccess​(Hook.AfterSuccessContext context,
                                                                            java.net.http.HttpResponse<java.io.InputStream> response)
                                                                     throws java.lang.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:
        java.lang.Exception - on error
      • afterError

        public java.net.http.HttpResponse<java.io.InputStream> afterError​(Hook.AfterErrorContext context,
                                                                          java.util.Optional<java.net.http.HttpResponse<java.io.InputStream>> response,
                                                                          java.util.Optional<java.lang.Exception> error)
                                                                   throws java.lang.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:
        java.lang.Exception - if error to be propagated