Class FluentApiClient


  • public class FluentApiClient
    extends AbstractApiClient
    A fluent API client that uses the Result pattern 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
    • 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 requests
        config - 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 requests
        config - The RocketRest configuration
        fluentClient - 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 request
        Res - 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 endpoint
        responseType - 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 endpoint
        headers - The request headers
        responseType - 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 body
        Res - The type of the response
        Parameters:
        endpoint - The API endpoint
        body - The request body
        responseType - 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 body
        Res - The type of the response
        Parameters:
        endpoint - The API endpoint
        body - The request body
        headers - The request headers
        responseType - 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 body
        Res - The type of the response
        Parameters:
        endpoint - The API endpoint
        body - The request body
        responseType - 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 body
        Res - The type of the response
        Parameters:
        endpoint - The API endpoint
        body - The request body
        headers - The request headers
        responseType - 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 endpoint
        responseType - 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 endpoint
        headers - The request headers
        responseType - 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.