Search in sources :

Example 1 with HttpClient

use of io.vertx.rxjava.core.http.HttpClient 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(resp -> {
        if (resp.statusCode() != 200) {
            throw new RuntimeException("Wrong status code " + resp.statusCode());
        }
        return Observable.just(Buffer.buffer()).mergeWith(resp.toObservable());
    }).reduce(Buffer::appendBuffer).map(buffer -> buffer.toString("UTF-8")).subscribe(data -> System.out.println("Server content " + data));
    // End request
    req.end();
}
Also used : Buffer(io.vertx.rxjava.core.buffer.Buffer) HttpClientRequest(io.vertx.rxjava.core.http.HttpClientRequest) HttpClient(io.vertx.rxjava.core.http.HttpClient) HttpMethod(io.vertx.core.http.HttpMethod) Runner(io.vertx.example.util.Runner) AbstractVerticle(io.vertx.rxjava.core.AbstractVerticle) Observable(rx.Observable) Buffer(io.vertx.rxjava.core.buffer.Buffer) HttpClientRequest(io.vertx.rxjava.core.http.HttpClientRequest) HttpClient(io.vertx.rxjava.core.http.HttpClient)

Example 2 with HttpClient

use of io.vertx.rxjava.core.http.HttpClient 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 3 with HttpClient

use of io.vertx.rxjava.core.http.HttpClient 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 4 with HttpClient

use of io.vertx.rxjava.core.http.HttpClient 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(resp -> {
        if (resp.statusCode() != 200) {
            throw new RuntimeException("Wrong status code " + resp.statusCode());
        }
        return resp.toObservable();
    }).subscribe(data -> System.out.println("Server content " + data.toString("UTF-8")));
    // End request
    req.end();
}
Also used : HttpClientRequest(io.vertx.rxjava.core.http.HttpClientRequest) HttpClient(io.vertx.rxjava.core.http.HttpClient) HttpMethod(io.vertx.core.http.HttpMethod) Runner(io.vertx.example.util.Runner) AbstractVerticle(io.vertx.rxjava.core.AbstractVerticle) HttpClientRequest(io.vertx.rxjava.core.http.HttpClientRequest) HttpClient(io.vertx.rxjava.core.http.HttpClient)

Example 5 with HttpClient

use of io.vertx.rxjava.core.http.HttpClient 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)8 HttpClientRequest (io.vertx.rxjava.core.http.HttpClientRequest)5 AbstractVerticle (io.vertx.rxjava.core.AbstractVerticle)4 HttpClientOptions (io.vertx.core.http.HttpClientOptions)3 HttpMethod (io.vertx.core.http.HttpMethod)3 Runner (io.vertx.example.util.Runner)3 JsonObject (io.vertx.core.json.JsonObject)2 HttpClientResponse (io.vertx.rxjava.core.http.HttpClientResponse)2 Observable (rx.Observable)2 HttpException (io.georocket.util.HttpException)1 AsyncResult (io.vertx.core.AsyncResult)1 WebClientOptions (io.vertx.ext.web.client.WebClientOptions)1 Buffer (io.vertx.rxjava.core.buffer.Buffer)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 HttpResponse (io.vertx.rxjava.ext.web.client.HttpResponse)1 WebClient (io.vertx.rxjava.ext.web.client.WebClient)1 BodyCodec (io.vertx.rxjava.ext.web.codec.BodyCodec)1 ServiceDiscovery (io.vertx.rxjava.servicediscovery.ServiceDiscovery)1