@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(); }); } }
public Observable<NetworkType> getNetworkType() { return this.client .getAbs(this.url.toString()) .as(BodyCodec.jsonObject()) .rxSend() .toObservable() .map(Http::mapJsonObjectOrError) .map(json -> json.getString("name")) .map(name -> { if (name.equalsIgnoreCase("mijinTest")) return NetworkType.MIJIN_TEST; else { throw new IllegalArgumentException("network " + name + " is not supported yet by the sdk"); } }); } }
@Override public Observable<TransactionAnnounceResponse> announce(SignedTransaction signedTransaction) { JsonObject requestBody = new JsonObject(); requestBody.put("payload", signedTransaction.getPayload()); return this.client .putAbs(this.url.toString()) .as(BodyCodec.jsonObject()) .rxSendJson(requestBody) .toObservable() .map(Http::mapJsonObjectOrError) .map(json -> new TransactionAnnounceResponse(new JsonObject(json.toString()).getString("message"))); }
@Override public Observable<TransactionAnnounceResponse> announceAggregateBonded(SignedTransaction signedTransaction) { JsonObject requestBody = new JsonObject(); requestBody.put("payload", signedTransaction.getPayload()); return this.client .putAbs(this.url + "/partial") .as(BodyCodec.jsonObject()) .rxSendJson(requestBody) .toObservable() .map(Http::mapJsonObjectOrError) .map(json -> new TransactionAnnounceResponse(new JsonObject(json.toString()).getString("message"))); }
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()); }
@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()); }
@Override public Observable<TransactionAnnounceResponse> announceAggregateBondedCosignature(CosignatureSignedTransaction cosignatureSignedTransaction) { JsonObject requestBody = new JsonObject(); requestBody.put("parentHash", cosignatureSignedTransaction.getParentHash()); requestBody.put("signature", cosignatureSignedTransaction.getSignature()); requestBody.put("signer", cosignatureSignedTransaction.getSigner()); return this.client .putAbs(this.url + "/cosignature") .as(BodyCodec.jsonObject()) .rxSendJson(requestBody) .toObservable() .map(Http::mapJsonObjectOrError) .map(json -> new TransactionAnnounceResponse(new JsonObject(json.toString()).getString("message"))); } }
@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()); }
@Override public Observable<List<Transaction>> getTransactions(List<String> transactionHashes) { JsonObject requestBody = new JsonObject(); requestBody.put("transactionIds", transactionHashes); return this.client .postAbs(this.url.toString()) .as(BodyCodec.jsonArray()) .rxSendJson(requestBody) .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 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(); }
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(); }
@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<List<MosaicName>> getMosaicNames(List<MosaicId> mosaicIds) { JsonObject requestBody = new JsonObject(); requestBody.put("mosaicIds", mosaicIds.stream().map(id -> UInt64.bigIntegerToHex(id.getId())).collect(Collectors.toList())); return this.client .postAbs(this.url + "/mosaic/names") .as(BodyCodec.jsonArray()) .rxSendJson(requestBody) .toObservable() .map(Http::mapJsonArrayOrError) .map(json -> objectMapper.<List<MosaicNameDTO>>readValue(json.toString(), new TypeReference<List<MosaicNameDTO>>() { })) .flatMapIterable(item -> item) .map(mosaicNameDTO -> new MosaicName(new MosaicId(mosaicNameDTO.getMosaicId().extractIntArray()), mosaicNameDTO.getName(), new NamespaceId(mosaicNameDTO.getParentId().extractIntArray()))) .toList() .toObservable(); }
@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())); }
@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(); } } }
@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(); } }