Package com.guinetik.rr.http
Class FluentHttpClient
- java.lang.Object
-
- com.guinetik.rr.http.FluentHttpClient
-
- All Implemented Interfaces:
RocketClient
public class FluentHttpClient extends Object implements RocketClient
HTTP client using the Result pattern for exception-free error handling.This client wraps any
RocketClientand converts exception-based errors intoResultobjects, enabling functional-style error handling.Benefits
- No exceptions to catch - errors are values
- Compile-time enforcement of error handling
- Functional composition with map, flatMap, fold
Basic Usage
FluentHttpClient client = new FluentHttpClient("https://api.example.com"); Result<User, ApiError> result = client.executeWithResult(request); // Pattern matching style result.match( user -> System.out.println("Success: " + user.getName()), error -> System.err.println("Error: " + error.getMessage()) ); // Or check and extract if (result.isSuccess()) { User user = result.getValue(); }Functional Composition
// Transform success value Result<String, ApiError> name = result.map(User::getName); // Provide default on error User userOrDefault = result.getOrElse(defaultUser);Via RocketRest
RocketRest client = new RocketRest(config); Result<User, ApiError> result = client.fluent() .get("/users/1", User.class);- Since:
- 1.0.0
- Author:
- guinetik <guinetik@gmail.com>
- See Also:
RocketClient,Result,RocketRest.fluent()
-
-
Constructor Summary
Constructors Constructor Description FluentHttpClient(RocketClient delegate, String baseUrl, RocketRestOptions clientOptions)Creates a new FluentHttpClient that delegates to the specified RocketClient.FluentHttpClient(String baseUrl)Creates a new FluentHttpClient with the specified base URL.FluentHttpClient(String baseUrl, RocketRestOptions clientOptions)Creates a new FluentHttpClient with the specified base URL and client options.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidconfigureSsl(SSLContext sslContext)Sets the SSL context to be used for HTTPS requests.<Req,Res>
Resexecute(RequestSpec<Req,Res> requestSpec)Executes an HTTP request based on the provided request specification.<Req,Res>
Result<Res,ApiError>executeWithResult(RequestSpec<Req,Res> requestSpec)Executes a request and returns a Result object containing either the response or an error.StringgetBaseUrl()Gets the base URL.RocketRestOptionsgetClientOptions()Gets the client options.voidsetBaseUrl(String baseUrl)
-
-
-
Constructor Detail
-
FluentHttpClient
public FluentHttpClient(String baseUrl)
Creates a new FluentHttpClient with the specified base URL.- Parameters:
baseUrl- The base URL for all requests
-
FluentHttpClient
public FluentHttpClient(String baseUrl, RocketRestOptions clientOptions)
Creates a new FluentHttpClient with the specified base URL and client options.- Parameters:
baseUrl- The base URL for all requestsclientOptions- The client options
-
FluentHttpClient
public FluentHttpClient(RocketClient delegate, String baseUrl, RocketRestOptions clientOptions)
Creates a new FluentHttpClient that delegates to the specified RocketClient.- Parameters:
delegate- The RocketClient to delegate requests tobaseUrl- The base URL for all requestsclientOptions- The client options
-
-
Method Detail
-
configureSsl
public void configureSsl(SSLContext sslContext)
Description copied from interface:RocketClientSets the SSL context to be used for HTTPS requests.- Specified by:
configureSslin interfaceRocketClient- Parameters:
sslContext- The SSL context to use.
-
setBaseUrl
public void setBaseUrl(String baseUrl)
- Specified by:
setBaseUrlin interfaceRocketClient
-
getClientOptions
public RocketRestOptions getClientOptions()
Gets the client options.- Returns:
- The client options
-
getBaseUrl
public String getBaseUrl()
Gets the base URL.- Returns:
- The base URL
-
executeWithResult
public <Req,Res> Result<Res,ApiError> executeWithResult(RequestSpec<Req,Res> requestSpec)
Executes a request and returns a Result object containing either the response or an error. This method is the primary API for executing requests in a functional way without exceptions.- Type Parameters:
Req- The type of the request bodyRes- The type of the response- Parameters:
requestSpec- The request specification- Returns:
- A Result object containing either the response or an error
-
execute
public <Req,Res> Res execute(RequestSpec<Req,Res> requestSpec) throws RocketRestException
Description copied from interface:RocketClientExecutes an HTTP request based on the provided request specification.- Specified by:
executein interfaceRocketClient- Type Parameters:
Req- The type of the request body.Res- The type of the response.- Parameters:
requestSpec- The specification of the request to be executed.- Returns:
- The response object.
- Throws:
RocketRestException- If an error occurs during the request execution.
-
-