@Override public void start() throws Exception { WebClient client = WebClient.create(vertx); client.get(8080, "localhost", "/") .as(BodyCodec.json(User.class)) .send(ar -> { if (ar.succeeded()) { HttpResponse<User> response = ar.result(); System.out.println("Got HTTP response body"); User user = response.body(); System.out.println("FirstName " + user.firstName); System.out.println("LastName " + user.lastName); System.out.println("Male " + user.male); } else { ar.cause().printStackTrace(); } }); } }
@Override public void start() throws Exception { WebClient client = WebClient.create(vertx); client.get(8080, "localhost", "/") .as(BodyCodec.jsonObject()) .send(ar -> { if (ar.succeeded()) { HttpResponse<JsonObject> response = ar.result(); System.out.println("Got HTTP response body"); System.out.println(response.body().encodePrettily()); } else { ar.cause().printStackTrace(); } }); } }
private void invoke(RoutingContext rc) { client.get("/luke").as(BodyCodec.string()).send(ar -> { if (ar.failed()) { rc.response().end("Unable to call the greeting service: " + ar.cause().getMessage()); } else { if (ar.result().statusCode() != 200) { rc.response().end("Unable to call the greeting service - received status:" + ar.result().statusMessage()); } else { rc.response().end("Greeting service invoked: '" + ar.result().body() + "'"); } } }); } }
/** * 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; }
/** * 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; }
private void getNow(RoutingContext ctx) { count++; final WebClient client = WebClient.create(vertx); client.get(80, HTTP_NOW, "/") .timeout(5000) .as(BodyCodec.jsonObject()) .send(ar -> { if (ar.succeeded()) { HttpResponse<JsonObject> response = ar.result(); JsonObject body = response.body(); String now = body.getJsonObject("now").getString("rfc2822"); ctx.response().end(now + " " + String.format(RESPONSE_STRING_FORMAT, HOSTNAME, count)); } else { ctx.response().setStatusCode(503).end(ar.cause().getMessage()); } }); }
protected JsonObject getResource(String type, String basePath, String name, int expectedCode) throws Exception { String path = basePath + "/" + name; log.info("GET: path {}", path); CompletableFuture<JsonObject> responsePromise = new CompletableFuture<>(); return doRequestNTimes(initRetry, () -> { client.get(endpoint.getPort(), endpoint.getHost(), path) .as(BodyCodec.jsonObject()) .putHeader(HttpHeaders.AUTHORIZATION, authzString) .send(ar -> responseHandler(ar, responsePromise, expectedCode, String.format("Error: get %s %s", type, name))); return responsePromise.get(30, TimeUnit.SECONDS); }, Optional.of(endpointSupplier), Optional.empty()); }
/** * Get all info about messaging client (stdOut, stdErr, code, isRunning) * * @param uuid client id * @return * @throws InterruptedException * @throws ExecutionException * @throws TimeoutException */ public JsonObject getClientInfo(String uuid) throws InterruptedException, ExecutionException, TimeoutException { CompletableFuture<JsonObject> responsePromise = new CompletableFuture<>(); JsonObject request = new JsonObject(); request.put("id", uuid); client.get(endpoint.getPort(), endpoint.getHost(), "") .as(BodyCodec.jsonObject()) .timeout(120000) .sendJson(request, ar -> responseHandler(ar, responsePromise, HttpURLConnection.HTTP_OK, "Error getting messaging clients info")); return responsePromise.get(150000, TimeUnit.SECONDS); }
/** * Stop messaging client and get all informations about them (stdOut, stdErr, code, isRunning) * * @param uuid client id * @return * @throws InterruptedException * @throws ExecutionException * @throws TimeoutException */ public JsonObject stopClient(String uuid) throws InterruptedException, ExecutionException, TimeoutException { CompletableFuture<JsonObject> responsePromise = new CompletableFuture<>(); JsonObject request = new JsonObject(); request.put("id", uuid); client.delete(endpoint.getPort(), endpoint.getHost(), "") .as(BodyCodec.jsonObject()) .timeout(120000) .sendJson(request, ar -> responseHandler(ar, responsePromise, HttpURLConnection.HTTP_OK, "Error removing messaging clients")); return responsePromise.get(150000, TimeUnit.SECONDS); }
@Test public void testResponseBodyAsAsJsonObject() throws Exception { JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu"); server.requestHandler(req -> req.response().end(expected.encode())); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(BodyCodec.jsonObject()) .send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(expected, resp.body()); testComplete(); })); await(); }
@Test @DisplayName("Start a HTTP server, then issue a HTTP client request and check the response") void vertx_check_http_server_response() throws InterruptedException { Vertx vertx = Vertx.vertx(); VertxTestContext testContext = new VertxTestContext(); vertx.deployVerticle(new HttpServerVerticle(), testContext.succeeding(id -> { WebClient client = WebClient.create(vertx); client.get(8080, "localhost", "/") .as(BodyCodec.string()) .send(testContext.succeeding(response -> testContext.verify(() -> { assertThat(response.body()).isEqualTo("Plop"); testContext.completeNow(); }))); })); assertThat(testContext.awaitCompletion(5, TimeUnit.SECONDS)).isTrue(); closeVertx(vertx); }
@Test public void testResponseBodyUnmarshallingError() throws Exception { server.requestHandler(req -> req.response().end("not-json-object")); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(BodyCodec.jsonObject()) .send(onFailure(err -> { assertTrue(err instanceof DecodeException); testComplete(); })); await(); }
private void handleMutateCodec(HttpContext context) { if (context.phase() == ClientPhase.RECEIVE_RESPONSE) { if (context.clientResponse().statusCode() == 200) { context.request().as(BodyCodec.none()); } } context.next(); }
@Test public void testResponseBodyAsAsJsonMapped() throws Exception { JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu"); server.requestHandler(req -> req.response().end(expected.encode())); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(BodyCodec.json(WineAndCheese.class)) .send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(new WineAndCheese().setCheese("Goat Cheese").setWine("Condrieu"), resp.body()); testComplete(); })); await(); }
private <R> void testResponseMissingBody(BodyCodec<R> codec) throws Exception { server.requestHandler(req -> req.response().setStatusCode(403).end()); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(codec) .send(onSuccess(resp -> { assertEquals(403, resp.statusCode()); assertNull(resp.body()); testComplete(); })); await(); }
@Test public void testHttpResponseError() throws Exception { server.requestHandler(req -> req.response().setChunked(true).write(Buffer.buffer("some-data")).close()); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(BodyCodec.jsonObject()) .send(onFailure(err -> { assertTrue(err instanceof VertxException); testComplete(); })); await(); }
@Test public void testResponseBodyDiscarded() throws Exception { server.requestHandler(req -> req.response().end(TestUtils.randomAlphaString(1024))); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(BodyCodec.none()) .send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(null, resp.body()); testComplete(); })); await(); }
@Test public void testResponseBodyAsAsJsonArray() throws Exception { JsonArray expected = new JsonArray().add("cheese").add("wine"); server.requestHandler(req -> req.response().end(expected.encode())); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(BodyCodec.jsonArray()) .send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(expected, resp.body()); testComplete(); })); await(); }
@Test public void testResponseBodyAsAsJsonArrayMapped() throws Exception { JsonArray expected = new JsonArray().add("cheese").add("wine"); server.requestHandler(req -> req.response().end(expected.encode())); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(BodyCodec.json(List.class)) .send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(expected.getList(), resp.body()); testComplete(); })); await(); }