Skip to content

Commit

Permalink
Add @ClientFormParam to Reactive REST Client
Browse files Browse the repository at this point in the history
  • Loading branch information
Eng-Fouad committed Jul 6, 2023
1 parent e7e1a8a commit ea5c291
Show file tree
Hide file tree
Showing 12 changed files with 731 additions and 77 deletions.
89 changes: 82 additions & 7 deletions docs/src/main/asciidoc/rest-client-reactive.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,82 @@ Note that if an interface method contains an argument annotated with `@QueryPara
priority over anything specified in any `@ClientQueryParam` annotation.
====

More information about this annotation can be found on the javadoc of
More information about this annotation can be found on the javadoc of link:https://javadoc.io/doc/io.quarkus/quarkus-rest-client-reactive/latest/io/quarkus/rest/client/reactive/ClientQueryParam.html[`@ClientQueryParam`].

=== Form Parameters

Form parameters can be specified using `@RestForm` (or `@FormParam`) annotations:

[source, java]
----
package org.acme.rest.client;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import org.jboss.resteasy.reactive.RestForm;
import jakarta.ws.rs.PORT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.FormParam;
import jakarta.ws.rs.core.MultivaluedMap;
import java.util.Map;
import java.util.Set;
@Path("/extensions")
@RegisterRestClient(configKey = "extensions-api")
public interface ExtensionsService {
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
Set<Extension> postId(@FormParam("id") Integer id);
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
Set<Extension> postName(@RestForm String name);
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
Set<Extension> postFilter(@RestForm Map<String, String> filter);
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
Set<Extension> postFilters(@RestForm MultivaluedMap<String, String> filters);
}
----

==== Using @ClientFormParam

Form parameters can also be specified using `@ClientFormParam`, similar to `@ClientQueryParam`:

[source, java]
----
@ClientFormParam(name = "my-param", value = "${my.property-value}")
public interface Client {
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
String postWithParam();
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@ClientFormParam(name = "some-other-param", value = "other")
String postWithOtherParam();
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@ClientFormParam(name = "param-from-method", value = "{with-param}")
String postFromMethod();
default String withParam(String name) {
if ("param-from-method".equals(name)) {
return "test";
}
throw new IllegalArgumentException();
}
}
----

More information about this annotation can be found on the javadoc of link:https://javadoc.io/doc/io.quarkus/quarkus-rest-client-reactive/latest/io/quarkus/rest/client/reactive/ClientFormParam.html[`@ClientFormParam`].

=== Path Parameters

Expand Down Expand Up @@ -520,7 +595,7 @@ A HTTP server can redirect a response to another location by sending a response
- `quarkus.rest-client.follow-redirects` to enable redirection for all REST clients.
- `quarkus.rest-client.<client-prefix>.follow-redirects` to enable redirection for a specific REST client.

If this property is true, then REST Client will perform a new request that it receives a redirection response from the HTTP server.
If this property is true, then REST Client will perform a new request that it receives a redirection response from the HTTP server.

Additionally, we can limit the number of redirections using the property "max-redirects".

Expand Down Expand Up @@ -994,7 +1069,7 @@ QuarkusRestClientBuilder.newBuilder()

=== Injecting the `jakarta.ws.rs.ext.Providers` instance in filters

The `jakarta.ws.rs.ext.Providers` is useful when we need to lookup the provider instances of the current client.
The `jakarta.ws.rs.ext.Providers` is useful when we need to lookup the provider instances of the current client.

We can get the `Providers` instance in our filters from the request context as follows:

Expand Down Expand Up @@ -1086,7 +1161,7 @@ NOTE: Methods annotated with `@ClientExceptionMapper` can also take a `java.lang

=== Using @Blocking annotation in exception mappers

In cases that warrant using `InputStream` as the return type of REST Client method (such as when large amounts of data need to be read):
In cases that warrant using `InputStream` as the return type of REST Client method (such as when large amounts of data need to be read):

[source, java]
----
Expand Down Expand Up @@ -1120,8 +1195,8 @@ public class MyResponseExceptionMapper implements ResponseExceptionMapper<Runtim
}
----

<1> With the `@Blocking` annotation, the MyResponseExceptionMapper exception mapper will be executed in the worker thread pool.
<2> Reading the entity is now allowed because we're executing the mapper on the worker thread pool.
<1> With the `@Blocking` annotation, the MyResponseExceptionMapper exception mapper will be executed in the worker thread pool.
<2> Reading the entity is now allowed because we're executing the mapper on the worker thread pool.

Note that you can also use the `@Blocking` annotation when using @ClientExceptionMapper:

Expand Down Expand Up @@ -1288,7 +1363,7 @@ As previously mentioned, the body parameter needs to be properly crafted by the

=== Receiving compressed messages
REST Client Reactive also supports receiving compressed messages using GZIP. You can enable the HTTP compression support by adding the property `quarkus.http.enable-compression=true`.
When this feature is enabled and a server returns a response that includes the header `Content-Encoding: gzip`, REST Client Reactive will automatically decode the content and proceed with the message handling.
When this feature is enabled and a server returns a response that includes the header `Content-Encoding: gzip`, REST Client Reactive will automatically decode the content and proceed with the message handling.

== Proxy support
REST Client Reactive supports sending requests through a proxy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ void forClass(MethodCreator ctor, AssignableResultHandle globalTarget,
* Called when a {@link jakarta.ws.rs.client.WebTarget} has been populated for a normal Client
*/
void forWebTarget(MethodCreator methodCreator, IndexView index, ClassInfo interfaceClass, MethodInfo method,
AssignableResultHandle webTarget, BuildProducer<GeneratedClassBuildItem> generatedClasses);
AssignableResultHandle webTarget, BuildProducer<GeneratedClassBuildItem> generatedClasses,
AssignableResultHandle formValues, boolean multipart);

/**
* Called when a {@link jakarta.ws.rs.client.WebTarget} has been populated for a sub Client
*/
void forSubResourceWebTarget(MethodCreator methodCreator, IndexView index, ClassInfo rootInterfaceClass,
ClassInfo subInterfaceClass,
MethodInfo rootMethod, MethodInfo subMethod, AssignableResultHandle webTarget,
BuildProducer<GeneratedClassBuildItem> generatedClasses);
BuildProducer<GeneratedClassBuildItem> generatedClasses, AssignableResultHandle formValues,
boolean multipart);

/**
* Method-level alterations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ A more full example of generated client (with sub-resource) can is at the bottom
for (JaxrsClientReactiveEnricherBuildItem enricher : enrichers) {
enricher.getEnricher()
.forWebTarget(methodCreator, index, interfaceClass, jandexMethod, methodTarget,
generatedClasses);
generatedClasses, formParams, multipart);
}

AssignableResultHandle builder = methodCreator.createVariable(Invocation.Builder.class);
Expand Down Expand Up @@ -1659,7 +1659,7 @@ private void handleSubResourceMethod(List<JaxrsClientReactiveEnricherBuildItem>
for (JaxrsClientReactiveEnricherBuildItem enricher : enrichers) {
enricher.getEnricher()
.forSubResourceWebTarget(subMethodCreator, index, interfaceClass, subInterface,
jandexMethod, jandexSubMethod, methodTarget, generatedClasses);
jandexMethod, jandexSubMethod, methodTarget, generatedClasses, formParams, multipart);
}

AssignableResultHandle builder = subMethodCreator.createVariable(Invocation.Builder.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.jboss.jandex.DotName;

import io.quarkus.rest.client.reactive.ClientExceptionMapper;
import io.quarkus.rest.client.reactive.ClientFormParam;
import io.quarkus.rest.client.reactive.ClientFormParams;
import io.quarkus.rest.client.reactive.ClientQueryParam;
import io.quarkus.rest.client.reactive.ClientQueryParams;
import io.quarkus.rest.client.reactive.ClientRedirectHandler;
Expand All @@ -27,6 +29,8 @@ public class DotNames {

public static final DotName CLIENT_QUERY_PARAM = DotName.createSimple(ClientQueryParam.class.getName());
public static final DotName CLIENT_QUERY_PARAMS = DotName.createSimple(ClientQueryParams.class.getName());
public static final DotName CLIENT_FORM_PARAM = DotName.createSimple(ClientFormParam.class.getName());
public static final DotName CLIENT_FORM_PARAMS = DotName.createSimple(ClientFormParams.class.getName());
public static final DotName REGISTER_CLIENT_HEADERS = DotName.createSimple(RegisterClientHeaders.class.getName());
public static final DotName CLIENT_REQUEST_FILTER = DotName.createSimple(ClientRequestFilter.class.getName());
public static final DotName CLIENT_RESPONSE_FILTER = DotName.createSimple(ClientResponseFilter.class.getName());
Expand Down
Loading

0 comments on commit ea5c291

Please sign in to comment.