@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)); } }
@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(); }); } }
@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())); } }
/** * @return the codec */ public static io.vertx.rxjava.ext.web.codec.BodyCodec<io.vertx.rxjava.core.buffer.Buffer> buffer() { io.vertx.rxjava.ext.web.codec.BodyCodec<io.vertx.rxjava.core.buffer.Buffer> ret = io.vertx.rxjava.ext.web.codec.BodyCodec.newInstance(io.vertx.ext.web.codec.BodyCodec.buffer(), io.vertx.rxjava.core.buffer.Buffer.__TYPE_ARG); return ret; }
public static <T>BodyCodec<T> newInstance(io.vertx.ext.web.codec.BodyCodec arg, io.vertx.lang.rx.TypeArg<T> __typeArg_T) { return arg != null ? new BodyCodec<T>(arg, __typeArg_T) : null; } }
/** * Configure the request to decode the response with the <code>responseCodec</code>. * @param responseCodec the response codec * @return a reference to this, so the API can be used fluently */ public <U> io.vertx.rxjava.ext.web.client.HttpRequest<U> as(io.vertx.rxjava.ext.web.codec.BodyCodec<U> responseCodec) { io.vertx.rxjava.ext.web.client.HttpRequest<U> ret = io.vertx.rxjava.ext.web.client.HttpRequest.newInstance(delegate.as(responseCodec.getDelegate()), responseCodec.__typeArg_0); return ret; }
@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(); } }
/** * @return the codec */ public static io.vertx.rxjava.ext.web.codec.BodyCodec<io.vertx.rxjava.core.buffer.Buffer> buffer() { io.vertx.rxjava.ext.web.codec.BodyCodec<io.vertx.rxjava.core.buffer.Buffer> ret = io.vertx.rxjava.ext.web.codec.BodyCodec.newInstance(io.vertx.ext.web.codec.BodyCodec.buffer(), io.vertx.rxjava.core.buffer.Buffer.__TYPE_ARG); return ret; }
public static <T>BodyCodec<T> newInstance(io.vertx.ext.web.codec.BodyCodec arg, io.vertx.lang.rx.TypeArg<T> __typeArg_T) { return arg != null ? new BodyCodec<T>(arg, __typeArg_T) : null; } }
/** * Configure the request to decode the response with the <code>responseCodec</code>. * @param responseCodec the response codec * @return a reference to this, so the API can be used fluently */ public <U> io.vertx.rxjava.ext.web.client.HttpRequest<U> as(io.vertx.rxjava.ext.web.codec.BodyCodec<U> responseCodec) { io.vertx.rxjava.ext.web.client.HttpRequest<U> ret = io.vertx.rxjava.ext.web.client.HttpRequest.newInstance(delegate.as(responseCodec.getDelegate()), responseCodec.__typeArg_0); return ret; }
/** * @return a codec that simply discards the response */ public static io.vertx.rxjava.ext.web.codec.BodyCodec<Void> none() { io.vertx.rxjava.ext.web.codec.BodyCodec<Void> ret = io.vertx.rxjava.ext.web.codec.BodyCodec.newInstance(io.vertx.ext.web.codec.BodyCodec.none(), io.vertx.lang.rx.TypeArg.unknown()); return ret; }
@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(); } } }
public static <T>BodyCodec<T> newInstance(io.vertx.ext.web.codec.BodyCodec arg) { return arg != null ? new BodyCodec<T>(arg) : null; }
/** * @return the codec */ public static io.vertx.rxjava.ext.web.codec.BodyCodec<JsonArray> jsonArray() { io.vertx.rxjava.ext.web.codec.BodyCodec<JsonArray> ret = io.vertx.rxjava.ext.web.codec.BodyCodec.newInstance(io.vertx.ext.web.codec.BodyCodec.jsonArray(), io.vertx.lang.rx.TypeArg.unknown()); return ret; }
@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(); } }
public static <T>BodyCodec<T> newInstance(io.vertx.ext.web.codec.BodyCodec arg) { return arg != null ? new BodyCodec<T>(arg) : null; }
/** * @return a codec that simply discards the response */ public static io.vertx.rxjava.ext.web.codec.BodyCodec<Void> none() { io.vertx.rxjava.ext.web.codec.BodyCodec<Void> ret = io.vertx.rxjava.ext.web.codec.BodyCodec.newInstance(io.vertx.ext.web.codec.BodyCodec.none(), io.vertx.lang.rx.TypeArg.unknown()); return ret; }
/** * A codec for strings using a specific <code>encoding</code>. * @param encoding the encoding * @return the codec */ public static io.vertx.rxjava.ext.web.codec.BodyCodec<String> string(String encoding) { io.vertx.rxjava.ext.web.codec.BodyCodec<String> ret = io.vertx.rxjava.ext.web.codec.BodyCodec.newInstance(io.vertx.ext.web.codec.BodyCodec.string(encoding), io.vertx.lang.rx.TypeArg.unknown()); return ret; }
/** * @return the codec */ public static io.vertx.rxjava.ext.web.codec.BodyCodec<JsonArray> jsonArray() { io.vertx.rxjava.ext.web.codec.BodyCodec<JsonArray> ret = io.vertx.rxjava.ext.web.codec.BodyCodec.newInstance(io.vertx.ext.web.codec.BodyCodec.jsonArray(), io.vertx.lang.rx.TypeArg.unknown()); return ret; }
/** * @return the UTF-8 string codec */ public static io.vertx.rxjava.ext.web.codec.BodyCodec<String> string() { io.vertx.rxjava.ext.web.codec.BodyCodec<String> ret = io.vertx.rxjava.ext.web.codec.BodyCodec.newInstance(io.vertx.ext.web.codec.BodyCodec.string(), io.vertx.lang.rx.TypeArg.unknown()); return ret; }