-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
These factories should make it easier for developers to obtain a RestClientBuilder instance preconfigured according to current application config. The builder can be further tweaked and then used to programatically build a rest client proxy.
- Loading branch information
1 parent
23d2600
commit 95cfff7
Showing
12 changed files
with
190 additions
and
6 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
...t-config/runtime/src/main/java/io/quarkus/restclient/config/RestClientBuilderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.quarkus.restclient.config; | ||
|
||
import org.eclipse.microprofile.rest.client.RestClientBuilder; | ||
|
||
import java.util.ServiceLoader; | ||
|
||
/** | ||
* Factory which creates MicroProfile RestClientBuilder instance configured according to current Quarkus application | ||
* configuration. | ||
* | ||
* The builder instance can be further tweaked, if needed, before building the rest client proxy. | ||
*/ | ||
public interface RestClientBuilderFactory { | ||
|
||
default RestClientBuilder newBuilder(Class<?> proxyType) { | ||
return newBuilder(proxyType, RestClientsConfig.getInstance()); | ||
} | ||
|
||
RestClientBuilder newBuilder(Class<?> proxyType, RestClientsConfig restClientsConfigRoot); | ||
|
||
static RestClientBuilderFactory getInstance() { | ||
ServiceLoader<RestClientBuilderFactory> sl = ServiceLoader.load(RestClientBuilderFactory.class); | ||
RestClientBuilderFactory instance = null; | ||
for (RestClientBuilderFactory spi: sl) { | ||
if (instance != null) { | ||
throw new IllegalStateException("Multiple RestClientBuilderFactory implementations found: " | ||
+ spi.getClass().getName() + " and " | ||
+ instance.getClass().getName()); | ||
} | ||
instance = spi; | ||
} | ||
return instance; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...rc/test/java/io/quarkus/restclient/configuration/ClassicRestClientBuilderFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.quarkus.restclient.configuration; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.eclipse.microprofile.rest.client.RestClientBuilder; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.restclient.config.RestClientBuilderFactory; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ClassicRestClientBuilderFactoryTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> | ||
jar.addClasses(EchoClientWithoutAnnotation.class, EchoClientWithConfigKey.class, EchoResource.class)) | ||
.withConfigurationResource("factory-test-application.properties"); | ||
|
||
|
||
@Test | ||
public void testAnnotatedClientClass() { | ||
RestClientBuilder restClientBuilder = RestClientBuilderFactory.getInstance().newBuilder(EchoClientWithConfigKey.class); | ||
EchoClientWithConfigKey restClient = restClientBuilder.build(EchoClientWithConfigKey.class); | ||
|
||
assertThat(restClient.echo("Hello")).contains("Hello"); | ||
} | ||
|
||
@Test | ||
public void testNotAnnotatedClientClass() { | ||
RestClientBuilder restClientBuilder = RestClientBuilderFactory.getInstance().newBuilder(EchoClientWithoutAnnotation.class); | ||
EchoClientWithoutAnnotation restClient = restClientBuilder.build(EchoClientWithoutAnnotation.class); | ||
|
||
assertThat(restClient.echo("Hello")).contains("Hello"); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...oyment/src/test/java/io/quarkus/restclient/configuration/EchoClientWithoutAnnotation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.quarkus.restclient.configuration; | ||
|
||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
|
||
@Path("/echo") | ||
public interface EchoClientWithoutAnnotation { | ||
|
||
@GET | ||
@Produces(MediaType.TEXT_PLAIN) | ||
@Consumes(MediaType.TEXT_PLAIN) | ||
String echo(@QueryParam("message") String message); | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
...asy-classic/rest-client/deployment/src/test/resources/factory-test-application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
quarkus.rest-client.echo-client.url=http://localhost:8081 | ||
quarkus.rest-client.EchoClientWithoutAnnotation.url=http://localhost:8081 | ||
quarkus.rest-client.read-timeout=3456 |
26 changes: 26 additions & 0 deletions
26
.../runtime/src/main/java/io/quarkus/restclient/runtime/ClassicRestClientBuilderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.quarkus.restclient.runtime; | ||
|
||
import io.quarkus.restclient.config.RestClientBuilderFactory; | ||
import io.quarkus.restclient.config.RestClientsConfig; | ||
import org.eclipse.microprofile.rest.client.RestClientBuilder; | ||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
|
||
public class ClassicRestClientBuilderFactory implements RestClientBuilderFactory { | ||
|
||
public RestClientBuilder newBuilder(Class<?> proxyType, RestClientsConfig restClientsConfigRoot) { | ||
RegisterRestClient annotation = proxyType.getAnnotation(RegisterRestClient.class); | ||
String configKey = null; | ||
String baseUri = null; | ||
if (annotation != null) { | ||
configKey = annotation.configKey(); | ||
baseUri = annotation.baseUri(); | ||
} | ||
|
||
RestClientBuilder restClientBuilder = RestClientBuilder.newBuilder(); | ||
RestClientBase restClientBase = new RestClientBase(proxyType, baseUri, configKey, new Class[0], restClientsConfigRoot); | ||
restClientBase.configureBuilder(restClientBuilder); | ||
|
||
return restClientBuilder; | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...rc/main/resources/META-INF/services/io.quarkus.restclient.config.RestClientBuilderFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.quarkus.restclient.runtime.ClassicRestClientBuilderFactory |
34 changes: 34 additions & 0 deletions
34
...t/src/test/java/io/quarkus/rest/client/reactive/ReactiveRestClientBuilderFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.quarkus.rest.client.reactive; | ||
|
||
import java.util.Optional; | ||
|
||
import io.quarkus.rest.client.reactive.configuration.EchoResource; | ||
import org.eclipse.microprofile.rest.client.RestClientBuilder; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.restclient.config.RestClientBuilderFactory; | ||
import io.quarkus.restclient.config.RestClientConfig; | ||
import io.quarkus.restclient.config.RestClientsConfig; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class ReactiveRestClientBuilderFactoryTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar.addClasses(HelloClient2.class, EchoResource.class)) | ||
.withConfigurationResource("factory-test-application.properties"); | ||
|
||
|
||
@Test | ||
public void test() throws Exception { | ||
RestClientBuilder restClientBuilder = RestClientBuilderFactory.getInstance().newBuilder(HelloClient2.class); | ||
HelloClient2 restClient = restClientBuilder.build(HelloClient2.class); | ||
|
||
assertThat(restClientBuilder.getConfiguration().getProperties().get("io.quarkus.rest.client.read-timeout")) | ||
.isEqualTo(3456L); | ||
assertThat(restClient.echo("Hello")).contains("Hello"); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...ve/rest-client-reactive/deployment/src/test/resources/factory-test-application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
quarkus.rest-client.hello2.url=http://localhost:8081/hello | ||
quarkus.rest-client.read-timeout=3456 |
27 changes: 27 additions & 0 deletions
27
...c/main/java/io/quarkus/rest/client/reactive/runtime/ReactiveRestClientBuilderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.quarkus.rest.client.reactive.runtime; | ||
|
||
import org.eclipse.microprofile.rest.client.RestClientBuilder; | ||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
|
||
import io.quarkus.restclient.config.RestClientBuilderFactory; | ||
import io.quarkus.restclient.config.RestClientsConfig; | ||
|
||
public class ReactiveRestClientBuilderFactory implements RestClientBuilderFactory { | ||
|
||
public RestClientBuilder newBuilder(Class<?> proxyType, RestClientsConfig restClientsConfigRoot) { | ||
RegisterRestClient annotation = proxyType.getAnnotation(RegisterRestClient.class); | ||
String configKey = null; | ||
String baseUri = null; | ||
if (annotation != null) { | ||
configKey = annotation.configKey(); | ||
baseUri = annotation.baseUri(); | ||
} | ||
|
||
RestClientBuilderImpl restClientBuilder = new RestClientBuilderImpl(); | ||
RestClientCDIDelegateBuilder<?> restClientBase = new RestClientCDIDelegateBuilder<>(proxyType, baseUri, configKey, restClientsConfigRoot); | ||
restClientBase.configureBuilder(restClientBuilder); | ||
|
||
return restClientBuilder; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...rc/main/resources/META-INF/services/io.quarkus.restclient.config.RestClientBuilderFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.quarkus.rest.client.reactive.runtime.ReactiveRestClientBuilderFactory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters