Skip to content

Commit 6a6739a

Browse files
committed
#51 - Add to HttpClientContext.Builder to expose state of builder (getters) and configureWith(BeanScope)
1 parent 2a9649b commit 6a6739a

File tree

6 files changed

+217
-32
lines changed

6 files changed

+217
-32
lines changed

client/pom.xml

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
<tag>HEAD</tag>
1818
</scm>
1919

20+
<properties>
21+
<surefire.useModulePath>false</surefire.useModulePath>
22+
</properties>
23+
2024
<dependencies>
2125

2226
<dependency>
@@ -29,7 +33,14 @@
2933
<dependency>
3034
<groupId>io.avaje</groupId>
3135
<artifactId>avaje-jsonb</artifactId>
32-
<version>0.15</version>
36+
<version>1.0-RC1</version>
37+
<optional>true</optional>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>io.avaje</groupId>
42+
<artifactId>avaje-inject</artifactId>
43+
<version>8.6</version>
3344
<optional>true</optional>
3445
</dependency>
3546

@@ -56,13 +67,6 @@
5667
<scope>test</scope>
5768
</dependency>
5869

59-
<dependency>
60-
<groupId>io.avaje</groupId>
61-
<artifactId>avaje-inject</artifactId>
62-
<version>8.3</version>
63-
<scope>test</scope>
64-
</dependency>
65-
6670
<dependency>
6771
<groupId>io.avaje</groupId>
6872
<artifactId>avaje-http-api</artifactId>
@@ -85,40 +89,54 @@
8589
</dependency>
8690

8791
<!-- test annotation processors -->
88-
89-
<dependency>
90-
<groupId>io.avaje</groupId>
91-
<artifactId>avaje-inject-generator</artifactId>
92-
<version>8.3</version>
93-
<scope>test</scope>
94-
</dependency>
95-
9692
<!-- <dependency>-->
9793
<!-- <groupId>io.avaje</groupId>-->
98-
<!-- <artifactId>avaje-jsonb-generator</artifactId>-->
99-
<!-- <version>0.5</version>-->
94+
<!-- <artifactId>avaje-inject-generator</artifactId>-->
95+
<!-- <version>8.6</version>-->
10096
<!-- <scope>test</scope>-->
10197
<!-- </dependency>-->
10298

10399
</dependencies>
104100

105101
<build>
106102
<plugins>
103+
<plugin>
104+
<groupId>org.apache.maven.plugins</groupId>
105+
<artifactId>maven-compiler-plugin</artifactId>
106+
<version>3.10.1</version>
107+
<executions>
108+
<execution>
109+
<id>default-testCompile</id>
110+
<configuration>
111+
<annotationProcessorPaths>
112+
<path>
113+
<groupId>io.avaje</groupId>
114+
<artifactId>avaje-inject-generator</artifactId>
115+
<version>8.6</version>
116+
</path>
117+
</annotationProcessorPaths>
118+
</configuration>
119+
</execution>
120+
</executions>
121+
</plugin>
122+
123+
107124
<plugin>
108125
<artifactId>maven-surefire-plugin</artifactId>
109-
<version>3.0.0-M4</version>
110-
<configuration>
111-
<argLine>
112-
--add-modules com.fasterxml.jackson.databind
113-
--add-modules io.avaje.jsonb
114-
--add-opens io.avaje.http.client/io.avaje.http.client=ALL-UNNAMED
115-
--add-opens io.avaje.http.client/org.example.webserver=ALL-UNNAMED
116-
--add-opens io.avaje.http.client/org.example.github=ALL-UNNAMED
117-
--add-opens io.avaje.http.client/org.example.webserver=com.fasterxml.jackson.databind
118-
--add-opens io.avaje.http.client/org.example.github=com.fasterxml.jackson.databind
119-
--add-opens io.avaje.http.client/org.example.github=io.avaje.jsonb
120-
</argLine>
121-
</configuration>
126+
<version>3.0.0-M6</version>
127+
<!-- <configuration>-->
128+
<!-- <argLine>-->
129+
<!-- &#45;&#45;add-modules com.fasterxml.jackson.databind-->
130+
<!-- &#45;&#45;add-modules io.avaje.jsonb-->
131+
<!-- &#45;&#45;add-opens io.avaje.http.client/io.avaje.http.client=ALL-UNNAMED-->
132+
<!-- &#45;&#45;add-opens io.avaje.http.client/org.example.webserver=ALL-UNNAMED-->
133+
<!-- &#45;&#45;add-opens io.avaje.http.client/org.example.github=ALL-UNNAMED-->
134+
<!-- &#45;&#45;add-opens io.avaje.http.client/org.example.webserver=com.fasterxml.jackson.databind-->
135+
<!-- &#45;&#45;add-opens io.avaje.http.client/org.example.github=com.fasterxml.jackson.databind-->
136+
<!-- &#45;&#45;add-opens io.avaje.http.client/org.example.github=io.avaje.jsonb-->
137+
<!-- &#45;&#45;add-opens io.avaje.http.client/org.example.github=io.avaje.inject-->
138+
<!-- </argLine>-->
139+
<!-- </configuration>-->
122140
</plugin>
123141

124142
</plugins>

client/src/main/java/io/avaje/http/client/DHttpClientContextBuilder.java

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package io.avaje.http.client;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import io.avaje.inject.BeanScope;
5+
import io.avaje.jsonb.Jsonb;
6+
37
import javax.net.ssl.SSLContext;
48
import javax.net.ssl.SSLParameters;
59
import java.net.Authenticator;
@@ -10,11 +14,12 @@
1014
import java.time.Duration;
1115
import java.util.ArrayList;
1216
import java.util.List;
17+
import java.util.Optional;
1318
import java.util.concurrent.Executor;
1419

1520
import static java.util.Objects.requireNonNull;
1621

17-
final class DHttpClientContextBuilder implements HttpClientContext.Builder {
22+
final class DHttpClientContextBuilder implements HttpClientContext.Builder.State {
1823

1924
private HttpClient client;
2025
private String baseUrl;
@@ -148,6 +153,39 @@ public HttpClientContext.Builder priority(int priority) {
148153
return this;
149154
}
150155

156+
@Override
157+
public State state() {
158+
return this;
159+
}
160+
161+
@Override
162+
public HttpClientContext.Builder configureWith(BeanScope beanScope) {
163+
if (bodyAdapter == null) {
164+
configureBodyAdapter(beanScope);
165+
}
166+
if (retryHandler == null) {
167+
configureRetryHandler(beanScope);
168+
}
169+
return this;
170+
}
171+
172+
private void configureRetryHandler(BeanScope beanScope) {
173+
beanScope.getOptional(RetryHandler.class)
174+
.ifPresent(this::retryHandler);
175+
}
176+
177+
private void configureBodyAdapter(BeanScope beanScope) {
178+
Optional<BodyAdapter> body = beanScope.getOptional(BodyAdapter.class);
179+
if (body.isPresent()) {
180+
bodyAdapter = body.get();
181+
} else if (beanScope.contains("io.avaje.jsonb.Jsonb")) {
182+
bodyAdapter = new JsonbBodyAdapter(beanScope.get(Jsonb.class));
183+
} else if (beanScope.contains("com.fasterxml.jackson.databind.ObjectMapper")) {
184+
ObjectMapper objectMapper = beanScope.get(ObjectMapper.class);
185+
bodyAdapter = new JacksonBodyAdapter(objectMapper);
186+
}
187+
}
188+
151189
@Override
152190
public HttpClientContext build() {
153191
requireNonNull(baseUrl, "baseUrl is not specified");
@@ -165,6 +203,36 @@ public HttpClientContext build() {
165203
return new DHttpClientContext(client, baseUrl, requestTimeout, bodyAdapter, retryHandler, buildListener(), authTokenProvider, buildIntercept());
166204
}
167205

206+
@Override
207+
public String baseUrl() {
208+
return baseUrl;
209+
}
210+
211+
@Override
212+
public BodyAdapter bodyAdapter() {
213+
return bodyAdapter;
214+
}
215+
216+
@Override
217+
public HttpClient client() {
218+
return client;
219+
}
220+
221+
@Override
222+
public boolean requestLogging() {
223+
return requestLogging;
224+
}
225+
226+
@Override
227+
public Duration requestTimeout() {
228+
return requestTimeout;
229+
}
230+
231+
@Override
232+
public RetryHandler retryHandler() {
233+
return retryHandler;
234+
}
235+
168236
private RequestListener buildListener() {
169237
if (listeners.isEmpty()) {
170238
return null;

client/src/main/java/io/avaje/http/client/HttpClientContext.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.avaje.http.client;
22

3+
import io.avaje.inject.BeanScope;
4+
35
import javax.net.ssl.SSLContext;
46
import javax.net.ssl.SSLParameters;
57
import java.net.Authenticator;
@@ -311,6 +313,16 @@ interface Builder {
311313
*/
312314
Builder priority(int priority);
313315

316+
/**
317+
* Configure BodyAdapter and RetryHandler using dependency injection BeanScope.
318+
*/
319+
Builder configureWith(BeanScope beanScope);
320+
321+
/**
322+
* Return the state of the builder.
323+
*/
324+
State state();
325+
314326
/**
315327
* Build and return the context.
316328
*
@@ -330,6 +342,42 @@ interface Builder {
330342
* }</pre>
331343
*/
332344
HttpClientContext build();
345+
346+
/**
347+
* The state of the builder with methods to read the set state.
348+
*/
349+
interface State extends Builder {
350+
351+
/**
352+
* Return the base URL.
353+
*/
354+
String baseUrl();
355+
356+
/**
357+
* Return the body adapter.
358+
*/
359+
BodyAdapter bodyAdapter();
360+
361+
/**
362+
* Return the HttpClient.
363+
*/
364+
HttpClient client();
365+
366+
/**
367+
* Return true if requestLogging is on.
368+
*/
369+
boolean requestLogging();
370+
371+
/**
372+
* Return the request timeout.
373+
*/
374+
Duration requestTimeout();
375+
376+
/**
377+
* Return the retry handler.
378+
*/
379+
RetryHandler retryHandler();
380+
}
333381
}
334382

335383
/**

client/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
requires static com.fasterxml.jackson.annotation;
88
requires static com.fasterxml.jackson.core;
99
requires static io.avaje.jsonb;
10+
requires static io.avaje.inject;
1011

1112
exports io.avaje.http.client;
1213
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.example.dinject;
2+
3+
import io.avaje.http.client.BodyAdapter;
4+
import io.avaje.http.client.HttpClientContext;
5+
import io.avaje.http.client.JsonbBodyAdapter;
6+
import io.avaje.inject.BeanScope;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.time.Duration;
10+
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
13+
class ConfigureWithDITest {
14+
15+
@Test
16+
void configureWith() {
17+
try (BeanScope beanScope = BeanScope.builder().build()) {
18+
19+
assertThat(beanScope.contains("io.avaje.jsonb.Jsonb")).isTrue();
20+
21+
HttpClientContext.Builder builder = HttpClientContext.builder();
22+
HttpClientContext.Builder.State state = builder.state();
23+
assertThat(state.baseUrl()).isNull();
24+
assertThat(state.bodyAdapter()).isNull();
25+
assertThat(state.client()).isNull();
26+
assertThat(state.requestLogging()).isTrue();
27+
assertThat(state.requestTimeout()).isEqualByComparingTo(Duration.ofSeconds(20));
28+
assertThat(state.retryHandler()).isNull();
29+
30+
builder.configureWith(beanScope);
31+
BodyAdapter bodyAdapter = state.bodyAdapter();
32+
assertThat(bodyAdapter).isInstanceOf(JsonbBodyAdapter.class);
33+
}
34+
}
35+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.example.dinject;
2+
3+
import io.avaje.inject.Bean;
4+
import io.avaje.inject.Factory;
5+
import io.avaje.jsonb.Jsonb;
6+
7+
@Factory
8+
class MyDIConfig {
9+
10+
@Bean
11+
Jsonb jsonb() {
12+
return Jsonb.builder().build();
13+
}
14+
15+
}

0 commit comments

Comments
 (0)