Skip to content

Commit

Permalink
#51 - Add to HttpClientContext.Builder to expose state of builder (ge…
Browse files Browse the repository at this point in the history
…tters) and configureWith(BeanScope)
  • Loading branch information
rbygrave committed Jun 9, 2022
1 parent d86dec3 commit 2e7a0e4
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 32 deletions.
80 changes: 49 additions & 31 deletions http-client/client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<tag>HEAD</tag>
</scm>

<properties>
<surefire.useModulePath>false</surefire.useModulePath>
</properties>

<dependencies>

<dependency>
Expand All @@ -29,7 +33,14 @@
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jsonb</artifactId>
<version>0.15</version>
<version>1.0-RC1</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject</artifactId>
<version>8.6</version>
<optional>true</optional>
</dependency>

Expand All @@ -56,13 +67,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject</artifactId>
<version>8.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-http-api</artifactId>
Expand All @@ -85,40 +89,54 @@
</dependency>

<!-- test annotation processors -->

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-generator</artifactId>
<version>8.3</version>
<scope>test</scope>
</dependency>

<!-- <dependency>-->
<!-- <groupId>io.avaje</groupId>-->
<!-- <artifactId>avaje-jsonb-generator</artifactId>-->
<!-- <version>0.5</version>-->
<!-- <artifactId>avaje-inject-generator</artifactId>-->
<!-- <version>8.6</version>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<executions>
<execution>
<id>default-testCompile</id>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-generator</artifactId>
<version>8.6</version>
</path>
</annotationProcessorPaths>
</configuration>
</execution>
</executions>
</plugin>


<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<argLine>
--add-modules com.fasterxml.jackson.databind
--add-modules io.avaje.jsonb
--add-opens io.avaje.http.client/io.avaje.http.client=ALL-UNNAMED
--add-opens io.avaje.http.client/org.example.webserver=ALL-UNNAMED
--add-opens io.avaje.http.client/org.example.github=ALL-UNNAMED
--add-opens io.avaje.http.client/org.example.webserver=com.fasterxml.jackson.databind
--add-opens io.avaje.http.client/org.example.github=com.fasterxml.jackson.databind
--add-opens io.avaje.http.client/org.example.github=io.avaje.jsonb
</argLine>
</configuration>
<version>3.0.0-M6</version>
<!-- <configuration>-->
<!-- <argLine>-->
<!-- &#45;&#45;add-modules com.fasterxml.jackson.databind-->
<!-- &#45;&#45;add-modules io.avaje.jsonb-->
<!-- &#45;&#45;add-opens io.avaje.http.client/io.avaje.http.client=ALL-UNNAMED-->
<!-- &#45;&#45;add-opens io.avaje.http.client/org.example.webserver=ALL-UNNAMED-->
<!-- &#45;&#45;add-opens io.avaje.http.client/org.example.github=ALL-UNNAMED-->
<!-- &#45;&#45;add-opens io.avaje.http.client/org.example.webserver=com.fasterxml.jackson.databind-->
<!-- &#45;&#45;add-opens io.avaje.http.client/org.example.github=com.fasterxml.jackson.databind-->
<!-- &#45;&#45;add-opens io.avaje.http.client/org.example.github=io.avaje.jsonb-->
<!-- &#45;&#45;add-opens io.avaje.http.client/org.example.github=io.avaje.inject-->
<!-- </argLine>-->
<!-- </configuration>-->
</plugin>

</plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package io.avaje.http.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.avaje.inject.BeanScope;
import io.avaje.jsonb.Jsonb;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import java.net.Authenticator;
Expand All @@ -10,11 +14,12 @@
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Executor;

import static java.util.Objects.requireNonNull;

final class DHttpClientContextBuilder implements HttpClientContext.Builder {
final class DHttpClientContextBuilder implements HttpClientContext.Builder.State {

private HttpClient client;
private String baseUrl;
Expand Down Expand Up @@ -148,6 +153,39 @@ public HttpClientContext.Builder priority(int priority) {
return this;
}

@Override
public State state() {
return this;
}

@Override
public HttpClientContext.Builder configureWith(BeanScope beanScope) {
if (bodyAdapter == null) {
configureBodyAdapter(beanScope);
}
if (retryHandler == null) {
configureRetryHandler(beanScope);
}
return this;
}

private void configureRetryHandler(BeanScope beanScope) {
beanScope.getOptional(RetryHandler.class)
.ifPresent(this::retryHandler);
}

private void configureBodyAdapter(BeanScope beanScope) {
Optional<BodyAdapter> body = beanScope.getOptional(BodyAdapter.class);
if (body.isPresent()) {
bodyAdapter = body.get();
} else if (beanScope.contains("io.avaje.jsonb.Jsonb")) {
bodyAdapter = new JsonbBodyAdapter(beanScope.get(Jsonb.class));
} else if (beanScope.contains("com.fasterxml.jackson.databind.ObjectMapper")) {
ObjectMapper objectMapper = beanScope.get(ObjectMapper.class);
bodyAdapter = new JacksonBodyAdapter(objectMapper);
}
}

@Override
public HttpClientContext build() {
requireNonNull(baseUrl, "baseUrl is not specified");
Expand All @@ -165,6 +203,36 @@ public HttpClientContext build() {
return new DHttpClientContext(client, baseUrl, requestTimeout, bodyAdapter, retryHandler, buildListener(), authTokenProvider, buildIntercept());
}

@Override
public String baseUrl() {
return baseUrl;
}

@Override
public BodyAdapter bodyAdapter() {
return bodyAdapter;
}

@Override
public HttpClient client() {
return client;
}

@Override
public boolean requestLogging() {
return requestLogging;
}

@Override
public Duration requestTimeout() {
return requestTimeout;
}

@Override
public RetryHandler retryHandler() {
return retryHandler;
}

private RequestListener buildListener() {
if (listeners.isEmpty()) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.avaje.http.client;

import io.avaje.inject.BeanScope;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import java.net.Authenticator;
Expand Down Expand Up @@ -311,6 +313,16 @@ interface Builder {
*/
Builder priority(int priority);

/**
* Configure BodyAdapter and RetryHandler using dependency injection BeanScope.
*/
Builder configureWith(BeanScope beanScope);

/**
* Return the state of the builder.
*/
State state();

/**
* Build and return the context.
*
Expand All @@ -330,6 +342,42 @@ interface Builder {
* }</pre>
*/
HttpClientContext build();

/**
* The state of the builder with methods to read the set state.
*/
interface State extends Builder {

/**
* Return the base URL.
*/
String baseUrl();

/**
* Return the body adapter.
*/
BodyAdapter bodyAdapter();

/**
* Return the HttpClient.
*/
HttpClient client();

/**
* Return true if requestLogging is on.
*/
boolean requestLogging();

/**
* Return the request timeout.
*/
Duration requestTimeout();

/**
* Return the retry handler.
*/
RetryHandler retryHandler();
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions http-client/client/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
requires static com.fasterxml.jackson.annotation;
requires static com.fasterxml.jackson.core;
requires static io.avaje.jsonb;
requires static io.avaje.inject;

exports io.avaje.http.client;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.example.dinject;

import io.avaje.http.client.BodyAdapter;
import io.avaje.http.client.HttpClientContext;
import io.avaje.http.client.JsonbBodyAdapter;
import io.avaje.inject.BeanScope;
import org.junit.jupiter.api.Test;

import java.time.Duration;

import static org.assertj.core.api.Assertions.assertThat;

class ConfigureWithDITest {

@Test
void configureWith() {
try (BeanScope beanScope = BeanScope.builder().build()) {

assertThat(beanScope.contains("io.avaje.jsonb.Jsonb")).isTrue();

HttpClientContext.Builder builder = HttpClientContext.builder();
HttpClientContext.Builder.State state = builder.state();
assertThat(state.baseUrl()).isNull();
assertThat(state.bodyAdapter()).isNull();
assertThat(state.client()).isNull();
assertThat(state.requestLogging()).isTrue();
assertThat(state.requestTimeout()).isEqualByComparingTo(Duration.ofSeconds(20));
assertThat(state.retryHandler()).isNull();

builder.configureWith(beanScope);
BodyAdapter bodyAdapter = state.bodyAdapter();
assertThat(bodyAdapter).isInstanceOf(JsonbBodyAdapter.class);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.example.dinject;

import io.avaje.inject.Bean;
import io.avaje.inject.Factory;
import io.avaje.jsonb.Jsonb;

@Factory
class MyDIConfig {

@Bean
Jsonb jsonb() {
return Jsonb.builder().build();
}

}

0 comments on commit 2e7a0e4

Please sign in to comment.