Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit260899b

Browse files
committed
[3.0.0-SNAPSHOT]
Added JdkEthHttpClient gzip/deflate protocol supportAdded EthScanAPIBuilder key required
1 parent8dbcc82 commit260899b

File tree

6 files changed

+59
-25
lines changed

6 files changed

+59
-25
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ protected <T> T getResponse(String urlParameters, Class<T> tClass) {
129129

130130
protected <T>TgetResponse(StringurlParameters,Class<T>tClass,intretryCount) {
131131
try {
132+
System.out.println("URL - " +URI.create(baseUrl +module +urlParameters));
132133
EthResponseresponse =getResponse(urlParameters);
134+
System.out.println("Response - " +newString(response.body(),StandardCharsets.UTF_8));
133135
returnconvert(response.body(),tClass);
134136
}catch (Exceptione) {
135137
if (retryCount <retryCountLimit) {

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
importjava.io.IOException;
1313
importjava.io.InputStreamReader;
1414
importjava.nio.charset.StandardCharsets;
15+
importjava.util.Objects;
1516
importjava.util.function.Supplier;
1617
importorg.jetbrains.annotations.NotNull;
1718

@@ -22,12 +23,11 @@
2223
publicclassEthScanAPIBuilderimplementsEtherScanAPI.Builder {
2324

2425
privatestaticfinalSupplier<EthHttpClient>DEFAULT_SUPPLIER =JdkEthHttpClient::new;
25-
privatestaticfinalStringDEFAULT_KEY ="YourApiKeyToken";
2626

2727
privatefinalGsongson =newGsonConfiguration().builder().create();
2828

2929
privateintretryCountOnLimitReach =0;
30-
privateStringapiKey =DEFAULT_KEY;
30+
privateStringapiKey;
3131
privateRequestQueueManagerqueueManager;
3232
privateEthNetworkethNetwork =EthNetworks.MAINNET;
3333
privateSupplier<EthHttpClient>ethHttpClientSupplier =DEFAULT_SUPPLIER;
@@ -43,6 +43,10 @@ public class EthScanAPIBuilder implements EtherScanAPI.Builder {
4343
}
4444
};
4545

46+
publicEthScanAPIBuilder(StringapiKey) {
47+
this.apiKey =apiKey;
48+
}
49+
4650
@NotNull
4751
@Override
4852
publicEtherScanAPI.BuilderwithApiKey(@NotNullStringapiKey) {
@@ -101,13 +105,7 @@ public EtherScanAPI.Builder withRetryOnRateLimit(int maxRetryCount) {
101105
@Override
102106
public@NotNullEtherScanAPIbuild() {
103107
RequestQueueManagerrequestQueueManager;
104-
if (queueManager !=null) {
105-
requestQueueManager =queueManager;
106-
}elseif (DEFAULT_KEY.equals(apiKey)) {
107-
requestQueueManager =RequestQueueManager.anonymous();
108-
}else {
109-
requestQueueManager =RequestQueueManager.planFree();
110-
}
108+
requestQueueManager =Objects.requireNonNullElseGet(queueManager,RequestQueueManager::planFree);
111109

112110
returnnewEtherScanAPIProvider(apiKey,ethNetwork,requestQueueManager,ethHttpClientSupplier.get(),
113111
converterSupplier.get(),retryCountOnLimitReach);

‎src/main/java/io/goodforgod/api/etherscan/EtherScanAPI.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public interface EtherScanAPI extends AutoCloseable {
4040
GasTrackerAPIgasTracker();
4141

4242
@NotNull
43-
staticBuilderbuilder() {
44-
returnnewEthScanAPIBuilder();
43+
staticBuilderbuilder(@NotNullStringkey) {
44+
returnnewEthScanAPIBuilder(key);
4545
}
4646

4747
interfaceBuilder {

‎src/main/java/io/goodforgod/api/etherscan/http/impl/JdkEthHttpClient.java‎

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
importio.goodforgod.api.etherscan.http.EthHttpClient;
66
importio.goodforgod.api.etherscan.http.EthResponse;
77
importjava.io.IOException;
8+
importjava.io.InputStream;
9+
importjava.io.UncheckedIOException;
810
importjava.net.URI;
911
importjava.net.http.HttpClient;
1012
importjava.net.http.HttpConnectTimeoutException;
1113
importjava.net.http.HttpRequest;
1214
importjava.net.http.HttpResponse;
1315
importjava.time.Duration;
14-
importjava.util.HashMap;
15-
importjava.util.List;
16-
importjava.util.Map;
16+
importjava.util.*;
17+
importjava.util.zip.GZIPInputStream;
18+
importjava.util.zip.InflaterInputStream;
1719
importorg.jetbrains.annotations.ApiStatus.Internal;
1820
importorg.jetbrains.annotations.NotNull;
1921

@@ -75,7 +77,9 @@ public EthResponse get(@NotNull URI uri) {
7577
headers.forEach(requestBuilder::header);
7678

7779
try {
78-
HttpResponse<byte[]>response =httpClient.send(requestBuilder.build(),HttpResponse.BodyHandlers.ofByteArray());
80+
HttpResponse<InputStream>response =httpClient.send(requestBuilder.build(),
81+
HttpResponse.BodyHandlers.ofInputStream());
82+
byte[]bodyAsBytes =getDeflatedBytes(response);
7983
returnnewEthResponse() {
8084

8185
@Override
@@ -85,7 +89,7 @@ public int statusCode() {
8589

8690
@Override
8791
publicbyte[]body() {
88-
returnresponse.body();
92+
returnbodyAsBytes;
8993
}
9094

9195
@Override
@@ -115,7 +119,9 @@ public EthResponse post(@NotNull URI uri, byte[] body) {
115119
headers.forEach(requestBuilder::header);
116120

117121
try {
118-
HttpResponse<byte[]>response =httpClient.send(requestBuilder.build(),HttpResponse.BodyHandlers.ofByteArray());
122+
HttpResponse<InputStream>response =httpClient.send(requestBuilder.build(),
123+
HttpResponse.BodyHandlers.ofInputStream());
124+
byte[]bodyAsBytes =getDeflatedBytes(response);
119125
returnnewEthResponse() {
120126

121127
@Override
@@ -125,7 +131,7 @@ public int statusCode() {
125131

126132
@Override
127133
publicbyte[]body() {
128-
returnresponse.body();
134+
returnbodyAsBytes;
129135
}
130136

131137
@Override
@@ -144,4 +150,28 @@ public byte[] body() {
144150
thrownewEtherScanConnectionException("Etherscan HTTP server interrupt exception occurred: " +e.getMessage(),e);
145151
}
146152
}
153+
154+
privatestaticbyte[]getDeflatedBytes(HttpResponse<InputStream>response) {
155+
try {
156+
Optional<String>encoding =response.headers().firstValue("content-encoding");
157+
if (encoding.isEmpty()) {
158+
try (varis =response.body()) {
159+
returnis.readAllBytes();
160+
}
161+
}
162+
163+
switch (encoding.get().strip().toLowerCase(Locale.ROOT)) {
164+
case"gzip":
165+
returnnewGZIPInputStream(response.body()).readAllBytes();
166+
case"deflate":
167+
returnnewInflaterInputStream(response.body()).readAllBytes();
168+
default:
169+
try (varis =response.body()) {
170+
returnis.readAllBytes();
171+
}
172+
}
173+
}catch (IOExceptione) {
174+
thrownewUncheckedIOException(e);
175+
}
176+
}
147177
}

‎src/test/java/io/goodforgod/api/etherscan/ApiRunner.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public class ApiRunner extends Assertions {
2525
?RequestQueueManager.anonymous()
2626
:RequestQueueManager.planFree();
2727

28-
API =EtherScanAPI.builder()
29-
.withApiKey(ApiRunner.API_KEY)
28+
API =EtherScanAPI.builder(ApiRunner.API_KEY)
3029
.withNetwork(EthNetworks.MAINNET)
3130
.withQueue(queueManager)
3231
.withRetryOnRateLimit(5)
@@ -37,6 +36,10 @@ public static EtherScanAPI getApi() {
3736
returnAPI;
3837
}
3938

39+
publicstaticStringgetKey() {
40+
returnAPI_KEY;
41+
}
42+
4043
@AfterAll
4144
publicstaticvoidcleanup()throwsException {
4245
API.close();

‎src/test/java/io/goodforgod/api/etherscan/EtherScanAPITests.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,26 @@ class EtherScanAPITests extends ApiRunner {
2222
@Test
2323
voidvalidKey() {
2424
StringvalidKey ="YourKey";
25-
EtherScanAPIapi =EtherScanAPI.builder().withApiKey(validKey).withNetwork(network).build();
25+
EtherScanAPIapi =EtherScanAPI.builder(validKey).withApiKey(validKey).withNetwork(network).build();
2626
assertNotNull(api);
2727
}
2828

2929
@Test
3030
voidemptyKey() {
31-
assertThrows(EtherScanKeyException.class, () ->EtherScanAPI.builder().withApiKey("").build());
31+
assertThrows(EtherScanKeyException.class, () ->EtherScanAPI.builder("someKey").withApiKey("").build());
3232
}
3333

3434
@Test
3535
voidblankKey() {
3636
assertThrows(EtherScanKeyException.class,
37-
() ->EtherScanAPI.builder().withApiKey(" ").withNetwork(network).build());
37+
() ->EtherScanAPI.builder("someKey").withApiKey(" ").withNetwork(network).build());
3838
}
3939

4040
@Test
4141
voidnoTimeoutOnRead() {
4242
Supplier<EthHttpClient>supplier = () ->newUrlEthHttpClient(Duration.ofMillis(300));
43-
EtherScanAPIapi =EtherScanAPI.builder().withNetwork(EthNetworks.MAINNET).withHttpClient(supplier).build();
43+
EtherScanAPIapi =EtherScanAPI.builder(ApiRunner.getKey()).withNetwork(EthNetworks.MAINNET).withHttpClient(supplier)
44+
.build();
4445
Balancebalance =api.account().balance("0xF318ABc9A5a92357c4Fea8d082dade4D43e780B7");
4546
assertNotNull(balance);
4647
}
@@ -67,7 +68,7 @@ void noTimeoutUnlimitedAwait() {
6768
voidtimeout()throwsInterruptedException {
6869
TimeUnit.SECONDS.sleep(5);
6970
Supplier<EthHttpClient>supplier = () ->newUrlEthHttpClient(Duration.ofMillis(300),Duration.ofMillis(300));
70-
EtherScanAPIapi =EtherScanAPI.builder()
71+
EtherScanAPIapi =EtherScanAPI.builder(ApiRunner.getKey())
7172
.withNetwork(() ->URI.create("https://api-unknown.etherscan.io/api"))
7273
.withHttpClient(supplier)
7374
.build();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp