Search in sources :

Example 1 with HttpClientResponse

use of io.vertx.rxjava.core.http.HttpClientResponse in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    HttpClient client = vertx.createHttpClient();
    HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/");
    req.toObservable().flatMap(HttpClientResponse::toObservable).lift(io.vertx.rxjava.core.RxHelper.unmarshaller(Data.class)).subscribe(data -> {
        System.out.println("Got response " + data.message);
    });
    // End request
    req.end();
}
Also used : HttpClientRequest(io.vertx.rxjava.core.http.HttpClientRequest) HttpClient(io.vertx.rxjava.core.http.HttpClient) HttpClientResponse(io.vertx.rxjava.core.http.HttpClientResponse)

Example 2 with HttpClientResponse

use of io.vertx.rxjava.core.http.HttpClientResponse in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    HttpClient client = vertx.createHttpClient();
    // Create two requests
    HttpClientRequest req1 = client.request(HttpMethod.GET, 8080, "localhost", "/");
    HttpClientRequest req2 = client.request(HttpMethod.GET, 8080, "localhost", "/");
    // Turn the requests responses into Observable<JsonObject>
    Observable<JsonObject> obs1 = req1.toObservable().flatMap(HttpClientResponse::toObservable).map(buf -> new JsonObject(buf.toString("UTF-8")));
    Observable<JsonObject> obs2 = req2.toObservable().flatMap(HttpClientResponse::toObservable).map(buf -> new JsonObject(buf.toString("UTF-8")));
    // Combine the responses with the zip into a single response
    obs1.zipWith(obs2, (b1, b2) -> new JsonObject().put("req1", b1).put("req2", b2)).subscribe(json -> {
        System.out.println("Got combined result " + json);
    }, err -> {
        err.printStackTrace();
    });
    req1.end();
    req2.end();
}
Also used : HttpClientRequest(io.vertx.rxjava.core.http.HttpClientRequest) HttpClient(io.vertx.rxjava.core.http.HttpClient) 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) Observable(rx.Observable) HttpClientRequest(io.vertx.rxjava.core.http.HttpClientRequest) HttpClient(io.vertx.rxjava.core.http.HttpClient) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

HttpClient (io.vertx.rxjava.core.http.HttpClient)2 HttpClientRequest (io.vertx.rxjava.core.http.HttpClientRequest)2 HttpClientResponse (io.vertx.rxjava.core.http.HttpClientResponse)2 HttpMethod (io.vertx.core.http.HttpMethod)1 JsonObject (io.vertx.core.json.JsonObject)1 Runner (io.vertx.example.util.Runner)1 AbstractVerticle (io.vertx.rxjava.core.AbstractVerticle)1 Observable (rx.Observable)1