@Override public void start() throws Exception { WebClient client = WebClient.create(vertx); client.get(80, "perdu.com", "/") .rxSend() .map(HttpResponse::bodyAsString) .subscribe( s -> LOGGER.info("From web client: " + s), Throwable::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())); } }
@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(); }); } }
/** * 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)); }
@Override public Observable<Transaction> getTransaction(String transactionHash) { return this.client .getAbs(this.url + transactionHash) .as(BodyCodec.jsonObject()) .rxSend() .toObservable() .map(Http::mapJsonObjectOrError) .map(json -> new JsonObject(json.toString())) .map(new TransactionMapping()); }
public Observable<BigInteger> getBlockchainScore() { return this.client .getAbs(this.url + "/chain/score") .as(BodyCodec.jsonObject()) .rxSend() .toObservable() .map(Http::mapJsonObjectOrError) .map(json -> objectMapper.readValue(json.toString(), BlockchainScoreDTO.class)) .map(blockchainScoreDTO -> blockchainScoreDTO.extractIntArray()); }
private Observable<List<Transaction>> getBlockTransactions(BigInteger height, Optional<QueryParams> queryParams) { return this.client .getAbs(this.url + "/block/" + height + "/transactions" + (queryParams.isPresent() ? queryParams.get().toUrl() : "")) .as(BodyCodec.jsonArray()) .rxSend() .toObservable() .map(Http::mapJsonArrayOrError) .map(json -> new JsonArray(json.toString()).stream().map(s -> (JsonObject) s).collect(Collectors.toList())) .flatMapIterable(item -> item) .map(new TransactionMapping()) .toList() .toObservable(); }
@Override public Observable<BigInteger> getBlockchainHeight() { return this.client .getAbs(this.url + "/chain/height") .as(BodyCodec.jsonObject()) .rxSend() .toObservable() .map(Http::mapJsonObjectOrError) .map(json -> objectMapper.readValue(json.toString(), HeightDTO.class)) .map(blockchainHeight -> blockchainHeight.getHeight().extractIntArray()); }
private Observable<List<Transaction>> findTransactions(PublicAccount publicAccount, Optional<QueryParams> queryParams, String path) { return this.client .getAbs(this.url + publicAccount.getPublicKey() + path + (queryParams.isPresent() ? queryParams.get().toUrl() : "")) .as(BodyCodec.jsonArray()) .rxSend() .toObservable() .map(Http::mapJsonArrayOrError) .map(json -> new JsonArray(json.toString()).stream().map(s -> (JsonObject) s).collect(Collectors.toList())) .flatMapIterable(item -> item) .map(new TransactionMapping()) .toList() .toObservable(); }
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()); }
@Override public Observable<MultisigAccountInfo> getMultisigAccountInfo(Address address) { Observable<NetworkType> networkTypeResolve = getNetworkTypeObservable(); return networkTypeResolve .flatMap(networkType -> this.client .getAbs(this.url + address.plain() + "/multisig") .as(BodyCodec.jsonObject()) .rxSend() .toObservable() .map(Http::mapJsonObjectOrError) .map(json -> objectMapper.readValue(json.toString(), MultisigAccountInfoDTO.class)) .map(MultisigAccountInfoDTO::getMultisig) .map(transfromMultisigAccountInfoDTO(networkType))); }
@Override public Observable<BlockchainStorageInfo> getBlockchainStorage() { return this.client .getAbs(this.url + "/diagnostic/storage") .as(BodyCodec.jsonObject()) .rxSend() .toObservable() .map(Http::mapJsonObjectOrError) .map(json -> objectMapper.readValue(json.toString(), BlockchainStorageInfoDTO.class)) .map(blockchainStorageInfoDTO -> new BlockchainStorageInfo(blockchainStorageInfoDTO.getNumAccounts(), blockchainStorageInfoDTO.getNumBlocks(), blockchainStorageInfoDTO.getNumBlocks())); } }
private Single<Buffer> sendGetRequest(String mbAlbumId) { return webClient .getAbs("http://coverartarchive.org") .uri("/release/" + mbAlbumId + "/front") .as(BodyCodec.buffer()) .rxSend() .map(HttpResponse::body); }
@Override public Observable<TransactionStatus> getTransactionStatus(String transactionHash) { return this.client .getAbs(this.url + transactionHash + "/status") .as(BodyCodec.jsonObject()) .rxSend() .toObservable() .map(Http::mapJsonObjectOrError) .map(json -> objectMapper.readValue(json.toString(), TransactionStatusDTO.class)) .map(transactionStatusDTO -> new TransactionStatus(transactionStatusDTO.getGroup(), transactionStatusDTO.getStatus(), transactionStatusDTO.getHash(), new Deadline(transactionStatusDTO.getDeadline().extractIntArray()), transactionStatusDTO.getHeight().extractIntArray())); }
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()); }); }
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()); }); }
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)); }); }