Class RocketRest

  • Direct Known Subclasses:
    RocketRestMock

    public class RocketRest
    extends Object
    Main entry point for interacting with REST APIs using RocketRest.

    This class provides a unified facade for making HTTP requests with three different API styles:

    Quick Start

    
     // Create a client with simple URL
     RocketRest client = new RocketRest("https://api.example.com");
    
     // Make a GET request
     User user = client.get("/users/1", User.class);
    
     // Don't forget to shutdown when done
     client.shutdown();
     

    Using Different API Styles

    
     // Synchronous API - blocks until response
     Todo todo = client.sync().get("/todos/1", Todo.class);
    
     // Asynchronous API - returns CompletableFuture
     CompletableFuture<Todo> future = client.async().get("/todos/1", Todo.class);
     future.thenAccept(t -> System.out.println(t.getTitle()));
    
     // Fluent API with Result pattern - no exceptions
     Result<Todo, ApiError> result = client.fluent().get("/todos/1", Todo.class);
     result.match(
         todo -> System.out.println("Success: " + todo.getTitle()),
         error -> System.err.println("Error: " + error.getMessage())
     );
     

    Configuration

    
     RocketRestConfig config = RocketRestConfig.builder("https://api.example.com")
         .authStrategy(AuthStrategyFactory.createBearerToken("my-token"))
         .defaultOptions(opts -> {
             opts.set(RocketRestOptions.RETRY_ENABLED, true);
             opts.set(RocketRestOptions.MAX_RETRIES, 3);
         })
         .build();
    
     RocketRest client = new RocketRest(config);
     
    Since:
    1.0.0
    Author:
    guinetik <guinetik@gmail.com>
    See Also:
    RocketRestConfig, Result
    • Field Detail

      • logger

        protected final org.slf4j.Logger logger
    • Constructor Detail

      • RocketRest

        public RocketRest​(RocketRestConfig config)
        Creates a new RocketRest instance.
        Parameters:
        config - the configuration for the REST client.
      • RocketRest

        public RocketRest​(String baseUrl,
                          RocketRestConfig config)
        Creates a new RocketRest instance with a specific base URL.
        Parameters:
        baseUrl - the base URL of the API.
        config - the configuration for the REST client.
    • Method Detail

      • get

        public <T> T get​(String endpoint,
                         Class<T> responseType)
        Performs a synchronous GET request to the specified endpoint.
        Type Parameters:
        T - The response type
        Parameters:
        endpoint - The API endpoint
        responseType - The class of the response type
        Returns:
        The response object
      • get

        public <T> T get​(String endpoint,
                         Class<T> responseType,
                         Map<String,​String> queryParams)
        Performs a synchronous GET request with query parameters.
        Type Parameters:
        T - The response type
        Parameters:
        endpoint - The API endpoint
        responseType - The class of the response type
        queryParams - The query parameters
        Returns:
        The response object
      • post

        public <Res> Res post​(String endpoint,
                              Class<Res> responseType)
        Performs a synchronous POST request.
        Type Parameters:
        Res - The response type
        Parameters:
        endpoint - The API endpoint
        responseType - The class of the response type
        Returns:
        The response object
      • post

        public <Req,​Res> Res post​(String endpoint,
                                        Req body,
                                        Class<Res> responseType)
        Performs a synchronous POST request with a body.
        Type Parameters:
        Req - The request body type
        Res - The response type
        Parameters:
        endpoint - The API endpoint
        body - The request body
        responseType - The class of the response type
        Returns:
        The response object
      • put

        public <Res> Res put​(String endpoint,
                             Class<Res> responseType)
        Performs a synchronous PUT request.
        Type Parameters:
        Res - The response type
        Parameters:
        endpoint - The API endpoint
        responseType - The class of the response type
        Returns:
        The response object
      • put

        public <Req,​Res> Res put​(String endpoint,
                                       Req body,
                                       Class<Res> responseType)
        Performs a synchronous PUT request with a body.
        Type Parameters:
        Req - The request body type
        Res - The response type
        Parameters:
        endpoint - The API endpoint
        body - The request body
        responseType - The class of the response type
        Returns:
        The response object
      • delete

        public <T> T delete​(String endpoint,
                            Class<T> responseType)
        Performs a synchronous DELETE request.
        Type Parameters:
        T - The response type
        Parameters:
        endpoint - The API endpoint
        responseType - The class of the response type
        Returns:
        The response object
      • execute

        public <Req,​Res> Res execute​(RequestSpec<Req,​Res> requestSpec)
        Executes a synchronous request with the given request specification.
        Type Parameters:
        Req - The request body type
        Res - The response type
        Parameters:
        requestSpec - The request specification
        Returns:
        The response object
      • sync

        public RocketRest.SyncApi sync()
        Gets the synchronous API interface.
        Returns:
        The synchronous API interface
      • async

        public RocketRest.AsyncApi async()
        Gets the asynchronous API interface.
        Returns:
        The asynchronous API interface
      • fluent

        public RocketRest.FluentApi fluent()
        Gets the fluent API interface with Result pattern.
        Returns:
        The fluent API interface
      • configure

        public RocketRest configure​(String key,
                                    Object value)
        Configures a client option with the specified value.
        Parameters:
        key - The option key from ClientOptions.
        value - The option value.
        Returns:
        this client instance for method chaining
      • shutdown

        public void shutdown()
        Shuts down all resources used by this client.
      • setBaseUrl

        public void setBaseUrl​(String baseUrl)
      • getAccessToken

        public String getAccessToken()
      • getTokenExpiryTime

        public Date getTokenExpiryTime()