-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathBasicProvider.java
88 lines (74 loc) · 3.22 KB
/
BasicProvider.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
package io.goodforgod.api.etherscan;
import io.goodforgod.api.etherscan.error.EtherScanParseException;
import io.goodforgod.api.etherscan.error.EtherScanRateLimitException;
import io.goodforgod.api.etherscan.error.EtherScanResponseException;
import io.goodforgod.api.etherscan.http.EthHttpClient;
import io.goodforgod.api.etherscan.manager.RequestQueueManager;
import io.goodforgod.api.etherscan.model.response.StringResponseTO;
import java.net.URI;
import java.nio.charset.StandardCharsets;
/**
* Base provider for API Implementations
*
* @author GoodforGod
* @see EtherScanAPIProvider
* @since 28.10.2018
*/
abstract class BasicProvider {
private static final String MAX_RATE_LIMIT_REACHED = "Max rate limit reached";
static final int MAX_END_BLOCK = Integer.MAX_VALUE;
static final int MIN_START_BLOCK = 0;
static final String ACT_PREFIX = "&action=";
private final String module;
private final String baseUrl;
private final EthHttpClient executor;
private final RequestQueueManager queue;
private final Converter converter;
BasicProvider(RequestQueueManager requestQueueManager,
String module,
String baseUrl,
EthHttpClient ethHttpClient,
Converter converter) {
this.queue = requestQueueManager;
this.module = "&module=" + module;
this.baseUrl = baseUrl;
this.executor = ethHttpClient;
this.converter = converter;
}
<T> T convert(byte[] json, Class<T> tClass) {
try {
final T t = converter.fromJson(json, tClass);
if (t instanceof StringResponseTO && ((StringResponseTO) t).getResult().startsWith(MAX_RATE_LIMIT_REACHED)) {
throw new EtherScanRateLimitException(((StringResponseTO) t).getResult());
}
return t;
} catch (Exception e) {
final StringResponseTO response = converter.fromJson(json, StringResponseTO.class);
if (response.getResult() != null && response.getStatus() == 0) {
if (response.getResult().startsWith(MAX_RATE_LIMIT_REACHED)) {
throw new EtherScanRateLimitException(response.getResult());
} else {
throw new EtherScanResponseException(response);
}
}
final String jsonAsString = new String(json, StandardCharsets.UTF_8);
throw new EtherScanParseException(e.getMessage() + ", for response: " + jsonAsString, e.getCause(), jsonAsString);
}
}
byte[] getRequest(String urlParameters) {
queue.takeTurn();
final URI uri = URI.create(baseUrl + module + urlParameters);
return executor.get(uri);
}
byte[] postRequest(String urlParameters, String dataToPost) {
queue.takeTurn();
final URI uri = URI.create(baseUrl + module + urlParameters);
return executor.post(uri, dataToPost.getBytes(StandardCharsets.UTF_8));
}
<T> T getRequest(String urlParameters, Class<T> tClass) {
return convert(getRequest(urlParameters), tClass);
}
<T> T postRequest(String urlParameters, String dataToPost, Class<T> tClass) {
return convert(postRequest(urlParameters, dataToPost), tClass);
}
}