Skip to content

Commit ec9b6b0

Browse files
committed
#27 - ENH: Add HttpClientRequest setAttribute() getAttribute() to pass custom attributes between RequestIntercept methods
1 parent b7ca81c commit ec9b6b0

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

http-client/client/src/main/java/io/avaje/http/client/DHttpClientRequest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class DHttpClientRequest implements HttpClientRequest, HttpClientResponse {
5252
private boolean suppressLogging;
5353
private long startAsyncNanos;
5454
private String label;
55+
private Map<String, Object> customAttributes;
5556

5657
DHttpClientRequest(DHttpClientContext context, Duration requestTimeout) {
5758
this.context = context;
@@ -83,6 +84,21 @@ public String label() {
8384
return label;
8485
}
8586

87+
@Override
88+
public HttpClientRequest setAttribute(String key, Object value) {
89+
if (customAttributes == null) {
90+
customAttributes = new HashMap<>();
91+
}
92+
customAttributes.put(key, value);
93+
return this;
94+
}
95+
96+
@SuppressWarnings("unchecked")
97+
@Override
98+
public <E> E getAttribute(String key) {
99+
return customAttributes == null ? null : (E) customAttributes.get(key);
100+
}
101+
86102
@Override
87103
public HttpClientRequest requestTimeout(Duration requestTimeout) {
88104
this.requestTimeout = requestTimeout;

http-client/client/src/main/java/io/avaje/http/client/HttpClientRequest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@ public interface HttpClientRequest {
6161
*/
6262
String label();
6363

64+
/**
65+
* Used to pass custom attribute between {@link RequestIntercept} methods.
66+
* <p>
67+
* Allows us to pass something between {@code beforeRequest} and {@code afterResponse}
68+
* methods of a {@link RequestIntercept} or between multiple {@link RequestIntercept}.
69+
*
70+
* @param key The unique key used to store the attribute
71+
* @param value The attribute to store
72+
*/
73+
HttpClientRequest setAttribute(String key, Object value);
74+
75+
/**
76+
* Return a custom attribute typically set by a {@link RequestIntercept#beforeRequest(HttpClientRequest)}.
77+
*
78+
* @param key The key for the custom attribute
79+
* @param <E> The inferred type of the attribute
80+
* @return The custom attribute
81+
*/
82+
<E> E getAttribute(String key);
83+
6484
/**
6585
* Set the request timeout to use for this request. When not set the default
6686
* request timeout will be used.

http-client/client/src/test/java/io/avaje/http/client/RetryTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,35 @@ void retryTest() {
3333
assertThat(res.body()).isEqualTo("All good at 3rd attempt");
3434

3535
assertThat(myIntercept.responseTimeMicros).isGreaterThan(1);
36+
assertThat(myIntercept.customAttributeTimeMillis).isGreaterThan(1);
37+
3638
assertThat(myIntercept.counter).isEqualTo(1);
3739
assertThat(myIntercept.label).isEqualTo("http_client_hello_retry");
3840
}
3941

4042
static class MyIntercept implements RequestIntercept {
4143
int counter;
4244
long responseTimeMicros;
45+
long customAttributeTimeMillis;
4346
String label;
4447

48+
@Override
49+
public void beforeRequest(HttpClientRequest request) {
50+
final String label = request.setAttribute("MY_START_TIME", System.currentTimeMillis()).label();
51+
assertThat(label).isEqualTo("http_client_hello_retry");
52+
}
53+
4554
/**
4655
* Not called for the retry attempts. Only called on the final success or error response.
4756
*/
4857
@Override
4958
public void afterResponse(HttpResponse<?> response, HttpClientRequest request) {
59+
60+
final String does_not_exist = request.getAttribute("DOES_NOT_EXIST");
61+
assertThat(does_not_exist).isNull();
62+
63+
long start = request.getAttribute("MY_START_TIME");
64+
customAttributeTimeMillis = System.currentTimeMillis() - start;
5065
counter++;
5166
responseTimeMicros = request.responseTimeMicros();
5267
label = request.label();

0 commit comments

Comments
 (0)