Skip to content

Commit 3210c39

Browse files
committed
[2.0.0-SNAPSHOT]
EthHttpClient contract refactored to response with byte[] Converter contract refactored to receive byte[]
1 parent 948a6f3 commit 3210c39

File tree

10 files changed

+54
-41
lines changed

10 files changed

+54
-41
lines changed

src/main/java/io/goodforgod/api/etherscan/BasicProvider.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import io.goodforgod.api.etherscan.http.EthHttpClient;
88
import io.goodforgod.api.etherscan.manager.RequestQueueManager;
99
import io.goodforgod.api.etherscan.model.response.StringResponseTO;
10-
import io.goodforgod.api.etherscan.util.BasicUtils;
1110
import java.net.URI;
1211
import java.nio.charset.StandardCharsets;
1312
import java.util.Map;
@@ -44,7 +43,7 @@ abstract class BasicProvider {
4443
this.converter = converter;
4544
}
4645

47-
<T> T convert(String json, Class<T> tClass) {
46+
<T> T convert(byte[] json, Class<T> tClass) {
4847
try {
4948
final T t = converter.fromJson(json, tClass);
5049
if (t instanceof StringResponseTO && ((StringResponseTO) t).getResult().startsWith("Max rate limit reached")) {
@@ -53,32 +52,33 @@ <T> T convert(String json, Class<T> tClass) {
5352

5453
return t;
5554
} catch (Exception e) {
55+
final String jsonAsString = new String(json, StandardCharsets.UTF_8);
5656
try {
5757
final Map<String, Object> map = converter.fromJson(json, Map.class);
5858
final Object result = map.get("result");
5959
if (result instanceof String && ((String) result).startsWith("Max rate limit reached"))
6060
throw new EtherScanRateLimitException(((String) result));
6161

62-
throw new EtherScanParseException(e.getMessage() + ", for response: " + json, e.getCause(), json);
62+
throw new EtherScanParseException(e.getMessage() + ", for response: " + jsonAsString, e.getCause(), jsonAsString);
6363
} catch (EtherScanException ex) {
6464
throw ex;
6565
} catch (Exception ex) {
66-
throw new EtherScanParseException(e.getMessage() + ", for response: " + json, e.getCause(), json);
66+
throw new EtherScanParseException(e.getMessage() + ", for response: " + jsonAsString, e.getCause(), jsonAsString);
6767
}
6868
}
6969
}
7070

71-
String getRequest(String urlParameters) {
71+
byte[] getRequest(String urlParameters) {
7272
queue.takeTurn();
7373
final URI uri = URI.create(baseUrl + module + urlParameters);
74-
final String result = executor.get(uri);
75-
if (BasicUtils.isEmpty(result))
74+
final byte[] result = executor.get(uri);
75+
if (result.length == 0)
7676
throw new EtherScanResponseException("Server returned null value for GET request at URL - " + uri);
7777

7878
return result;
7979
}
8080

81-
String postRequest(String urlParameters, String dataToPost) {
81+
byte[] postRequest(String urlParameters, String dataToPost) {
8282
queue.takeTurn();
8383
final URI uri = URI.create(baseUrl + module + urlParameters);
8484
return executor.post(uri, dataToPost.getBytes(StandardCharsets.UTF_8));

src/main/java/io/goodforgod/api/etherscan/BlockAPIProvider.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@ final class BlockAPIProvider extends BasicProvider implements BlockAPI {
3333
@Override
3434
public Optional<BlockUncle> uncles(long blockNumber) throws EtherScanException {
3535
final String urlParam = ACT_BLOCK_PARAM + BLOCKNO_PARAM + blockNumber;
36-
final String response = getRequest(urlParam);
37-
if (BasicUtils.isEmpty(response) || response.contains("NOTOK"))
36+
final byte[] response = getRequest(urlParam);
37+
if (response.length == 0)
3838
return Optional.empty();
3939

4040
final UncleBlockResponseTO responseTO = convert(response, UncleBlockResponseTO.class);
41+
if (responseTO.getMessage().equals("NOTOK")) {
42+
return Optional.empty();
43+
}
44+
4145
BasicUtils.validateTxResponse(responseTO);
4246
return (responseTO.getResult() == null || responseTO.getResult().isEmpty())
4347
? Optional.empty()

src/main/java/io/goodforgod/api/etherscan/ContractAPIProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ public Abi contractAbi(String address) throws EtherScanException {
3636

3737
final String urlParam = ACT_ABI_PARAM + ADDRESS_PARAM + address;
3838
final StringResponseTO response = getRequest(urlParam, StringResponseTO.class);
39-
if (response.getStatus() != 1 && "NOTOK".equals(response.getMessage()))
39+
if (response.getStatus() != 1 && "NOTOK".equals(response.getMessage())) {
4040
throw new EtherScanResponseException(response);
41+
}
4142

4243
return (response.getResult().startsWith("Contract sou"))
4344
? Abi.nonVerified()

src/main/java/io/goodforgod/api/etherscan/Converter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
public interface Converter {
1010

1111
@NotNull
12-
<T> T fromJson(@NotNull String json, @NotNull Class<T> type);
12+
<T> T fromJson(byte[] jsonAsByteArray, @NotNull Class<T> type);
1313
}

src/main/java/io/goodforgod/api/etherscan/EthScanAPIBuilder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.goodforgod.api.etherscan.manager.RequestQueueManager;
88
import io.goodforgod.api.etherscan.util.BasicUtils;
99
import io.goodforgod.gson.configuration.GsonConfiguration;
10+
import java.nio.charset.StandardCharsets;
1011
import java.util.function.Supplier;
1112
import org.jetbrains.annotations.NotNull;
1213

@@ -28,8 +29,9 @@ final class EthScanAPIBuilder implements EtherScanAPI.Builder {
2829
private Supplier<Converter> converterSupplier = () -> new Converter() {
2930

3031
@Override
31-
public <T> @NotNull T fromJson(@NotNull String json, @NotNull Class<T> type) {
32-
return gson.fromJson(json, type);
32+
public <T> @NotNull T fromJson(byte[] jsonAsByteArray, @NotNull Class<T> type) {
33+
final String jsonAsString = new String(jsonAsByteArray, StandardCharsets.UTF_8);
34+
return gson.fromJson(jsonAsString, type);
3335
}
3436
};
3537

src/main/java/io/goodforgod/api/etherscan/http/EthHttpClient.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ public interface EthHttpClient {
1717
* @param uri as string
1818
* @return result as string
1919
*/
20-
@NotNull
21-
String get(@NotNull URI uri);
20+
byte[] get(@NotNull URI uri);
2221

2322
/**
2423
* Performs a Http POST request
@@ -27,6 +26,5 @@ public interface EthHttpClient {
2726
* @param body to post
2827
* @return result as string
2928
*/
30-
@NotNull
31-
String post(@NotNull URI uri, byte[] body);
29+
byte[] post(@NotNull URI uri, byte[] body);
3230
}

src/main/java/io/goodforgod/api/etherscan/http/impl/UrlEthHttpClient.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,11 @@
55
import io.goodforgod.api.etherscan.error.EtherScanConnectionException;
66
import io.goodforgod.api.etherscan.error.EtherScanTimeoutException;
77
import io.goodforgod.api.etherscan.http.EthHttpClient;
8-
import java.io.BufferedReader;
9-
import java.io.IOException;
10-
import java.io.InputStreamReader;
11-
import java.io.OutputStream;
8+
import java.io.*;
129
import java.net.HttpURLConnection;
1310
import java.net.SocketTimeoutException;
1411
import java.net.URI;
1512
import java.net.URL;
16-
import java.nio.charset.StandardCharsets;
1713
import java.time.Duration;
1814
import java.util.Collections;
1915
import java.util.HashMap;
@@ -83,7 +79,7 @@ private HttpURLConnection buildConnection(URI uri, String method) throws IOExcep
8379
}
8480

8581
@Override
86-
public @NotNull String get(@NotNull URI uri) {
82+
public byte[] get(@NotNull URI uri) {
8783
try {
8884
final HttpURLConnection connection = buildConnection(uri, "GET");
8985
final int status = connection.getResponseCode();
@@ -95,7 +91,7 @@ private HttpURLConnection buildConnection(URI uri, String method) throws IOExcep
9591
throw new EtherScanConnectionException("Server error: " + connection.getResponseMessage());
9692
}
9793

98-
final String data = readData(connection);
94+
final byte[] data = readData(connection);
9995
connection.disconnect();
10096
return data;
10197
} catch (SocketTimeoutException e) {
@@ -106,7 +102,7 @@ private HttpURLConnection buildConnection(URI uri, String method) throws IOExcep
106102
}
107103

108104
@Override
109-
public @NotNull String post(@NotNull URI uri, byte[] body) {
105+
public byte[] post(@NotNull URI uri, byte[] body) {
110106
try {
111107
final HttpURLConnection connection = buildConnection(uri, "POST");
112108
final int contentLength = body.length;
@@ -129,7 +125,7 @@ private HttpURLConnection buildConnection(URI uri, String method) throws IOExcep
129125
throw new EtherScanConnectionException("Server error: " + connection.getResponseMessage());
130126
}
131127

132-
final String data = readData(connection);
128+
final byte[] data = readData(connection);
133129
connection.disconnect();
134130
return data;
135131
} catch (SocketTimeoutException e) {
@@ -139,25 +135,29 @@ private HttpURLConnection buildConnection(URI uri, String method) throws IOExcep
139135
}
140136
}
141137

142-
private String readData(HttpURLConnection connection) throws IOException {
143-
final StringBuilder content = new StringBuilder();
144-
try (BufferedReader in = new BufferedReader(getStreamReader(connection))) {
145-
String inputLine;
146-
while ((inputLine = in.readLine()) != null)
147-
content.append(inputLine);
148-
}
138+
private byte[] readData(HttpURLConnection connection) throws IOException {
139+
try (ByteArrayOutputStream buffer = new ByteArrayOutputStream()) {
140+
try (InputStream in = getStreamReader(connection)) {
141+
byte[] data = new byte[256];
142+
int nRead;
143+
while ((nRead = in.read(data, 0, data.length)) != -1) {
144+
buffer.write(data, 0, nRead);
145+
}
146+
}
149147

150-
return content.toString();
148+
buffer.flush();
149+
return buffer.toByteArray();
150+
}
151151
}
152152

153-
private InputStreamReader getStreamReader(HttpURLConnection connection) throws IOException {
153+
private InputStream getStreamReader(HttpURLConnection connection) throws IOException {
154154
switch (String.valueOf(connection.getContentEncoding())) {
155155
case "gzip":
156-
return new InputStreamReader(new GZIPInputStream(connection.getInputStream()), StandardCharsets.UTF_8);
156+
return new GZIPInputStream(connection.getInputStream());
157157
case "deflate":
158-
return new InputStreamReader(new InflaterInputStream(connection.getInputStream()), StandardCharsets.UTF_8);
158+
return new InflaterInputStream(connection.getInputStream());
159159
default:
160-
return new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8);
160+
return connection.getInputStream();
161161
}
162162
}
163163
}

src/main/java/io/goodforgod/api/etherscan/manager/RequestQueueManager.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import java.time.Duration;
66

77
/**
8-
* Queue manager to support API limits (EtherScan 5request\sec limit) Managers grants turn if the
9-
* limit is not exhausted And resets queue each set period
8+
* Queue manager to support API limits
9+
* Manager grants turn if the limit is not exhausted And resets queue each set period
1010
*
1111
* @author GoodforGod
1212
* @since 30.10.2018
@@ -23,6 +23,9 @@ public interface RequestQueueManager extends AutoCloseable {
2323
* <a href="https://docs.etherscan.io/getting-started/viewing-api-usage-statistics">Free API KEY</a>
2424
*/
2525
RequestQueueManager FREE_PLAN = new SemaphoreRequestQueueManager(5, Duration.ofMillis(1010L));
26+
RequestQueueManager STANDARD_PLAN = new SemaphoreRequestQueueManager(10, Duration.ofMillis(1010L));
27+
RequestQueueManager ADVANCED_PLAN = new SemaphoreRequestQueueManager(20, Duration.ofMillis(1010L));
28+
RequestQueueManager PROFESSIONAL_PLAN = new SemaphoreRequestQueueManager(30, Duration.ofMillis(1010L));
2629

2730
RequestQueueManager UNLIMITED = new FakeRequestQueueManager();
2831

src/main/java/io/goodforgod/api/etherscan/model/Wei.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ public class Wei {
1111

1212
private final BigInteger result;
1313

14+
public Wei(int value) {
15+
this.result = BigInteger.valueOf(value);
16+
}
17+
1418
public Wei(long value) {
1519
this.result = BigInteger.valueOf(value);
1620
}

src/test/java/io/goodforgod/api/etherscan/model/ModelBuilderTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ void txInternalBuilder() {
252252
.withContractAddress("1")
253253
.withFrom("1")
254254
.withTo("1")
255+
.withValue(BigInteger.ONE)
255256
.withGas(BigInteger.ONE)
256257
.withGasUsed(BigInteger.ONE)
257258
.withHash("1")

0 commit comments

Comments
 (0)