-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathEthScanAPIBuilder.java
97 lines (83 loc) · 3.3 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
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 String apiKey = DEFAULT_KEY;
private EthNetwork ethNetwork = EthNetworks.MAINNET;
private RequestQueueManager queueManager = RequestQueueManager.ANONYMOUS;
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;
if (!DEFAULT_KEY.equals(apiKey)) {
queueManager = RequestQueueManager.UNLIMITED;
}
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;
}
@Override
public @NotNull EtherScanAPI build() {
return new EtherScanAPIProvider(apiKey, ethNetwork, queueManager, ethHttpClientSupplier.get(), converterSupplier.get());
}
}