Interface WrappedApi


  • public interface WrappedApi
    • Method Detail

      • getDefaultUrl

        java.lang.String getDefaultUrl()
        Gets the default URL for this API.
        Returns:
        The default URL for this API.
      • getDefaultRequestHeaders

        default RequestHeader[] getDefaultRequestHeaders()
        Gets the default RequestHeaders for this API.
        Useful if you want to set some headers for all requests.
        Returns:
        Nullable array of RequestHeaders.
      • createHttpClientInstance

        default java.net.http.HttpClient createHttpClientInstance()
        Creates a new HttpClient instance that will be used for sending requests.
        If the ApiRequest.createHttpClientInstance() is overridden, this method may not be called, depending on the implementation.
        Returns:
        The created HttpClient instance.
      • createHttpRequestBuilderInstance

        default java.net.http.HttpRequest.Builder createHttpRequestBuilderInstance()
        Creates a new HttpRequest.Builder instance that will be used for building requests.
        If the ApiRequest.createHttpRequestBuilderInstance() is overridden, this method may not be called, depending on the implementation.
        Returns:
        The created HttpRequest.Builder instance.
      • runAsync

        default void runAsync​(java.lang.Runnable runnable)
        This method is used for async requests. You may override this method to change the way async requests are sent, for example, using a thread pool, etc.
        Parameters:
        runnable - The runnable to run.
      • createInstanceOfResponseClass

        default <T> T createInstanceOfResponseClass​(java.lang.Class<T> responseClass)
                                             throws java.lang.NoSuchMethodException,
                                                    java.lang.reflect.InvocationTargetException,
                                                    java.lang.InstantiationException,
                                                    java.lang.IllegalAccessException
        Creates a new instance of the response class.
        If the ApiRequest.createInstanceOfResponseClass() is overridden, this method may not be called, depending on the implementation.
        Type Parameters:
        T - The type of the response.
        Parameters:
        responseClass - The response class to create an instance of.
        Returns:
        The created instance of the response class.
        Throws:
        java.lang.NoSuchMethodException - Is thrown if the response class does not have a default constructor.
        java.lang.reflect.InvocationTargetException - Is thrown if the constructor of the response class throws an exception.
        java.lang.InstantiationException - Is thrown if the response class is abstract or interface.
        java.lang.IllegalAccessException - Is thrown if the constructor of the response class is not accessible.
      • handleResponse

        default <T> T handleResponse​(ApiRequest<T> apiRequest,
                                     java.net.http.HttpResponse<?> httpResponse)
                              throws java.lang.reflect.InvocationTargetException,
                                     java.lang.NoSuchMethodException,
                                     java.lang.InstantiationException,
                                     java.lang.IllegalAccessException
        Handles the given HttpResponse.
        If the ApiRequest.handleResponse(HttpResponse) is overridden, this method may not be called, depending on the implementation.
        Type Parameters:
        T - The type of the response.
        Parameters:
        apiRequest - The ApiRequest to handle the response for.
        httpResponse - The HttpResponse to handle.
        Returns:
        The instance of the response class.
        Throws:
        java.lang.NoSuchMethodException - Is thrown if the response class does not have a default constructor.
        java.lang.reflect.InvocationTargetException - Is thrown if the constructor of the response class throws an exception.
        java.lang.InstantiationException - Is thrown if the response class is abstract or interface.
        java.lang.IllegalAccessException - Is thrown if the constructor of the response class is not accessible.
      • send

        default <T> T send​(ApiRequest<T> apiRequest)
                    throws java.io.IOException,
                           java.lang.InterruptedException,
                           java.lang.reflect.InvocationTargetException,
                           java.lang.NoSuchMethodException,
                           java.lang.InstantiationException,
                           java.lang.IllegalAccessException
        Sends the request synchronously.
        If the ApiRequest.send() is overridden, this method may not be called, depending on the implementation.
        Type Parameters:
        T - The type of the response.
        Parameters:
        apiRequest - The request to send.
        Returns:
        The instance of the response class.
        Throws:
        java.io.IOException - If an I/O error occurs.
        java.lang.InterruptedException - If the operation is interrupted.
        java.lang.NoSuchMethodException - Is thrown if the response class does not have a default constructor.
        java.lang.reflect.InvocationTargetException - Is thrown if the constructor of the response class throws an exception.
        java.lang.InstantiationException - Is thrown if the response class is abstract or interface.
        java.lang.IllegalAccessException - Is thrown if the constructor of the response class is not accessible.
      • onApiRequest

        default <T> void onApiRequest​(ApiRequest<T> request)
        Is called before the request is sent.
        Type Parameters:
        T - The type of the response.
        Parameters:
        request - The request.
      • onAfterApiRequest

        default <T> void onAfterApiRequest​(ApiRequest<T> request)
        It is called after the request is sent but before the response is handled.
        If the request send fails with exception, this method is not called.
        Type Parameters:
        T - The type of the response.
        Parameters:
        request - The request.
      • onAfterHandledApiRequest

        default <T> void onAfterHandledApiRequest​(ApiRequest<T> request,
                                                  T response)
        It is called after the request's response is handled.
        If the request send and/or request's response handler fails with exception, this method is not called.
        Type Parameters:
        T - The type of the response.
        Parameters:
        request - The request.
        response - The response.
      • onException

        default <T> void onException​(ApiRequest<T> request,
                                     java.lang.Throwable throwable)
        It is called when any exception occurs when sending the request or handling the response.

        Note: This method does not catch any exceptions.
        All exceptions are re-thrown after this method is called, if the rethrowExceptions() returns true.
        Type Parameters:
        T - The type of the response.
        Parameters:
        request - The request.
        throwable - The exception.
      • rethrowExceptions

        default boolean rethrowExceptions()
        Determines if exceptions should be re-thrown after onException(ApiRequest, Throwable) is called.
        Also, if this method returns false, all responses that failed to be sent or handled will be null.
        Returns:
        If exceptions should be re-thrown after onException(ApiRequest, Throwable) is called.