Search in sources :

Example 1 with WebClient

use of io.vertx.rxjava.ext.web.client.WebClient in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    WebClient client = WebClient.create(vertx);
    Single<HttpResponse<Data>> request = client.get(8080, "localhost", "/").as(BodyCodec.json(Data.class)).rxSend();
    // Fire the request
    request.subscribe(resp -> System.out.println("Server content " + resp.body().message));
    // Again
    request.subscribe(resp -> System.out.println("Server content " + resp.body().message));
    // And again
    request.subscribe(resp -> System.out.println("Server content " + resp.body().message));
}
Also used : HttpResponse(io.vertx.rxjava.ext.web.client.HttpResponse) WebClient(io.vertx.rxjava.ext.web.client.WebClient)

Example 2 with WebClient

use of io.vertx.rxjava.ext.web.client.WebClient in project vertx-openshift-it by cescoffier.

the class GatewayVerticle method start.

@Override
public void start() throws Exception {
    dnsClient = vertx.createHttpClient(new HttpClientOptions().setDefaultHost(ENDPOINT_NAME).setDefaultPort(8080).setKeepAlive(false));
    dnsWeb = WebClient.create(vertx, new WebClientOptions().setDefaultHost(ENDPOINT_NAME).setDefaultPort(8080).setKeepAlive(false));
    dnsDatabase = JDBCClient.createShared(vertx, new JsonObject().put("url", "jdbc:postgresql://my-database:5432/my_data").put("driver_class", "org.postgresql.Driver").put("user", "luke").put("password", "secret"));
    Router router = Router.router(vertx);
    router.get("/health").handler(rc -> rc.response().end("OK"));
    router.get("/services/http").handler(this::invokeHttpService);
    router.get("/services/web").handler(this::invokeWebService);
    router.get("/services/db").handler(this::checkDb);
    router.get("/dns/http").handler(this::invokeHttpServiceWithDns);
    router.get("/dns/web").handler(this::invokeWebServiceWithDns);
    router.get("/dns/db").handler(this::checkDbWithDns);
    router.get("/ref/http").handler(this::invokeHttpServiceWithRef);
    router.get("/ref/web").handler(this::invokeWebServiceWithRef);
    router.put("/webclient").handler(this::webclient);
    ServiceDiscovery.create(vertx, discovery -> {
        this.discovery = discovery;
        Single<WebClient> svc1 = HttpEndpoint.rxGetWebClient(discovery, svc -> svc.getName().equals(ENDPOINT_NAME), new JsonObject().put("keepAlive", false));
        Single<HttpClient> svc2 = HttpEndpoint.rxGetClient(discovery, svc -> svc.getName().equals(ENDPOINT_NAME), new JsonObject().put("keepAlive", false));
        Single<JDBCClient> svc3 = JDBCDataSource.rxGetJDBCClient(discovery, svc -> svc.getName().equals("my-database"), new JsonObject().put("url", "jdbc:postgresql://my-database:5432/my_data").put("driver_class", "org.postgresql.Driver").put("user", "luke").put("password", "secret"));
        Single.zip(svc1, svc2, svc3, (wc, hc, db) -> {
            this.web = wc;
            this.client = hc;
            this.database = db;
            return vertx.createHttpServer().requestHandler(router::accept).listen(8080);
        }).subscribe();
    });
}
Also used : WebClientOptions(io.vertx.ext.web.client.WebClientOptions) HttpResponse(io.vertx.rxjava.ext.web.client.HttpResponse) Router(io.vertx.rxjava.ext.web.Router) RoutingContext(io.vertx.rxjava.ext.web.RoutingContext) HttpEndpoint(io.vertx.rxjava.servicediscovery.types.HttpEndpoint) AbstractVerticle(io.vertx.rxjava.core.AbstractVerticle) JDBCClient(io.vertx.rxjava.ext.jdbc.JDBCClient) WebClient(io.vertx.rxjava.ext.web.client.WebClient) HttpClient(io.vertx.rxjava.core.http.HttpClient) Single(rx.Single) JDBCDataSource(io.vertx.rxjava.servicediscovery.types.JDBCDataSource) ServiceDiscovery(io.vertx.rxjava.servicediscovery.ServiceDiscovery) JsonObject(io.vertx.core.json.JsonObject) HttpClientOptions(io.vertx.core.http.HttpClientOptions) BodyCodec(io.vertx.rxjava.ext.web.codec.BodyCodec) WebClientOptions(io.vertx.ext.web.client.WebClientOptions) HttpClient(io.vertx.rxjava.core.http.HttpClient) JsonObject(io.vertx.core.json.JsonObject) Router(io.vertx.rxjava.ext.web.Router) JDBCClient(io.vertx.rxjava.ext.jdbc.JDBCClient) WebClient(io.vertx.rxjava.ext.web.client.WebClient) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Example 3 with WebClient

use of io.vertx.rxjava.ext.web.client.WebClient in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    WebClient client = WebClient.create(vertx);
    Single<HttpResponse<String>> request = client.get(8080, "localhost", "/").as(BodyCodec.string()).rxSend();
    // Fire the request
    request.subscribe(resp -> System.out.println("Server content " + resp.body()));
    // Again
    request.subscribe(resp -> System.out.println("Server content " + resp.body()));
    // And again
    request.subscribe(resp -> System.out.println("Server content " + resp.body()));
}
Also used : HttpResponse(io.vertx.rxjava.ext.web.client.HttpResponse) WebClient(io.vertx.rxjava.ext.web.client.WebClient)

Example 4 with WebClient

use of io.vertx.rxjava.ext.web.client.WebClient in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    // Create two requests
    WebClient client = WebClient.create(vertx);
    Single<JsonObject> request = client.get(8080, "localhost", "/").as(BodyCodec.jsonObject()).rxSend().map(resp -> resp.body());
    // Combine the responses with the zip into a single response
    request.zipWith(request, (b1, b2) -> new JsonObject().put("req1", b1).put("req2", b2)).subscribe(json -> {
        System.out.println("Got combined result " + json);
    }, err -> {
        err.printStackTrace();
    });
}
Also used : HttpClientRequest(io.vertx.rxjava.core.http.HttpClientRequest) HttpClient(io.vertx.rxjava.core.http.HttpClient) HttpResponse(io.vertx.rxjava.ext.web.client.HttpResponse) Single(rx.Single) HttpMethod(io.vertx.core.http.HttpMethod) JsonObject(io.vertx.core.json.JsonObject) Runner(io.vertx.example.util.Runner) AbstractVerticle(io.vertx.rxjava.core.AbstractVerticle) HttpClientResponse(io.vertx.rxjava.core.http.HttpClientResponse) WebClient(io.vertx.rxjava.ext.web.client.WebClient) BodyCodec(io.vertx.rxjava.ext.web.codec.BodyCodec) Observable(rx.Observable) JsonObject(io.vertx.core.json.JsonObject) WebClient(io.vertx.rxjava.ext.web.client.WebClient)

Aggregations

HttpResponse (io.vertx.rxjava.ext.web.client.HttpResponse)4 WebClient (io.vertx.rxjava.ext.web.client.WebClient)4 JsonObject (io.vertx.core.json.JsonObject)2 AbstractVerticle (io.vertx.rxjava.core.AbstractVerticle)2 HttpClient (io.vertx.rxjava.core.http.HttpClient)2 BodyCodec (io.vertx.rxjava.ext.web.codec.BodyCodec)2 Single (rx.Single)2 HttpClientOptions (io.vertx.core.http.HttpClientOptions)1 HttpMethod (io.vertx.core.http.HttpMethod)1 Runner (io.vertx.example.util.Runner)1 WebClientOptions (io.vertx.ext.web.client.WebClientOptions)1 HttpClientRequest (io.vertx.rxjava.core.http.HttpClientRequest)1 HttpClientResponse (io.vertx.rxjava.core.http.HttpClientResponse)1 JDBCClient (io.vertx.rxjava.ext.jdbc.JDBCClient)1 Router (io.vertx.rxjava.ext.web.Router)1 RoutingContext (io.vertx.rxjava.ext.web.RoutingContext)1 ServiceDiscovery (io.vertx.rxjava.servicediscovery.ServiceDiscovery)1 HttpEndpoint (io.vertx.rxjava.servicediscovery.types.HttpEndpoint)1 JDBCDataSource (io.vertx.rxjava.servicediscovery.types.JDBCDataSource)1 Observable (rx.Observable)1