Skip to content

Commit 758ee83

Browse files
committed
#11 - Include service loading into HttpClientContext.create() mechanism
1 parent 10e36fd commit 758ee83

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,31 @@ void addProvider(HttpApiProvider apiProvider) {
3434
providerMap.put(apiProvider.type(), apiProvider);
3535
}
3636

37+
@SuppressWarnings("unchecked")
38+
private <T> HttpApiProvider<T> lookup(Class<T> type) {
39+
return (HttpApiProvider<T>) providerMap.get(type);
40+
}
41+
3742
@SuppressWarnings("unchecked")
3843
<T> T provideFor(Class<T> type, HttpClientContext clientContext) {
39-
final HttpApiProvider<T> apiProvider = (HttpApiProvider<T>) providerMap.get(type);
44+
final HttpApiProvider<T> apiProvider = lookup(type);
4045
if (apiProvider == null) {
4146
throw new IllegalArgumentException("No registered HttpApiProvider for type: " + type);
4247
}
4348
return apiProvider.provide(clientContext);
4449
}
4550

51+
/**
52+
* Return the client implementation via service loading.
53+
*/
4654
static <T> T provide(Class<T> type, HttpClientContext clientContext) {
4755
return INSTANCE.provideFor(type, clientContext);
4856
}
57+
58+
/**
59+
* Return the HttpApiProvider for the client interface type or null if not registered.
60+
*/
61+
static <T> HttpApiProvider<T> get(Class<T> type) {
62+
return INSTANCE.lookup(type);
63+
}
4964
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public <T> T create(Class<T> clientInterface) {
4040
if (!clientInterface.isInterface()) {
4141
throw new IllegalArgumentException("API declarations must be interfaces.");
4242
}
43+
HttpApiProvider<T> apiProvider = DHttpApi.get(clientInterface);
44+
if (apiProvider != null) {
45+
return apiProvider.provide(this);
46+
}
4347
String implClassName = clientImplementationClassName(clientInterface);
4448
try {
4549
Class<?> serviceClass = Class.forName(implClassName);

0 commit comments

Comments
 (0)