Package com.guinetik.rr.api
Class AbstractApiClient
- java.lang.Object
-
- com.guinetik.rr.api.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:
DefaultApiClient- Synchronous blocking requestsAsyncApiClient- Asynchronous requests withCompletableFutureFluentApiClient- Functional style withResultpattern
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
-
-
Field Summary
Fields Modifier and Type Field Description protected StringbaseUrlprotected RocketRestConfigconfigprotected RocketClienthttpClientprotected org.slf4j.Loggerloggerprotected RocketRestOptionsoptions
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractApiClient(String baseUrl, RocketRestConfig config, RocketClient httpClient)Creates a new API client.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description AbstractApiClientconfigure(String key, Object value)Sets a client option value.protected RocketHeaderscreateHeaders(Map<String,String> customHeaders)Creates a map of HTTP headers, including default headers, authorization headers, and any custom headers provided.<Req,Res>
Resexecute(RequestSpec<Req,Res> requestSpec)Executes the given request specification and returns the response.protected <Req,Res>
ResexecuteWithRetry(RequestSpec<Req,Res> requestSpec, int retriesLeft)Executes an API request with retry logic.protected <Req,Res>
ResexecuteWithTiming(RequestSpec<Req,Res> requestSpec)Executes a request with timing measurement if enabled.protected <Req,Res>
voidlogRequest(RequestSpec<Req,Res> requestSpec)Logs information about the request if logging is enabled.protected voidrefreshToken()Method to handle token refresh.voidsetBaseUrl(String baseUrl)
-
-
-
Field Detail
-
logger
protected final org.slf4j.Logger logger
-
baseUrl
protected String baseUrl
-
config
protected final RocketRestConfig config
-
options
protected final RocketRestOptions options
-
httpClient
protected RocketClient httpClient
-
-
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)
-
-