forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rest Client Reactive: Add reactive flavor for ClientHeadersFactory
Added reactive flavor of `ClientHeadersFactory` that allows doing blocking operations. For example: [source, java] ---- package org.acme.rest.client; import org.eclipse.microprofile.rest.client.ext.ClientHeadersFactory; import javax.enterprise.context.ApplicationScoped; import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; import java.util.UUID; @ApplicationScoped public class GetTokenReactiveClientHeadersFactory extends ReactiveClientHeadersFactory { @Inject Service service; @OverRide public Uni<MultivaluedMap<String, String>> getHeaders(MultivaluedMap<String, String> incomingHeaders) { return Uni.createFrom().item(() -> { MultivaluedHashMap<String, String> newHeaders = new MultivaluedHashMap<>(); // perform blocking call newHeaders.add(HEADER_NAME, service.getToken()); return newHeaders; }); } } ---- Fixes quarkusio#20001
- Loading branch information
Showing
4 changed files
with
152 additions
and
5 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
86 changes: 86 additions & 0 deletions
86
...t/java/io/quarkus/rest/client/reactive/headers/ReactiveClientHeadersFromProviderTest.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,86 @@ | ||
package io.quarkus.rest.client.reactive.headers; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.net.URI; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.HeaderParam; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.core.MultivaluedHashMap; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
|
||
import org.eclipse.microprofile.rest.client.RestClientBuilder; | ||
import org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.rest.client.reactive.ReactiveClientHeadersFactory; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.test.common.http.TestHTTPResource; | ||
import io.smallrye.common.annotation.Blocking; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
public class ReactiveClientHeadersFromProviderTest { | ||
private static final String HEADER_NAME = "my-header"; | ||
private static final String HEADER_VALUE = "oifajrofijaeoir5gjaoasfaxcvcz"; | ||
|
||
@TestHTTPResource | ||
URI baseUri; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar.addClasses(ReactiveClientHeadersFromProviderTest.Client.class) | ||
.addAsResource( | ||
new StringAsset("my.property-value=" + HEADER_VALUE), | ||
"application.properties")); | ||
|
||
@Test | ||
void shouldSetHeaderFromProperties() { | ||
ReactiveClientHeadersFromProviderTest.Client client = RestClientBuilder.newBuilder().baseUri(baseUri) | ||
.build(ReactiveClientHeadersFromProviderTest.Client.class); | ||
|
||
assertThat(client.getWithHeader()).isEqualTo(HEADER_VALUE); | ||
} | ||
|
||
@Path("/") | ||
@ApplicationScoped | ||
public static class Resource { | ||
@GET | ||
public String returnHeaderValue(@HeaderParam(HEADER_NAME) String header) { | ||
return header; | ||
} | ||
} | ||
|
||
@ApplicationScoped | ||
public static class Service { | ||
@Blocking | ||
public String getValue() { | ||
return HEADER_VALUE; | ||
} | ||
} | ||
|
||
@RegisterClientHeaders(CustomReactiveClientHeadersFactory.class) | ||
public interface Client { | ||
@GET | ||
String getWithHeader(); | ||
} | ||
|
||
public static class CustomReactiveClientHeadersFactory extends ReactiveClientHeadersFactory { | ||
|
||
@Inject | ||
Service service; | ||
|
||
@Override | ||
public Uni<MultivaluedMap<String, String>> getHeaders(MultivaluedMap<String, String> incomingHeaders) { | ||
return Uni.createFrom().item(() -> { | ||
MultivaluedHashMap<String, String> newHeaders = new MultivaluedHashMap<>(); | ||
newHeaders.add(HEADER_NAME, service.getValue()); | ||
return newHeaders; | ||
}); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...e/runtime/src/main/java/io/quarkus/rest/client/reactive/ReactiveClientHeadersFactory.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.rest.client.reactive; | ||
|
||
import javax.ws.rs.core.MultivaluedMap; | ||
|
||
import org.eclipse.microprofile.rest.client.ext.ClientHeadersFactory; | ||
|
||
import io.smallrye.mutiny.Uni; | ||
|
||
/** | ||
* Reactive ClientHeadersFactory flavor for Quarkus rest-client reactive extension. | ||
*/ | ||
public abstract class ReactiveClientHeadersFactory implements ClientHeadersFactory { | ||
public abstract Uni<MultivaluedMap<String, String>> getHeaders(MultivaluedMap<String, String> incomingHeaders); | ||
|
||
@Override | ||
public final MultivaluedMap<String, String> update(MultivaluedMap<String, String> incomingHeaders, | ||
MultivaluedMap<String, String> clientOutgoingHeaders) { | ||
throw new RuntimeException("Can't call `update` method in a Reactive context. Use `getHeaders` or implements ClientHeadersFactory."); | ||
} | ||
} |
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