Package com.guinetik.rr.api
Class FluentApiClient
- java.lang.Object
-
- com.guinetik.rr.api.AbstractApiClient
-
- com.guinetik.rr.api.FluentApiClient
-
public class FluentApiClient extends AbstractApiClient
A fluent API client that uses theResultpattern instead of exceptions.This client provides a functional, declarative approach to API calls where errors are returned as values rather than thrown as exceptions. This makes error handling explicit and composable.
Basic Usage
FluentApiClient client = new FluentApiClient("https://api.example.com", config); Result<User, ApiError> result = client.get("/users/1", User.class); // Handle success and failure explicitly result.match( user -> System.out.println("Found: " + user.getName()), error -> System.err.println("Error " + error.getStatusCode() + ": " + error.getMessage()) );Chaining Operations
String userName = client.get("/users/1", User.class) .map(User::getName) .map(String::toUpperCase) .getOrElse("Unknown");POST with Body
CreateUserRequest request = new CreateUserRequest("John", "john@example.com"); Result<User, ApiError> result = client.post("/users", request, User.class); if (result.isSuccess()) { User created = result.getValue(); System.out.println("Created user with ID: " + created.getId()); }- Since:
- 1.0.0
- Author:
- guinetik <guinetik@gmail.com>
- See Also:
Result,ApiError,AbstractApiClient
-
-
Field Summary
-
Fields inherited from class com.guinetik.rr.api.AbstractApiClient
baseUrl, config, httpClient, logger, options
-
-
Constructor Summary
Constructors Constructor Description FluentApiClient(String baseUrl, RocketRestConfig config)Creates a new FluentApiClient with the specified base URL and configuration.FluentApiClient(String baseUrl, RocketRestConfig config, FluentHttpClient fluentClient)Creates a new FluentApiClient with the specified base URL, configuration, and a pre-configured FluentHttpClient instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <Res> Result<Res,ApiError>delete(String endpoint, Class<Res> responseType)Performs a DELETE request using the Result pattern.<Res> Result<Res,ApiError>delete(String endpoint, Map<String,String> headers, Class<Res> responseType)Performs a DELETE request with headers using the Result pattern.<Req,Res>
Result<Res,ApiError>executeWithResult(RequestSpec<Req,Res> requestSpec)Executes a request and returns a Result object instead of throwing exceptions.<Res> Result<Res,ApiError>get(String endpoint, Class<Res> responseType)Performs a GET request using the Result pattern.<Res> Result<Res,ApiError>get(String endpoint, Map<String,String> headers, Class<Res> responseType)Performs a GET request with headers using the Result pattern.<Req,Res>
Result<Res,ApiError>post(String endpoint, Req body, Class<Res> responseType)Performs a POST request using the Result pattern.<Req,Res>
Result<Res,ApiError>post(String endpoint, Req body, Map<String,String> headers, Class<Res> responseType)Performs a POST request with headers using the Result pattern.<Req,Res>
Result<Res,ApiError>put(String endpoint, Req body, Class<Res> responseType)Performs a PUT request using the Result pattern.<Req,Res>
Result<Res,ApiError>put(String endpoint, Req body, Map<String,String> headers, Class<Res> responseType)Performs a PUT request with headers using the Result pattern.voidshutdown()Shutdown the client and release resources.-
Methods inherited from class com.guinetik.rr.api.AbstractApiClient
configure, createHeaders, execute, executeWithRetry, executeWithTiming, logRequest, refreshToken, setBaseUrl
-
-
-
-
Constructor Detail
-
FluentApiClient
public FluentApiClient(String baseUrl, RocketRestConfig config)
Creates a new FluentApiClient with the specified base URL and configuration.- Parameters:
baseUrl- The base URL for API requestsconfig- The RocketRest configuration
-
FluentApiClient
public FluentApiClient(String baseUrl, RocketRestConfig config, FluentHttpClient fluentClient)
Creates a new FluentApiClient with the specified base URL, configuration, and a pre-configured FluentHttpClient instance.- Parameters:
baseUrl- The base URL for API requestsconfig- The RocketRest configurationfluentClient- A pre-configured FluentHttpClient instance
-
-
Method Detail
-
executeWithResult
public <Req,Res> Result<Res,ApiError> executeWithResult(RequestSpec<Req,Res> requestSpec)
Executes a request and returns a Result object instead of throwing exceptions.- Type Parameters:
Req- The type of the requestRes- The type of the response- Parameters:
requestSpec- The request specification- Returns:
- A Result containing either the response or an ApiError
-
get
public <Res> Result<Res,ApiError> get(String endpoint, Class<Res> responseType)
Performs a GET request using the Result pattern.- Type Parameters:
Res- The type of the response- Parameters:
endpoint- The API endpointresponseType- The class of the response type- Returns:
- A Result containing either the response or an ApiError
-
get
public <Res> Result<Res,ApiError> get(String endpoint, Map<String,String> headers, Class<Res> responseType)
Performs a GET request with headers using the Result pattern.- Type Parameters:
Res- The type of the response- Parameters:
endpoint- The API endpointheaders- The request headersresponseType- The class of the response type- Returns:
- A Result containing either the response or an ApiError
-
post
public <Req,Res> Result<Res,ApiError> post(String endpoint, Req body, Class<Res> responseType)
Performs a POST request using the Result pattern.- Type Parameters:
Req- The type of the request bodyRes- The type of the response- Parameters:
endpoint- The API endpointbody- The request bodyresponseType- The class of the response type- Returns:
- A Result containing either the response or an ApiError
-
post
public <Req,Res> Result<Res,ApiError> post(String endpoint, Req body, Map<String,String> headers, Class<Res> responseType)
Performs a POST request with headers using the Result pattern.- Type Parameters:
Req- The type of the request bodyRes- The type of the response- Parameters:
endpoint- The API endpointbody- The request bodyheaders- The request headersresponseType- The class of the response type- Returns:
- A Result containing either the response or an ApiError
-
put
public <Req,Res> Result<Res,ApiError> put(String endpoint, Req body, Class<Res> responseType)
Performs a PUT request using the Result pattern.- Type Parameters:
Req- The type of the request bodyRes- The type of the response- Parameters:
endpoint- The API endpointbody- The request bodyresponseType- The class of the response type- Returns:
- A Result containing either the response or an ApiError
-
put
public <Req,Res> Result<Res,ApiError> put(String endpoint, Req body, Map<String,String> headers, Class<Res> responseType)
Performs a PUT request with headers using the Result pattern.- Type Parameters:
Req- The type of the request bodyRes- The type of the response- Parameters:
endpoint- The API endpointbody- The request bodyheaders- The request headersresponseType- The class of the response type- Returns:
- A Result containing either the response or an ApiError
-
delete
public <Res> Result<Res,ApiError> delete(String endpoint, Class<Res> responseType)
Performs a DELETE request using the Result pattern.- Type Parameters:
Res- The type of the response- Parameters:
endpoint- The API endpointresponseType- The class of the response type- Returns:
- A Result containing either the response or an ApiError
-
delete
public <Res> Result<Res,ApiError> delete(String endpoint, Map<String,String> headers, Class<Res> responseType)
Performs a DELETE request with headers using the Result pattern.- Type Parameters:
Res- The type of the response- Parameters:
endpoint- The API endpointheaders- The request headersresponseType- The class of the response type- Returns:
- A Result containing either the response or an ApiError
-
shutdown
public void shutdown()
Shutdown the client and release resources.
-
-