Skip to content

Commit

Permalink
Merge pull request #602 from fedinskiy/feature/reactive-rest-part-2
Browse files Browse the repository at this point in the history
Reactive REST client, part2
  • Loading branch information
Pablo Gonzalez Granados authored May 18, 2022
2 parents 16eca68 + 6ead928 commit 6a2386e
Show file tree
Hide file tree
Showing 23 changed files with 410 additions and 17 deletions.
27 changes: 27 additions & 0 deletions http/rest-client-reactive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,31 @@
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
<profiles>
<!-- Skipped on Windows as does not support Linux Containers / Testcontainers -->
<profile>
<id>skip-tests-on-windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package io.quarkus.ts.http.restclient.reactive;

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.annotation.RegisterClientHeaders;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import io.quarkus.ts.http.restclient.reactive.json.Book;
import io.smallrye.mutiny.Uni;

@RegisterRestClient
@Path("/books")
@RegisterClientHeaders
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public interface BookClient {

@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
Uni<Book> getBook(@QueryParam("title") String title, @QueryParam("author") String author);

@Path("/author")
AuthorClient getAuthor();

@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
interface AuthorClient {
@GET
@Path("/name")
Uni<String> getName(@QueryParam("author") String author);

@Path("profession")
ProfessionClient getProfession();
}

@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
interface ProfessionClient {
@GET
@Path("/name")
Uni<String> getName();

@Path("/wage")
WageClient getWage();
}

interface WageClient {
@GET
@Path("/amount")
String getAmount();

@Path("/currency")
CurrencyClient getCurrency();
}

interface CurrencyClient {
@GET
@Path("/name")
String getName();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
Expand All @@ -22,4 +23,31 @@ public Uni<Book> getByQueryMap(@QueryParam("param") Map<String, String> params)
.item(new Book(params.get("id"),
params.get("author")));
}

@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public Uni<Book> getBook(@QueryParam("title") String title, @QueryParam("author") String author) {
return Uni.createFrom().item(new Book(title, author));
}

@GET
@Path("/author/name")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public Uni<String> getAuthorName(@QueryParam("author") String author) {
return Uni.createFrom().item(author);
}

@GET
@Path("/author/profession/title")
public Uni<String> getProfession() {
return Uni.createFrom().item("writer");
}

@GET
@Path("/author/profession/wage/currency/name")
public Uni<String> getCurrency() {
return Uni.createFrom().item("USD");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,63 @@
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.rest.client.inject.RestClient;

import io.quarkus.ts.http.restclient.reactive.json.Book;
import io.quarkus.ts.http.restclient.reactive.json.IdBeanParam;
import io.quarkus.ts.http.restclient.reactive.json.JsonRestInterface;
import io.smallrye.mutiny.Uni;

@Path("/client/{id}/book")
@Path("/client/book")
public class ReactiveClientBookResource {

@Inject
@RestClient
ReactiveRestInterface restInterface;
JsonRestInterface restInterface;

@Inject
@RestClient
BookClient bookInterface;

@GET
@Path("/json")
@Path("/{id}/json")
@Produces(MediaType.APPLICATION_JSON)
public Uni<Book> getAsJson(@PathParam("id") String id) {
return restInterface.getAsJson(id);
}

@GET
@Path("/jsonByBeanParam")
@Path("/{id}/jsonByBeanParam")
@Produces(MediaType.APPLICATION_JSON)
public Uni<Book> getAsJsonByBeanParam(@PathParam("id") String id) {
return restInterface.getWithBeanParam(new IdBeanParam(id));
}

@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public Uni<Book> getResource(@QueryParam("title") String title, @QueryParam("author") String author) {
return bookInterface.getBook(title, author);
}

@GET
@Path("/author")
public Uni<String> getSubResource(@QueryParam("author") String author) {
return bookInterface.getAuthor().getName(author);
}

@GET
@Path("/profession")
public Uni<String> getSubSubResource() {
return bookInterface.getAuthor().getProfession().getName();
}

@GET
@Path("/currency")
public String getLastResource() {
return bookInterface.getAuthor().getProfession().getWage().getCurrency().getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@

import org.eclipse.microprofile.rest.client.inject.RestClient;

import io.quarkus.ts.http.restclient.reactive.OsUtils;
import io.smallrye.common.annotation.Blocking;
import io.smallrye.mutiny.Uni;

@Path("/file-client")
public class FileClientResource {
private static final String BIGGER_THAN_TWO_GIGABYTES = OsUtils.SIZE_2049MiB;
private final java.nio.file.Path FILE = Files.createTempFile("upload", "txt").toAbsolutePath();
private final java.nio.file.Path FILE = Files.createTempFile("upload", ".txt").toAbsolutePath();
private final FileClient client;
private final OsUtils utils;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.jboss.resteasy.reactive.MultipartForm;

import io.quarkus.logging.Log;
import io.quarkus.ts.http.restclient.reactive.OsUtils;
import io.smallrye.mutiny.Uni;

@Path("/file")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkus.ts.http.restclient.reactive;
package io.quarkus.ts.http.restclient.reactive.files;

import java.io.IOException;
import java.io.InputStream;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkus.ts.http.restclient.reactive;
package io.quarkus.ts.http.restclient.reactive.json;

import javax.ws.rs.PathParam;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkus.ts.http.restclient.reactive;
package io.quarkus.ts.http.restclient.reactive.json;

import javax.ws.rs.BeanParam;
import javax.ws.rs.GET;
Expand All @@ -10,13 +10,12 @@
import org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import io.quarkus.ts.http.restclient.reactive.json.Book;
import io.smallrye.mutiny.Uni;

@RegisterRestClient
@Path("/book/{id}")
@RegisterClientHeaders
public interface ReactiveRestInterface {
public interface JsonRestInterface {

@GET
@Path("/json")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.quarkus.ts.http.restclient.reactive.proxy;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import io.smallrye.mutiny.Uni;

@RegisterRestClient
@RegisterClientHeaders
public interface ProxyClient {

@GET
@Path("/")
@Produces(MediaType.TEXT_HTML)
Uni<String> getSite();

@GET
@Path("/example.txt")
@Produces(MediaType.TEXT_PLAIN)
Uni<String> getText();

@GET
@Path("/auth")
@Produces(MediaType.TEXT_PLAIN)
Uni<String> getAuthorized();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.quarkus.ts.http.restclient.reactive.proxy;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.rest.client.inject.RestClient;

import io.smallrye.mutiny.Uni;

@Path("/proxied")
public class ProxyResource {

@Inject
@RestClient
ProxyClient client;

@GET
@Path("/")
@Produces(MediaType.TEXT_HTML)
public Uni<String> getRoot() {
return client.getSite();
}

@GET
@Path("/banned")
@Produces(MediaType.TEXT_PLAIN)
public Uni<String> getBanned() {
return client.getText();
}

@GET
@Path("/authorization")
@Produces(MediaType.TEXT_PLAIN)
public Uni<String> getAuthorized() {
return client.getAuthorized();
}

}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
io.quarkus.ts.http.restclient.reactive.ReactiveRestInterface/mp-rest/url=http://localhost:${quarkus.http.port}
io.quarkus.ts.http.restclient.reactive.json.JsonRestInterface/mp-rest/url=http://localhost:${quarkus.http.port}
io.quarkus.ts.http.restclient.reactive.BookClient/mp-rest/url=http://localhost:${quarkus.http.port}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# new schema introduced in https://github.com/quarkusio/quarkus/pull/17220 . See classic.properties for old-style settings
quarkus.rest-client."io.quarkus.ts.http.restclient.reactive.ReactiveRestInterface".url=http://localhost:${quarkus.http.port}
quarkus.rest-client."io.quarkus.ts.http.restclient.reactive.json.JsonRestInterface".url=http://localhost:${quarkus.http.port}
quarkus.rest-client."io.quarkus.ts.http.restclient.reactive.files.FileClient".url=http://localhost:${quarkus.http.port}
quarkus.rest-client."io.quarkus.ts.http.restclient.reactive.BookClient".url=http://localhost:${quarkus.http.port}
quarkus.rest-client."io.quarkus.ts.http.restclient.reactive.BookClient.AuthorClient".url=http://localhost:${quarkus.http.port}
quarkus.rest-client.logging.scope=request-response
quarkus.log.category."org.jboss.resteasy.reactive.client.logging".level=DEBUG
quarkus.http.limits.max-body-size=3G
quarkus.rest-client.read-timeout = 120000
6 changes: 6 additions & 0 deletions http/rest-client-reactive/src/main/resources/proxy.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
quarkus.rest-client."io.quarkus.ts.http.restclient.reactive.proxy.ProxyClient".url=http://example.com
quarkus.rest-client."io.quarkus.ts.http.restclient.reactive.proxy.ProxyClient".proxy-address=localhost:8090
quarkus.rest-client."io.quarkus.ts.http.restclient.reactive.proxy.ProxyClient".proxy-user=proxyuser
quarkus.rest-client."io.quarkus.ts.http.restclient.reactive.proxy.ProxyClient".proxy-password=proxypassword
quarkus.rest-client.logging.scope=request-response
quarkus.log.category."org.jboss.resteasy.reactive.client.logging".level=DEBUG
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ClassicPropertiesIT {
@Test
public void shouldGetBookFromRestClientJson() {
Response response = app.given().with().pathParam("id", "123")
.get("/client/{id}/book/json");
.get("/client/book/{id}/json");
assertEquals(HttpStatus.SC_OK, response.statusCode());
assertEquals("Title in Json: 123", response.jsonPath().getString("title"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.quarkus.test.bootstrap.RestService;
import io.quarkus.test.scenarios.QuarkusScenario;
import io.quarkus.test.services.QuarkusApplication;
import io.quarkus.ts.http.restclient.reactive.files.OsUtils;
import io.restassured.response.Response;

@QuarkusScenario
Expand Down
Loading

0 comments on commit 6a2386e

Please sign in to comment.