-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathEthScanAPIBuilder.java
115 lines (98 loc) Β· 3.93 KB
/
EthScanAPIBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package io.goodforgod.api.etherscan;
import com.google.gson.Gson;
import io.goodforgod.api.etherscan.error.EtherScanKeyException;
import io.goodforgod.api.etherscan.error.EtherScanParseException;
import io.goodforgod.api.etherscan.http.EthHttpClient;
import io.goodforgod.api.etherscan.http.impl.UrlEthHttpClient;
import io.goodforgod.api.etherscan.manager.RequestQueueManager;
import io.goodforgod.api.etherscan.util.BasicUtils;
import io.goodforgod.gson.configuration.GsonConfiguration;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.function.Supplier;
import org.jetbrains.annotations.NotNull;
/**
* @author Anton Kurako (GoodforGod)
* @since 11.05.2023
*/
final class EthScanAPIBuilder implements EtherScanAPI.Builder {
private static final Supplier<EthHttpClient> DEFAULT_SUPPLIER = UrlEthHttpClient::new;
private static final String DEFAULT_KEY = "YourApiKeyToken";
private final Gson gson = new GsonConfiguration().builder().create();
private int retryCountOnLimitReach = 0;
private String apiKey = DEFAULT_KEY;
private RequestQueueManager queueManager;
private EthNetwork ethNetwork = EthNetworks.MAINNET;
private Supplier<EthHttpClient> ethHttpClientSupplier = DEFAULT_SUPPLIER;
private Supplier<Converter> converterSupplier = () -> new Converter() {
@Override
public <T> @NotNull T fromJson(byte[] jsonAsByteArray, @NotNull Class<T> type) {
try (InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(jsonAsByteArray))) {
return gson.fromJson(isr, type);
} catch (IOException e) {
throw new EtherScanParseException(e.getMessage(), e, new String(jsonAsByteArray, StandardCharsets.UTF_8));
}
}
};
@NotNull
@Override
public EtherScanAPI.Builder withApiKey(@NotNull String apiKey) {
if (BasicUtils.isBlank(apiKey))
throw new EtherScanKeyException("API key can not be null or empty");
this.apiKey = apiKey;
return this;
}
@NotNull
@Override
public EtherScanAPI.Builder withNetwork(@NotNull EthNetwork network) {
this.ethNetwork = network;
return this;
}
@NotNull
@Override
public EtherScanAPI.Builder withNetwork(@NotNull EthNetworks network) {
this.ethNetwork = network;
return this;
}
@NotNull
@Override
public EtherScanAPI.Builder withQueue(@NotNull RequestQueueManager queueManager) {
this.queueManager = queueManager;
return this;
}
@NotNull
@Override
public EtherScanAPI.Builder withHttpClient(@NotNull Supplier<EthHttpClient> httpClientSupplier) {
this.ethHttpClientSupplier = httpClientSupplier;
return this;
}
@NotNull
@Override
public EtherScanAPI.Builder withConverter(@NotNull Supplier<Converter> converterSupplier) {
this.converterSupplier = converterSupplier;
return this;
}
@NotNull
public EtherScanAPI.Builder withRetryOnRateLimit(int maxRetryCount) {
if (maxRetryCount < 0 || maxRetryCount > 20) {
throw new IllegalStateException("maxRetryCount value must be in range from 0 to 20, but was: " + maxRetryCount);
}
this.retryCountOnLimitReach = maxRetryCount;
return this;
}
@Override
public @NotNull EtherScanAPI build() {
RequestQueueManager requestQueueManager;
if (queueManager != null) {
requestQueueManager = queueManager;
} else if (DEFAULT_KEY.equals(apiKey)) {
requestQueueManager = RequestQueueManager.anonymous();
} else {
requestQueueManager = RequestQueueManager.planFree();
}
return new EtherScanAPIProvider(apiKey, ethNetwork, requestQueueManager, ethHttpClientSupplier.get(),
converterSupplier.get(), retryCountOnLimitReach);
}
}