@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)); } }
public void close() { if (webClient!=null) { webClient.close(); } } }
private Maybe<User> profile(String accessToken) { return client.getAbs(configuration.getUserProfileUri()) .putHeader(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken) .rxSend() .toMaybe() .map(httpClientResponse -> { if (httpClientResponse.statusCode() != 200) { throw new BadCredentialsException(httpClientResponse.statusMessage()); } return createUser(httpClientResponse.bodyAsJsonObject()); }); }
private void testGetRequest(VertxTestContext context, Vertx vertx, String url, String expectedResult) { WebClient client = WebClient.create(vertx); Single<HttpResponse<Buffer>> httpResponseSingle = client .get(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, url).rxSend(); subscribeToResult_shouldSucceed(context, httpResponseSingle, resp -> { assertEquals(expectedResult, resp.body().toString(), "Wrong engines processed request, expected " + expectedResult); assertEquals(HttpResponseStatus.OK.code(), resp.statusCode()); assertNotNull(resp.getHeader(EXPECTED_RESPONSE_HEADER)); assertEquals(EXPECTED_XSERVER_HEADER_VALUE, resp.getHeader(EXPECTED_RESPONSE_HEADER)); }); }
private Single<String> get(Vertx vertx, URI uri){ WebClient client = WebClient.create(vertx); Single<HttpResponse<Buffer>> responseHandler = client.get(uri.getPort(), uri.getHost(), uri.getPath()).rxSend(); return responseHandler.map(response -> response.body().toString()) .doAfterTerminate(() -> client.close()); }
static JsonObject mapJsonObjectOrError(final HttpResponse<JsonObject> response) { if (response.statusCode() < 200 || response.statusCode() > 299) { throw new RuntimeException(response.statusMessage()); } return response.body(); }
@Override public Completable putTemplate(String templateName, String template) { return httpClient .put(URL_TEMPLATE + '/' + templateName) .putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON) .rxSendBuffer(Buffer.buffer(template)) .flatMapCompletable(response -> { if (response.statusCode() != HttpStatusCode.OK_200) { logger.error("Unable to put template mapping: status[{}] template[{}] response[{}]", response.statusCode(), template, response.body()); return Completable.error(new ElasticsearchException("Unable to put template mapping")); } return Completable.complete(); }); }
private void testPostRequest(VertxTestContext context, Vertx vertx, String url, Consumer<HttpResponse<Buffer>> expectedResponse) { WebClient client = WebClient.create(vertx); MultiMap formData = MultiMap.caseInsensitiveMultiMap(); formData.add("a", "b"); Single<HttpResponse<Buffer>> httpResponseSingle = client .post(KnotxServerRoutingTest.KNOTX_SERVER_PORT, KnotxServerRoutingTest.KNOTX_SERVER_ADDRESS, url) .rxSendForm(formData); subscribeToResult_shouldSucceed(context, httpResponseSingle, expectedResponse); }
/** * Get the cluster health * * @return the cluster health * @throws ElasticsearchException error occurs during ES call */ @Override public Single<Health> getClusterHealth() { return httpClient .get(URL_STATE_CLUSTER) .rxSend() .map(response -> mapper.readValue(response.bodyAsString(), Health.class)); }
public HttpActionAdapterProxyImpl(Vertx vertx, ServiceAdapterOptions configuration) { this.httpClientFacade = new HttpClientFacade( WebClient.create(vertx, configuration.getClientOptions()), configuration); }
private void logResponse(Pair<ClientRequest, HttpDataSourceSettings> request, HttpResponse<Buffer> resp) { if (resp.statusCode() >= 400 && resp.statusCode() < 600) { LOGGER.error("{} {} -> Got response {}, headers[{}]", logResponseData(request, resp)); } else if (LOGGER.isDebugEnabled()) { LOGGER.debug("{} {} -> Got response {}, headers[{}]", logResponseData(request, resp)); } }
@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())); } }
private Maybe<User> profile(String accessToken) { return client.getAbs(configuration.getUserProfileUri()) .putHeader(HttpHeaders.AUTHORIZATION, "token " + accessToken) .rxSend() .toMaybe() .map(httpClientResponse -> { if (httpClientResponse.statusCode() != 200) { throw new BadCredentialsException(httpClientResponse.statusMessage()); } return createUser(httpClientResponse.bodyAsJsonObject()); }); }
static JsonArray mapJsonArrayOrError(final HttpResponse<JsonArray> response) { if (response.statusCode() < 200 || response.statusCode() > 299) { throw new RuntimeException(response.statusMessage()); } return response.body(); } }
public FormsAdapterProxyImpl(Vertx vertx, HttpFormsAdapterOptions configuration) { this.httpClientFacade = new HttpClientFacade( WebClient.create(vertx, configuration.getClientOptions()), configuration); }
@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(); }); } }
private Maybe<User> profile(String accessToken) { return client.getAbs(configuration.getUserProfileUri()) .putHeader(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken) .rxSend() .toMaybe() .map(httpClientResponse -> { if (httpClientResponse.statusCode() != 200) { throw new BadCredentialsException(httpClientResponse.statusMessage()); } return createUser(httpClientResponse.bodyAsJsonObject()); }); }
private Maybe<User> profile(String accessToken) { return client.getAbs(configuration.getUserProfileUri()) .putHeader(HttpHeaders.AUTHORIZATION, "token " + accessToken) .rxSend() .toMaybe() .map(httpClientResponse -> { if (httpClientResponse.statusCode() != 200) { throw new BadCredentialsException(httpClientResponse.statusMessage()); } return createUser(httpClientResponse.bodyAsJsonObject()); }); }