Class 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 RocketClient and converts exception-based errors into Result objects, 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 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 requests
        clientOptions - 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 to
        baseUrl - The base URL for all requests
        clientOptions - The client options
    • Method Detail

      • configureSsl

        public void configureSsl​(SSLContext sslContext)
        Description copied from interface: RocketClient
        Sets the SSL context to be used for HTTPS requests.
        Specified by:
        configureSsl in interface RocketClient
        Parameters:
        sslContext - The SSL context to use.
      • 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 body
        Res - 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: RocketClient
        Executes an HTTP request based on the provided request specification.
        Specified by:
        execute in interface RocketClient
        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.