Package com.guinetik.rr
Class RocketRest
- java.lang.Object
-
- com.guinetik.rr.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:
- Synchronous API - Traditional blocking calls via
sync() - Asynchronous API - Non-blocking calls with
CompletableFutureviaasync() - Fluent API - Functional error handling with
Resultpattern viafluent()
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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceRocketRest.AsyncApiInterface for asynchronous API operations.static interfaceRocketRest.FluentApiInterface for fluent API operations with a Result pattern.static interfaceRocketRest.SyncApiInterface for synchronous API operations.
-
Field Summary
Fields Modifier and Type Field Description protected org.slf4j.Loggerlogger
-
Constructor Summary
Constructors Constructor Description RocketRest(RocketRestConfig config)Creates a newRocketRestinstance.RocketRest(String baseUrl, RocketRestConfig config)Creates a newRocketRestinstance with a specific base URL.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RocketRest.AsyncApiasync()Gets the asynchronous API interface.RocketRestconfigure(String key, Object value)Configures a client option with the specified value.<T> Tdelete(String endpoint, Class<T> responseType)Performs a synchronous DELETE request.<Req,Res>
Resexecute(RequestSpec<Req,Res> requestSpec)Executes a synchronous request with the given request specification.RocketRest.FluentApifluent()Gets the fluent API interface with Result pattern.<T> Tget(String endpoint, Class<T> responseType)Performs a synchronous GET request to the specified endpoint.<T> Tget(String endpoint, Class<T> responseType, Map<String,String> queryParams)Performs a synchronous GET request with query parameters.StringgetAccessToken()DategetTokenExpiryTime()<Res> Respost(String endpoint, Class<Res> responseType)Performs a synchronous POST request.<Req,Res>
Respost(String endpoint, Req body, Class<Res> responseType)Performs a synchronous POST request with a body.<Res> Resput(String endpoint, Class<Res> responseType)Performs a synchronous PUT request.<Req,Res>
Resput(String endpoint, Req body, Class<Res> responseType)Performs a synchronous PUT request with a body.voidsetBaseUrl(String baseUrl)voidshutdown()Shuts down all resources used by this client.RocketRest.SyncApisync()Gets the synchronous API interface.
-
-
-
Constructor Detail
-
RocketRest
public RocketRest(RocketRestConfig config)
Creates a newRocketRestinstance.- Parameters:
config- the configuration for the REST client.
-
RocketRest
public RocketRest(String baseUrl, RocketRestConfig config)
Creates a newRocketRestinstance 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 endpointresponseType- 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 endpointresponseType- The class of the response typequeryParams- 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 endpointresponseType- 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 typeRes- The response type- Parameters:
endpoint- The API endpointbody- The request bodyresponseType- 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 endpointresponseType- 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 typeRes- The response type- Parameters:
endpoint- The API endpointbody- The request bodyresponseType- 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 endpointresponseType- 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 typeRes- 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()
-
-