Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions http-client/src/main/java/io/avaje/http/client/DHttpApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ final class DHttpApi {
private final Map<Class<?>, HttpApiProvider<?>> providerMap = new HashMap<>();

DHttpApi() {
init();
this(Thread.currentThread().getContextClassLoader());
}

void init() {
for (final var apiProvider : ServiceLoader.load(GeneratedComponent.class)) {
DHttpApi(ClassLoader classLoader) {
for (final var apiProvider : ServiceLoader.load(GeneratedComponent.class, classLoader)) {
apiProvider.register(providerMap);
}
log.log(DEBUG, "providers for {0}", providerMap.keySet());
Expand All @@ -46,4 +46,9 @@ <T> T provideFor(Class<T> type, HttpClient httpClient) {
static <T> T get(Class<T> type, HttpClient httpClient) {
return INSTANCE.provideFor(type, httpClient);
}

static <T> T get(Class<T> clientInterface, HttpClient httpClient, ClassLoader classLoader) {
return new DHttpApi(classLoader).provideFor(clientInterface, httpClient);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public <T> T create(Class<T> clientInterface) {
return DHttpApi.get(clientInterface, this);
}

@Override
public <T> T create(Class<T> clientInterface, ClassLoader classLoader) {
return DHttpApi.get(clientInterface, this, classLoader);
}

@Override
public HttpClientRequest request() {
return retryHandler == null
Expand Down
10 changes: 10 additions & 0 deletions http-client/src/main/java/io/avaje/http/client/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ static Builder builder() {
*/
<T> T create(Class<T> clientInterface);

/**
* Return the http client API implementation using the given ClassLoader.
*
* @param clientInterface A <code>@Client</code> interface with annotated API methods.
* @param classLoader The ClassLoader to use to service load the implementation
* @param <T> The service type.
* @return The http client API implementation.
*/
<T> T create(Class<T> clientInterface, ClassLoader classLoader);

/**
* Create a new request.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ class DHttpApiTest {
@Test
void test_github_listRepos() {

final var clientContext = HttpClient.builder()
final var client = HttpClient.builder()
.baseUrl("https://api.github.com")
.bodyAdapter(new JacksonBodyAdapter())
.build();

DHttpApi httpApi = new DHttpApi();
httpApi.addProvider(Simple.class, Simple$HttpClient::new);
final Simple simple = httpApi.provideFor(Simple.class, clientContext);
final Simple simple = httpApi.provideFor(Simple.class, client);

final List<Repo> repos = simple.listRepos("rbygrave", "junk");
assertThat(repos).isNotEmpty();
Expand All @@ -44,7 +44,7 @@ void jsonb_github_listRepos() {
.bodyAdapter(new JsonbBodyAdapter(jsonb))
.build();

DHttpApi httpApi = new DHttpApi();
DHttpApi httpApi = new DHttpApi(Thread.currentThread().getContextClassLoader());
httpApi.addProvider(Simple.class, Simple$HttpClient::new);
final Simple simple = httpApi.provideFor(Simple.class, client);

Expand Down