Class AbstractApiClient

  • Direct Known Subclasses:
    AsyncApiClient, DefaultApiClient, FluentApiClient

    public abstract class AbstractApiClient
    extends Object
    Abstract base class for API clients providing common HTTP request handling.

    This class serves as the foundation for all API client implementations in RocketRest. It provides:

    • Automatic token refresh on expiration
    • Configurable retry logic with exponential backoff
    • Request/response timing and logging
    • Header management including authentication

    Concrete implementations include:

    Request Execution Flow

    
     // 1. Create request specification
     RequestSpec<Void, User> request = RequestBuilder.get("/users/1")
         .responseType(User.class)
         .build();
    
     // 2. Execute with automatic retry and timing
     User user = client.execute(request);
     

    Configuration

    
     // Configure client options
     client.configure(RocketRestOptions.RETRY_ENABLED, true);
     client.configure(RocketRestOptions.MAX_RETRIES, 5);
     client.configure(RocketRestOptions.LOG_REQUEST_BODY, true);
     
    Since:
    1.0.0
    Author:
    guinetik <guinetik@gmail.com>
    See Also:
    DefaultApiClient, AsyncApiClient, FluentApiClient
    • Constructor Detail

      • AbstractApiClient

        protected AbstractApiClient​(String baseUrl,
                                    RocketRestConfig config,
                                    RocketClient httpClient)
        Creates a new API client.
        Parameters:
        baseUrl - The base URL for API requests.
        config - The RocketRest configuration.
        httpClient - The HTTP client implementation to use.
    • Method Detail

      • refreshToken

        protected void refreshToken()
        Method to handle token refresh. Refreshes the authentication token if needed using the configured auth strategy.
      • configure

        public AbstractApiClient configure​(String key,
                                           Object value)
        Sets a client option value.
        Parameters:
        key - The option key.
        value - The option value.
        Returns:
        This client instance for method chaining.
      • execute

        public <Req,​Res> Res execute​(RequestSpec<Req,​Res> requestSpec)
        Executes the given request specification and returns the response.
        Type Parameters:
        Req - The type of the request.
        Res - The type of the response.
        Parameters:
        requestSpec - The request specification.
        Returns:
        The response object.
      • executeWithTiming

        protected <Req,​Res> Res executeWithTiming​(RequestSpec<Req,​Res> requestSpec)
        Executes a request with timing measurement if enabled.
        Type Parameters:
        Req - The type of the request.
        Res - The type of the response.
        Parameters:
        requestSpec - The specification of the request to be executed.
        Returns:
        The response object.
      • executeWithRetry

        protected <Req,​Res> Res executeWithRetry​(RequestSpec<Req,​Res> requestSpec,
                                                       int retriesLeft)
        Executes an API request with retry logic. If the request fails due to a token expiration, it will attempt to refresh the token and retry the request up to a maximum number of retries.
        Type Parameters:
        Req - The type of the request.
        Res - The type of the response.
        Parameters:
        requestSpec - The specification of the request to be executed.
        retriesLeft - The number of retries remaining.
        Returns:
        The response object.
        Throws:
        ApiException - if the request fails after all retries are exhausted.
      • logRequest

        protected <Req,​Res> void logRequest​(RequestSpec<Req,​Res> requestSpec)
        Logs information about the request if logging is enabled.
        Type Parameters:
        Req - The type of the request.
        Res - The type of the response.
        Parameters:
        requestSpec - The request specification.
      • createHeaders

        protected RocketHeaders createHeaders​(Map<String,​String> customHeaders)
        Creates a map of HTTP headers, including default headers, authorization headers, and any custom headers provided.
        Parameters:
        customHeaders - A map of custom headers to include in the request.
        Returns:
        A complete map of headers for the request.
      • setBaseUrl

        public void setBaseUrl​(String baseUrl)