Package com.guinetik.rr.http
Class RocketClientFactory
- java.lang.Object
-
- com.guinetik.rr.http.RocketClientFactory
-
public class RocketClientFactory extends Object
Factory for creating and configuring HTTP clients with decorators and settings.This factory provides a fluent builder API for constructing
RocketClientinstances with various configurations like circuit breakers, custom decorators, and async execution support.Simple Client
RocketClient client = RocketClientFactory.builder("https://api.example.com") .build();With Circuit Breaker
RocketClient client = RocketClientFactory.builder("https://api.example.com") .withCircuitBreaker(5, 30000) // 5 failures, 30s timeout .build();From Configuration
RocketRestConfig config = RocketRestConfig.builder("https://api.example.com") .authStrategy(AuthStrategyFactory.createBearerToken("token")) .build(); RocketClient client = RocketClientFactory.fromConfig(config) .withCircuitBreaker() .build();Async Client
ExecutorService executor = Executors.newFixedThreadPool(4); AsyncHttpClient asyncClient = RocketClientFactory.builder("https://api.example.com") .withExecutorService(executor) .buildAsync();Fluent Client (Result Pattern)
FluentHttpClient fluentClient = RocketClientFactory.builder("https://api.example.com") .buildFluent(); Result<User, ApiError> result = fluentClient.executeWithResult(request);Custom Decorator
RocketClient client = RocketClientFactory.builder("https://api.example.com") .withCircuitBreaker() .withCustomDecorator(base -> new LoggingClientDecorator(base)) .build();With Interceptors
// Add retry with exponential backoff RocketClient client = RocketClientFactory.builder("https://api.example.com") .withRetry(3, 1000) // 3 retries, 1s initial delay .build(); // Multiple interceptors RocketClient client = RocketClientFactory.builder("https://api.example.com") .withInterceptor(new LoggingInterceptor()) .withRetry(3, 1000, 2.0) // With exponential backoff .build();- Since:
- 1.0.0
- Author:
- guinetik <guinetik@gmail.com>
- See Also:
RocketClient,CircuitBreakerClient,FluentHttpClient,RequestInterceptor,RetryInterceptor
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classRocketClientFactory.BuilderBuilder class for constructing RocketClient instances with various decorators.
-
Constructor Summary
Constructors Constructor Description RocketClientFactory()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static RocketClientFactory.Builderbuilder(String baseUrl)Creates a builder for constructing RocketClient instances.static RocketClientcreateDefaultClient(RocketRestConfig config)Creates a default HTTP client with the given config.static FluentHttpClientcreateFluentClient(RocketRestConfig config)Creates a fluent HTTP client with the given config.static FluentHttpClientcreateFluentClient(String baseUrl)Creates a fluent HTTP client with the given base URL.static RocketClientFactory.BuilderfromConfig(RocketRestConfig config)Creates a builder from an existing RocketRestConfig.static voidresetToDefault()Resets the client provider to the default.static voidsetClientProvider(UnaryOperator<RocketRestConfig> provider)Sets a custom client provider for testing/mocking.
-
-
-
Method Detail
-
setClientProvider
public static void setClientProvider(UnaryOperator<RocketRestConfig> provider)
Sets a custom client provider for testing/mocking. This allows tests to inject mock clients.- Parameters:
provider- The provider function
-
resetToDefault
public static void resetToDefault()
Resets the client provider to the default.
-
builder
public static RocketClientFactory.Builder builder(String baseUrl)
Creates a builder for constructing RocketClient instances.- Parameters:
baseUrl- The base URL for the client- Returns:
- A new builder instance
-
fromConfig
public static RocketClientFactory.Builder fromConfig(RocketRestConfig config)
Creates a builder from an existing RocketRestConfig.- Parameters:
config- The RocketRestConfig to use- Returns:
- A new builder instance pre-configured with settings from the config
-
createDefaultClient
public static RocketClient createDefaultClient(RocketRestConfig config)
Creates a default HTTP client with the given config.- Parameters:
config- The RocketRestConfig to use- Returns:
- A new DefaultHttpClient instance
-
createFluentClient
public static FluentHttpClient createFluentClient(RocketRestConfig config)
Creates a fluent HTTP client with the given config. This client uses the Result pattern instead of exceptions.- Parameters:
config- The RocketRestConfig to use- Returns:
- A new FluentHttpClient instance
-
createFluentClient
public static FluentHttpClient createFluentClient(String baseUrl)
Creates a fluent HTTP client with the given base URL. This client uses the Result pattern instead of exceptions.- Parameters:
baseUrl- The base URL for the client- Returns:
- A new FluentHttpClient instance
-
-