Skip to content

Commit

Permalink
getBalances(List<Address>) switch to POST request from GET (#83)
Browse files Browse the repository at this point in the history
* update waves-transactions to 1.0.6
  • Loading branch information
msmolyakov authored Apr 12, 2021
1 parent a668ffa commit 3e4b083
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 26 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ Use the codes below to add WavesJ as a dependency for your project.
<dependency>
<groupId>com.wavesplatform</groupId>
<artifactId>wavesj</artifactId>
<version>1.0.4</version>
<version>1.1.0</version>
</dependency>
```

##### Gradle:
```
compile group: 'com.wavesplatform', name: 'wavesj', version: '1.0.4'
compile group: 'com.wavesplatform', name: 'wavesj', version: '1.1.0'
```

##### SBT:
```
libraryDependencies += "com.wavesplatform" % "wavesj" % "1.0.4"
libraryDependencies += "com.wavesplatform" % "wavesj" % "1.1.0"
```

[This library's page at Maven Central](https://mvnrepository.com/artifact/com.wavesplatform/wavesj)
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.wavesplatform</groupId>
<artifactId>wavesj</artifactId>
<version>1.0.4</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<properties>
Expand Down Expand Up @@ -182,7 +182,7 @@
<dependency>
<groupId>com.wavesplatform</groupId>
<artifactId>waves-transactions</artifactId>
<version>1.0.5</version>
<version>1.0.6</version>
</dependency>

<!-- http client -->
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/wavesplatform/wavesj/AssetBalance.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
@SuppressWarnings("unused")
public class AssetBalance {

public final AssetId assetId;
public final long balance;
public final boolean reissuable;
public final long minSponsoredAssetFee;
public final long sponsorBalance;
public final long quantity;
public final Transaction issueTransaction;
private final AssetId assetId;
private final long balance;
private final boolean reissuable;
private final long minSponsoredAssetFee;
private final long sponsorBalance;
private final long quantity;
private final Transaction issueTransaction;

@JsonCreator
public AssetBalance(@JsonProperty("assetId") AssetId assetId,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/wavesplatform/wavesj/Balance.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.wavesplatform.wavesj;

import com.fasterxml.jackson.annotation.JsonProperty;
import im.mak.waves.transactions.account.Address;
import com.wavesplatform.transactions.account.Address;

import java.util.Objects;

Expand Down
38 changes: 25 additions & 13 deletions src/main/java/com/wavesplatform/wavesj/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,28 @@ public long getBalance(Address address, int confirmations) throws IOException, N
}

public List<Balance> getBalances(List<Address> addresses) throws IOException, NodeException {
RequestBuilder request = get("/addresses/balance");
addresses.forEach(address -> request.addParameter("address", address.toString()));
return asType(request, TypeRef.BALANCES);
ObjectNode jsonBody = JSON_MAPPER.createObjectNode();
ArrayNode jsonAddresses = jsonBody.putArray("addresses");
addresses.forEach(address -> jsonAddresses.add(address.toString()));

StringEntity body = new StringEntity(JSON_MAPPER.writeValueAsString(jsonBody), StandardCharsets.UTF_8);

return asType(post("/addresses/balance")
.addHeader("Content-Type", "application/json")
.setEntity(body), TypeRef.BALANCES);
}

public List<Balance> getBalances(List<Address> addresses, int height) throws IOException, NodeException {
RequestBuilder request = get("/addresses/balance");
addresses.forEach(address -> request.addParameter("address", address.toString()));
request.addParameter("height", String.valueOf(height));
return asType(request, TypeRef.BALANCES);
ObjectNode jsonBody = JSON_MAPPER.createObjectNode();
ArrayNode jsonAddresses = jsonBody.putArray("addresses");
addresses.forEach(address -> jsonAddresses.add(address.toString()));

jsonBody.put("height", height);
StringEntity body = new StringEntity(JSON_MAPPER.writeValueAsString(jsonBody), StandardCharsets.UTF_8);

return asType(post("/addresses/balance")
.addHeader("Content-Type", "application/json")
.setEntity(body), TypeRef.BALANCES);
}

public BalanceDetails getBalanceDetails(Address address) throws IOException, NodeException {
Expand Down Expand Up @@ -527,30 +539,30 @@ public ScriptInfo compileScript(String source) throws IOException, NodeException
// HTTP REQUESTS
//===============

private RequestBuilder get(String path) {
protected RequestBuilder get(String path) {
return RequestBuilder.get(uri.resolve(path));
}

private RequestBuilder post(String path) {
protected RequestBuilder post(String path) {
return RequestBuilder.post(uri.resolve(path));
}

private HttpResponse exec(HttpUriRequest request) throws IOException, NodeException {
protected HttpResponse exec(HttpUriRequest request) throws IOException, NodeException {
HttpResponse r = client.execute(request);
if (r.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
throw mapper.readValue(r.getEntity().getContent(), NodeException.class);
return r;
}

private InputStream asInputStream(RequestBuilder request) throws IOException, NodeException {
protected InputStream asInputStream(RequestBuilder request) throws IOException, NodeException {
return exec(request.build()).getEntity().getContent();
}

private <T> T asType(RequestBuilder request, TypeReference<T> reference) throws IOException, NodeException {
protected <T> T asType(RequestBuilder request, TypeReference<T> reference) throws IOException, NodeException {
return mapper.readValue(asInputStream(request), reference);
}

private JsonNode asJson(RequestBuilder request) throws IOException, NodeException {
protected JsonNode asJson(RequestBuilder request) throws IOException, NodeException {
return JSON_MAPPER.readTree(asInputStream(request));
}

Expand Down

0 comments on commit 3e4b083

Please sign in to comment.