-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce per invocation override of REST Client's base URL
This is done by introducing the @url annotation that can be placed on a method parameter. When this parameter is not null, it will be used as the base URL for the specific invocation Closes: #43316
- Loading branch information
Showing
8 changed files
with
353 additions
and
23 deletions.
There are no files selected for viewing
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
140 changes: 117 additions & 23 deletions
140
...c/main/java/io/quarkus/jaxrs/client/reactive/deployment/JaxrsClientReactiveProcessor.java
Large diffs are not rendered by default.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
...eployment/src/test/java/io/quarkus/rest/client/reactive/url/UrlOnStringParameterTest.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,64 @@ | ||
package io.quarkus.rest.client.reactive.url; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.net.UnknownHostException; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
|
||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.rest.client.reactive.Url; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class UrlOnStringParameterTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot( | ||
jar -> jar.addClasses(Resource.class, Client.class)) | ||
.overrideConfigKey("quarkus.rest-client.client.uri", "http://does-not-exist.io"); | ||
|
||
@RestClient | ||
Client client; | ||
|
||
@ConfigProperty(name = "quarkus.http.test-port") | ||
Integer testPort; | ||
|
||
@Test | ||
public void testOverride() { | ||
String result = client.test(String.format("http://localhost:%d", testPort)); | ||
assertEquals("bar", result); | ||
|
||
} | ||
|
||
@Test | ||
public void testNoOverride() { | ||
assertThatThrownBy(() -> client.test(null)).cause().isInstanceOf(UnknownHostException.class); | ||
} | ||
|
||
@Path("test") | ||
@RegisterRestClient(configKey = "client") | ||
public interface Client { | ||
|
||
@Path("count") | ||
@GET | ||
String test(@Url String uri); | ||
} | ||
|
||
@Path("test") | ||
public static class Resource { | ||
|
||
@GET | ||
@Path("count") | ||
public String test() { | ||
return "bar"; | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...t/deployment/src/test/java/io/quarkus/rest/client/reactive/url/UrlOnUriParameterTest.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,67 @@ | ||
package io.quarkus.rest.client.reactive.url; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.net.URI; | ||
import java.net.UnknownHostException; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
|
||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
import org.jboss.resteasy.reactive.RestQuery; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.rest.client.reactive.NotBody; | ||
import io.quarkus.rest.client.reactive.Url; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class UrlOnUriParameterTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot( | ||
jar -> jar.addClasses(Resource.class, Client.class)) | ||
.overrideConfigKey("quarkus.rest-client.\"client\".url", "http://does-not-exist.io"); | ||
|
||
@RestClient | ||
Client client; | ||
|
||
@ConfigProperty(name = "quarkus.http.test-port") | ||
Integer testPort; | ||
|
||
@Test | ||
public void testOverride() { | ||
String result = client.test("test", URI.create(String.format("http://localhost:%d", testPort)), "bar"); | ||
assertEquals("bar", result); | ||
|
||
} | ||
|
||
@Test | ||
public void testNoOverride() { | ||
assertThatThrownBy(() -> client.test("test", null, "bar")).cause().isInstanceOf(UnknownHostException.class); | ||
} | ||
|
||
@Path("test") | ||
@RegisterRestClient(configKey = "client") | ||
public interface Client { | ||
|
||
@Path("count") | ||
@GET | ||
String test(@NotBody String unused, @Url URI uri, @RestQuery String foo); | ||
} | ||
|
||
@Path("test") | ||
public static class Resource { | ||
|
||
@GET | ||
@Path("count") | ||
public String test(@RestQuery String foo) { | ||
return foo; | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...t/deployment/src/test/java/io/quarkus/rest/client/reactive/url/UrlOnUrlParameterTest.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,69 @@ | ||
package io.quarkus.rest.client.reactive.url; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.UnknownHostException; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
|
||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
import org.jboss.resteasy.reactive.RestHeader; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.rest.client.reactive.Url; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class UrlOnUrlParameterTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot( | ||
jar -> jar.addClasses(Resource.class, Client.class)) | ||
.overrideConfigKey( | ||
"quarkus.rest-client.\"io.quarkus.rest.client.reactive.url.UrlOnUrlParameterTest$Client\".uri", | ||
"http://does-not-exist.io"); | ||
|
||
@RestClient | ||
Client client; | ||
|
||
@ConfigProperty(name = "quarkus.http.test-port") | ||
Integer testPort; | ||
|
||
@Test | ||
public void testOverride() throws MalformedURLException { | ||
String result = client.test(new URL(String.format("http://localhost:%d/", testPort)), "bar"); | ||
assertEquals("bar", result); | ||
|
||
} | ||
|
||
@Test | ||
public void testNoOverride() { | ||
assertThatThrownBy(() -> client.test(null, "bar")).cause().isInstanceOf(UnknownHostException.class); | ||
} | ||
|
||
@Path("test") | ||
@RegisterRestClient(configKey = "client") | ||
public interface Client { | ||
|
||
@Path("count") | ||
@GET | ||
String test(@Url URL uri, @RestHeader String foo); | ||
} | ||
|
||
@Path("test") | ||
public static class Resource { | ||
|
||
@GET | ||
@Path("count") | ||
public String test(@RestHeader String foo) { | ||
return foo; | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...teasy-reactive/rest-client/runtime/src/main/java/io/quarkus/rest/client/reactive/Url.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,23 @@ | ||
package io.quarkus.rest.client.reactive; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Allows for a per invocation override the base URL. | ||
* At most a one such annotation can be used per REST Client method and the supported types are: | ||
* | ||
* <ul> | ||
* {@link String}</li> | ||
* {@link java.net.URI}</li> | ||
* {@link java.net.URL}</li> | ||
* </ul> | ||
*/ | ||
@Target({ ElementType.PARAMETER }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface Url { | ||
} |
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
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