Skip to content

Commit 61fe705

Browse files
pditommasoclaude
andcommitted
[release] Add lib-httpx module with HxClient (Http eXtended Client) and retry integration
This commit introduces a new lib-httpx module providing an enhanced HTTP client with automatic retry logic and JWT token refresh capabilities. Key Features: - **HxClient**: Enhanced HTTP client wrapping Java HttpClient with enterprise features - Automatic retry on configurable HTTP status codes (429, 500, 502, 503, 504) - Exponential backoff with jitter to prevent thundering herd problems - JWT token refresh on 401 Unauthorized responses - Thread-safe concurrent operation - Full async support with CompletableFuture - **HxConfig**: Flexible configuration builder - Implements Retryable.Config for seamless integration with lib-retry - withRetryConfig() method accepts any Retryable.Config instance - Configurable retry policies, timeouts, and JWT token settings - Support for custom HTTP status codes and token refresh endpoints - **HxTokenManager**: Thread-safe JWT token lifecycle management - Automatic Authorization header injection - OAuth 2.0 refresh token flow implementation - Coordinated token refresh to prevent race conditions Factory Methods: - HxClient.create() - default configuration - HxClient.create(HxConfig) - custom HTTP configuration - HxClient.create(Retryable.Config) - from generic retry config - HxClient.create(HttpClient, HxConfig) - custom client + config - HxClient.create(HttpClient, Retryable.Config) - custom client + retry config Changes to lib-retry: - Made Retryable.Config interface static to enable external implementation - Allows HxConfig to implement Retryable.Config without requiring enclosing instance Testing: - Comprehensive unit tests for all components - Integration tests with WireMock for retry and JWT refresh scenarios - Combined integration tests validating end-to-end functionality Project Structure: - Module renamed from lib-httpcli to lib-httpx for consistency with HxClient naming - Updated build configurations and CI workflows - Integrated into project documentation and dependency management The module follows the established patterns of the libseqera project and integrates seamlessly with the existing retry infrastructure while providing HTTP-specific enhancements for robust service-to-service communication. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Paolo Di Tommaso <paolo.ditommaso@gmail.com>
1 parent d1ad946 commit 61fe705

File tree

19 files changed

+2954
-2
lines changed

19 files changed

+2954
-2
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ jobs:
6969
bash publish.sh lib-data-stream-redis
7070
bash publish.sh lib-fixtures-redis
7171
bash publish.sh lib-lang
72+
bash publish.sh lib-httpx
7273
bash publish.sh lib-mail
7374
bash publish.sh lib-pool
7475
bash publish.sh lib-random

.github/workflows/generate-submit-dependencies.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
"lib-data-stream-redis",
2121
"lib-fixtures-redis",
2222
"lib-lang",
23+
"lib-httpx",
2324
"lib-mail",
2425
"lib-pool",
2526
"lib-random",

CLAUDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ This is a library collection (`libseqera`) containing reusable components for Se
2929
- **wave-utils**: Utility classes for Wave (Docker helpers, file operations, template rendering)
3030
- **lib-activator**: Conditional activation markers for enabling components based on infrastructure availability
3131
- **lib-crypto**: Cryptographic utilities (asymmetric encryption, HMAC signatures, secure tokens)
32+
- **lib-httpx**: Enhanced HTTP client with automatic retry logic and JWT token refresh
3233
- **lib-mail**: Email functionality with Micronaut integration
3334
- **lib-retry**: Retry mechanisms with exponential backoff
3435
- **lib-pool**: Simple object pooling utilities

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ A collection of reusable Java & Groovy libraries for Seqera platform components.
99
- **[wave-utils](wave-utils/)** - Utility classes for Wave (Docker helpers, file operations, template rendering)
1010
- **[lib-activator](lib-activator/)** - Conditional activation markers for enabling components based on infrastructure availability
1111
- **[lib-crypto](lib-crypto/)** - Cryptographic utilities (asymmetric encryption, HMAC signatures, secure tokens)
12+
- **[lib-httpx](lib-httpx/)** - Enhanced HTTP client with automatic retry logic and JWT token refresh
1213
- **[lib-mail](lib-mail/)** - Email functionality with Micronaut integration
1314
- **[lib-retry](lib-retry/)** - Retry mechanisms with exponential backoff
1415
- **[lib-pool](lib-pool/)** - Simple object pooling utilities

lib-httpx/README.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# lib-httpx
2+
3+
Enhanced HTTP client extension for Java `HttpClient` with built-in retry logic and JWT token refresh capabilities.
4+
5+
## Features
6+
7+
- **Retry Logic**: Automatic retry for configurable HTTP status codes (default: 429, 500, 502, 503, 504)
8+
- **JWT Token Refresh**: Automatic JWT token refresh when receiving 401 Unauthorized responses
9+
- **Configurable**: Customizable retry policies, timeouts, and token refresh settings
10+
- **Generic Integration**: Compatible with any `Retryable.Config` for flexible retry configuration
11+
- **Thread-safe**: Safe for concurrent use
12+
- **Async Support**: Support for both synchronous and asynchronous requests
13+
14+
## Usage
15+
16+
### Basic Usage
17+
18+
```groovy
19+
// Create with default configuration
20+
def client = HxClient.create()
21+
22+
// Create with custom configuration
23+
def config = HxConfig.builder()
24+
.withMaxAttempts(3)
25+
.withRetryStatusCodes([429, 503] as Set)
26+
.build()
27+
def client = HxClient.create(config)
28+
29+
// Make HTTP requests
30+
def request = HttpRequest.newBuilder()
31+
.uri(URI.create("https://api.example.com/data"))
32+
.GET()
33+
.build()
34+
35+
def response = client.send(request, HttpResponse.BodyHandlers.ofString())
36+
```
37+
38+
### JWT Token Configuration
39+
40+
```groovy
41+
def config = HxConfig.builder()
42+
.withJwtToken("your-jwt-token")
43+
.withRefreshToken("your-refresh-token")
44+
.withRefreshTokenUrl("https://api.example.com/oauth/token")
45+
.build()
46+
47+
def client = HxClient.create(config)
48+
```
49+
50+
### Custom Retry Configuration
51+
52+
```groovy
53+
def config = HxConfig.builder()
54+
.withMaxAttempts(5)
55+
.withDelay(Duration.ofSeconds(1))
56+
.withMaxDelay(Duration.ofMinutes(2))
57+
.withJitter(0.5)
58+
.withMultiplier(2.0)
59+
.withRetryStatusCodes([429, 500, 502, 503, 504] as Set)
60+
.build()
61+
```
62+
63+
### Integration with Existing Retry Configuration
64+
65+
```groovy
66+
// Use any existing Retryable.Config
67+
def retryConfig = Retryable.ofDefaults().config()
68+
def client = HxClient.create(retryConfig)
69+
70+
// Or combine with HTTP-specific settings
71+
def config = HxConfig.builder()
72+
.withRetryConfig(retryConfig)
73+
.withJwtToken("your-jwt-token")
74+
.withRetryStatusCodes([429, 503] as Set)
75+
.build()
76+
def client = HxClient.create(config)
77+
```
78+
79+
## Configuration Options
80+
81+
| Option | Description | Default |
82+
|--------|-------------|---------|
83+
| `maxAttempts` | Maximum number of retry attempts | 5 |
84+
| `delay` | Initial delay between retries | 500ms |
85+
| `maxDelay` | Maximum delay between retries | 30s |
86+
| `jitter` | Random jitter factor (0-1) | 0.25 |
87+
| `multiplier` | Exponential backoff multiplier | 2.0 |
88+
| `retryStatusCodes` | HTTP status codes to retry | [429, 500, 502, 503, 504] |
89+
| `tokenRefreshTimeout` | Timeout for token refresh requests | 30s |
90+
91+
## API Documentation
92+
93+
All classes and methods include comprehensive Javadoc documentation covering:
94+
95+
- **Class-level documentation**: Complete overview of functionality and usage patterns
96+
- **Method documentation**: Detailed parameter descriptions, return values, and behavior
97+
- **Usage examples**: Code samples showing common use cases
98+
- **Thread safety notes**: Concurrency guarantees and synchronization details
99+
- **Error handling**: Exception types and retry behavior
100+
101+
### Key Classes
102+
103+
- **`HxClient`** (Http eXtended Client): Main client class with retry and JWT functionality
104+
- **`HxConfig`**: Configuration builder with all available options including Retryable.Config integration
105+
- **`HxTokenManager`**: Thread-safe JWT token lifecycle management
106+
107+
## Dependencies
108+
109+
- `lib-retry`: Provides the underlying retry mechanism using Failsafe
110+
- `groovy-json`: For JSON processing during token refresh
111+
- `dev.failsafe:failsafe`: Core retry and circuit breaker library

lib-httpx/VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0.0

lib-httpx/build.gradle

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
plugins {
2+
id 'io.seqera.java-library-conventions'
3+
id 'io.seqera.groovy-library-conventions'
4+
}
5+
6+
group = 'io.seqera'
7+
version = "${project.file('VERSION').text.trim()}"
8+
9+
dependencies {
10+
implementation project(':lib-retry')
11+
implementation 'com.google.code.gson:gson:2.10.1'
12+
implementation 'dev.failsafe:failsafe:3.1.0'
13+
14+
testImplementation 'com.github.tomakehurst:wiremock:3.0.1'
15+
}

0 commit comments

Comments
 (0)