@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(); } }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(); HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/"); req.toObservable(). flatMap(HttpClientResponse::toObservable). // Unmarshall the response to the Data object via Jackon lift(io.vertx.rxjava.core.RxHelper.unmarshaller(Data.class)). subscribe( data -> { System.out.println("Got response " + data.message); }); // End request req.end(); } }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(); HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/"); req.toObservable(). // Status code check and -> Observable<Buffer> 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(); } }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(); client.put(8080, "localhost", "/", resp -> { System.out.println("Got response " + resp.statusCode()); resp.handler(buf -> System.out.println(buf.toString("UTF-8"))); }).setChunked(true).putHeader("Content-Type", "text/plain").write("hello").end(); } }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(); HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/"); req.toObservable(). // Status code check and -> Observable<Buffer> flatMap(resp -> { if (resp.statusCode() != 200) { throw new RuntimeException("Wrong status code " + resp.statusCode()); } return Observable.just(Buffer.buffer()).mergeWith(resp.toObservable()); }). // Reduce all buffers in a single buffer reduce(Buffer::appendBuffer). // Turn in to a string map(buffer -> buffer.toString("UTF-8")). // Get a single buffer subscribe(data -> System.out.println("Server content " + data)); // End request req.end(); } }
@Override public void start() throws Exception { JsonObject config = config(); this.http = vertx.createHttpClient(new HttpClientOptions(config)); if(config.containsKey("dbnames")){ config.getJsonArray("dbnames").forEach(dbname -> registerConsumer((String) dbname)); LOG.info("InfluxDB timeseries verticle started"); } else { LOG.warn("No InfluxDB name configured"); } }
@Test public void testHttpClientConnectionFailure() { HttpClient client = vertx.createHttpClient(new HttpClientOptions()); Single<HttpClientResponse> req = client.rxGetNow(9998, "255.255.255.255", "/the_uri"); req.subscribe( resp -> fail(), err -> testComplete()); await(); }
@Test public void testHttpClientConnectionFailureFlatMap() { HttpClient client = vertx.createHttpClient(new HttpClientOptions()); Single<HttpClientResponse> req = client.rxGetNow(9998, "255.255.255.255", "/the_uri"); req.flatMapObservable(HttpClientResponse::toObservable).forEach( buffer -> fail(), err -> testComplete(), this::fail); await(); }
@Test public void testErrorHandling() throws Exception { try { client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions())); Single<HttpResponse<WineAndCheese>> single = client .get(-1, "localhost", "/the_uri") .as(BodyCodec.json(WineAndCheese.class)) .rxSend(); single.subscribe(resp -> fail(), error -> { assertEquals(IllegalArgumentException.class, error.getClass()); testComplete(); }); await(); } catch (Throwable t) { fail(); } } }
onListen.subscribe( s -> { HttpClientRequest req = vertx.createHttpClient(new HttpClientOptions()).request(HttpMethod.PUT, 8080, "localhost", "/some/path", resp -> { }); req.putHeader("Content-Length", "3");
ObservableFuture<HttpServer> onListen = RxHelper.observableFuture(); onListen.subscribe( server -> vertx.createHttpClient(new HttpClientOptions()).websocket(8080, "localhost", "/some/path", ws -> { ws.write(Buffer.buffer("foo")); ws.close();
@Test public void testHttpClientFlatMapUnmarshallPojo() { HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> { req.response().setChunked(true).end("{\"foo\":\"bar\"}"); }); server.listen(ar -> { HttpClient client = vertx.createHttpClient(new HttpClientOptions()); Single<HttpClientResponse> req = client.rxGetNow(8080, "localhost", "/the_uri"); ArrayList<SimplePojo> objects = new ArrayList<>(); req.flatMapObservable(HttpClientResponse::toObservable). lift(io.vertx.rxjava.core.RxHelper.unmarshaller(SimplePojo.class)). forEach( objects::add, err -> fail(), () -> { server.close(); assertEquals(Arrays.asList(new SimplePojo("bar")), objects); testComplete(); }); }); await(); }
@Test public void testResponseBodyAsAsJsonMapped() throws Exception { JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu"); HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> req.response().end(expected.encode())); try { server.listen(ar -> { client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions())); Single<HttpResponse<WineAndCheese>> single = client .get(8080, "localhost", "/the_uri") .as(BodyCodec.json(WineAndCheese.class)) .rxSend(); single.subscribe(resp -> { assertEquals(200, resp.statusCode()); assertEquals(new WineAndCheese().setCheese("Goat Cheese").setWine("Condrieu"), resp.body()); testComplete(); }, this::fail); }); await(); } finally { server.close(); } }
@Test public void testWebsocketClientFlatMap() { HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.websocketStream().handler(ws -> { ws.write(Buffer.buffer("some_content")); ws.close(); }); server.listen(ar -> { HttpClient client = vertx.createHttpClient(new HttpClientOptions()); Buffer content = Buffer.buffer(); client. websocketStream(8080, "localhost", "/the_uri"). toObservable(). flatMap(WebSocket::toObservable). forEach(content::appendBuffer, err -> fail(), () -> { server.close(); assertEquals("some_content", content.toString("UTF-8")); testComplete(); }); }); await(); }
@Test public void testPost() { int times = 5; waitFor(times); HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> req.bodyHandler(buff -> { assertEquals("onetwothree", buff.toString()); req.response().end(); })); try { server.listen(ar -> { client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions())); Observable<Buffer> stream = Observable.just(Buffer.buffer("one"), Buffer.buffer("two"), Buffer.buffer("three")); Single<HttpResponse<Buffer>> single = client .post(8080, "localhost", "/the_uri") .rxSendStream(stream); for (int i = 0; i < times; i++) { single.subscribe(resp -> complete(), this::fail); } }); await(); } finally { server.close(); } }
@Test public void testWebsocketClient() { HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.websocketStream().handler(ws -> { ws.write(Buffer.buffer("some_content")); ws.close(); }); server.listen(ar -> { HttpClient client = vertx.createHttpClient(new HttpClientOptions()); client.websocket(8080, "localhost", "/the_uri", ws -> { Buffer content = Buffer.buffer(); Observable<Buffer> observable = ws.toObservable(); observable.forEach(content::appendBuffer, err -> fail(), () -> { server.close(); assertEquals("some_content", content.toString("UTF-8")); testComplete(); }); }); }); await(); }
@Test public void testHttpClientFlatMap() { HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> { req.response().setChunked(true).end("some_content"); }); server.listen(ar -> { HttpClient client = vertx.createHttpClient(new HttpClientOptions()); Single<HttpClientResponse> req = client.rxGetNow(8080, "localhost", "/the_uri"); Buffer content = Buffer.buffer(); req.flatMapObservable(HttpClientResponse::toObservable).forEach( content::appendBuffer, err -> fail(), () -> { server.close(); assertEquals("some_content", content.toString("UTF-8")); testComplete(); }); }); await(); }
@Test public void testHttpClient() { HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> { req.response().setChunked(true).end("some_content"); }); try { server.listen(ar -> { HttpClient client = vertx.createHttpClient(new HttpClientOptions()); client.rxGetNow(8080, "localhost", "/the_uri").subscribe(resp -> { Buffer content = Buffer.buffer(); Observable<Buffer> observable = resp.toObservable(); observable.forEach(content::appendBuffer, err -> fail(), () -> { assertEquals("some_content", content.toString("UTF-8")); testComplete(); }); }); }); await(); } finally { server.close(); } }
@Test public void testResponseMissingBody() throws Exception { int times = 5; waitFor(times); HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> req.response().setStatusCode(403).end()); try { server.listen(ar -> { client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions())); Single<HttpResponse<Buffer>> single = client .get(8080, "localhost", "/the_uri") .rxSend(); for (int i = 0; i < times; i++) { single.subscribe(resp -> { assertEquals(403, resp.statusCode()); assertNull(resp.body()); complete(); }, this::fail); } }); await(); } finally { server.close(); } }
@Test public void testGet() { int times = 5; waitFor(times); HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> req.response().setChunked(true).end("some_content")); try { server.listen(ar -> { client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions())); Single<HttpResponse<Buffer>> single = client .get(8080, "localhost", "/the_uri") .as(BodyCodec.buffer()) .rxSend(); for (int i = 0; i < times; i++) { single.subscribe(resp -> { Buffer body = resp.body(); assertEquals("some_content", body.toString("UTF-8")); complete(); }, this::fail); } }); await(); } finally { server.close(); } }