Skip to content

Commit ddccdef

Browse files
committed
ToString()
HttpExecutor improvements + default read timeout 0 Coverage improvements + README fixs
1 parent 9512b09 commit ddccdef

26 files changed

+431
-38
lines changed

README.md

+12-9
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ dependencies {
4343
API support Ethereum: *[MAINNET](https://etherscan.io), [ROPSTEN](https://ropsten.etherscan.io),
4444
[KOVAN](https://kovan.etherscan.io), [RINKEBY](https://rinkeby.etherscan.io)* networks.
4545
```java
46-
EtherScanApi api = new EtherScanApi(EthNetwork.MAINTNET);
47-
EtherScanApi api = new EtherScanApi(EthNetwork.RINKEBY);
48-
EtherScanApi api = new EtherScanApi("YourApiKey", EthNetwork.KOVAN);
46+
EtherScanApi api = new EtherScanApi(EthNetwork.MAINNET); // Default
47+
EtherScanApi apiRinkeby = new EtherScanApi(EthNetwork.RINKEBY);
48+
EtherScanApi apiRopsten = new EtherScanApi(EthNetwork.ROPSTEN);
49+
EtherScanApi apiKovan = new EtherScanApi("YourApiKey", EthNetwork.KOVAN);
4950
```
5051

5152
## Custom HttpClient
@@ -56,10 +57,10 @@ just implement **IHttpExecutor** by your self or initialize it with your values.
5657
```java
5758
int connectionTimeout = 10000;
5859
int readTimeout = 7000;
60+
5961
Supplier<IHttpExecutor> supplier = () -> new HttpExecutor(connectionTimeout);
6062
Supplier<IHttpExecutor> supplierFull = () -> new HttpExecutor(connectionTimeout, readTimeout);
61-
62-
63+
6364
EtherScanApi api = new EtherScanApi(EthNetwork.RINKEBY, supplier);
6465
EtherScanApi apiWithKey = new EtherScanApi("YourApiKey", EthNetwork.MAINNET, supplierFull);
6566
```
@@ -70,14 +71,15 @@ You can read about all API methods on [Etherscan](https://etherscan.io/apis)
7071

7172
*Library support all available EtherScan API.*
7273

73-
You can use API with you key or without key as well (Check API request\sec restrictions).
74-
Library support limit when used without key and will limit requests up to *5 req/sec by itself*.
74+
You can use library *with or without* API key *([Check API request\sec restrictions when used without API key](https://ethereum.stackexchange.com/questions/34190/does-etherscan-require-the-use-of-an-api-key))*.
75+
76+
Library will automatically limit requests up to **5 req/sec** when used *without* key.
7577
```java
7678
EtherScanApi api = new EtherScanApi();
7779
EtherScanApi api = new EtherScanApi("YourApiKey");
7880
```
7981

80-
Below there are examples for each API category.
82+
Below are examples for each API category.
8183

8284
### Account Api
8385
**Get Ether Balance for a single Address**
@@ -121,6 +123,7 @@ LogQuery query = LogQueryBuilder.with("0x33990122638b9132ca29c723bdf037f1a891a70
121123
.setOpTopic0_2(LogOp.OR)
122124
.setOpTopic1_2(LogOp.AND)
123125
.build();
126+
124127
List<Log> logs = api.logs().logs(query);
125128
```
126129

@@ -152,7 +155,7 @@ Optional<Boolean> status = api.txs().receiptStatus("0x513c1ba0bebf66436b5fed86ab
152155
```
153156

154157
### Token Api
155-
You can read token API [here](https://etherscan.io/apis#tokens)
158+
You can read about token API [here](https://etherscan.io/apis#tokens)
156159

157160
Token API methods migrated to [Account](#account-api) & [Stats](#stats-api) respectfully.
158161

src/main/java/io/api/etherscan/core/impl/EtherScanApi.java

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.api.etherscan.manager.impl.QueueManager;
1111
import io.api.etherscan.model.EthNetwork;
1212
import io.api.etherscan.util.BasicUtils;
13+
import org.jetbrains.annotations.NotNull;
1314

1415
import java.util.function.Supplier;
1516

@@ -83,48 +84,55 @@ public EtherScanApi(final String apiKey,
8384
/**
8485
* API for interactions with account and address
8586
*/
87+
@NotNull
8688
public IAccountApi account() {
8789
return account;
8890
}
8991

9092
/**
9193
* API for verifying contract ABI
9294
*/
95+
@NotNull
9396
public IContractApi contract() {
9497
return contract;
9598
}
9699

97100
/**
98101
* [BETA] API for interaction with tx statuses
99102
*/
103+
@NotNull
100104
public ITransactionApi txs() {
101105
return txs;
102106
}
103107

104108
/**
105109
* [BETA] API for getting block rewards and uncles
106110
*/
111+
@NotNull
107112
public IBlockApi block() {
108113
return block;
109114
}
110115

111116
/**
112117
* [BETA] API for interaction with eth_getLogs
113118
*/
119+
@NotNull
114120
public ILogsApi logs() {
115121
return logs;
116122
}
117123

118124
/**
119125
* API for interacting with geth/proxy etherscan
120126
*/
127+
@NotNull
121128
public IProxyApi proxy() {
122129
return proxy;
123130
}
124131

125132
/**
126133
* API for eth price and supply statistic
127134
*/
135+
@NotNull
128136
public IStatisticApi stats() {
129137
return stats;
130138
}

src/main/java/io/api/etherscan/executor/impl/HttpExecutor.java

+13-9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.HashMap;
1616
import java.util.Map;
1717
import java.util.zip.GZIPInputStream;
18+
import java.util.zip.InflaterInputStream;
1819

1920
import static java.net.HttpURLConnection.HTTP_MOVED_PERM;
2021
import static java.net.HttpURLConnection.HTTP_MOVED_TEMP;
@@ -31,13 +32,12 @@ public class HttpExecutor implements IHttpExecutor {
3132
private static final Map<String, String> DEFAULT_HEADERS = new HashMap<>();
3233

3334
private static final int CONNECT_TIMEOUT = 8000;
34-
private static final int READ_TIMEOUT = 30000;
35+
private static final int READ_TIMEOUT = 0;
3536

3637
static {
37-
DEFAULT_HEADERS.put("Accept-Language", "en;q=0.9");
38+
DEFAULT_HEADERS.put("Accept-Language", "en");
3839
DEFAULT_HEADERS.put("Accept-Encoding", "deflate, gzip");
3940
DEFAULT_HEADERS.put("User-Agent", "Chrome/68.0.3440.106");
40-
DEFAULT_HEADERS.put("Content-Type", "application/x-www-form-urlencoded");
4141
DEFAULT_HEADERS.put("Accept-Charset", "UTF-8");
4242
}
4343

@@ -46,15 +46,14 @@ public class HttpExecutor implements IHttpExecutor {
4646
private final int readTimeout;
4747

4848
public HttpExecutor() {
49-
this(CONNECT_TIMEOUT, READ_TIMEOUT);
49+
this(CONNECT_TIMEOUT);
5050
}
5151

5252
public HttpExecutor(final int connectTimeout) {
5353
this(connectTimeout, READ_TIMEOUT);
5454
}
5555

56-
public HttpExecutor(final int connectTimeout,
57-
final int readTimeout) {
56+
public HttpExecutor(final int connectTimeout, final int readTimeout) {
5857
this(connectTimeout, readTimeout, DEFAULT_HEADERS);
5958
}
6059

@@ -142,8 +141,13 @@ private String readData(final HttpURLConnection connection) throws IOException {
142141
}
143142

144143
private InputStreamReader getStreamReader(final HttpURLConnection connection) throws IOException {
145-
return (connection.getContentEncoding() != null && "gzip".equals(connection.getContentEncoding()))
146-
? new InputStreamReader(new GZIPInputStream(connection.getInputStream()), "utf-8")
147-
: new InputStreamReader(connection.getInputStream(), "utf-8");
144+
final boolean haveEncoding = connection.getContentEncoding() != null;
145+
146+
if (haveEncoding && "gzip".equals(connection.getContentEncoding()))
147+
return new InputStreamReader(new GZIPInputStream(connection.getInputStream()), "utf-8");
148+
else if (haveEncoding && "deflate".equals(connection.getContentEncoding()))
149+
return new InputStreamReader(new InflaterInputStream(connection.getInputStream()), "utf-8");
150+
else
151+
return new InputStreamReader(connection.getInputStream(), "utf-8");
148152
}
149153
}

src/main/java/io/api/etherscan/model/Abi.java

+26
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,30 @@ public String getContractAbi() {
3636
public boolean isVerified() {
3737
return isVerified;
3838
}
39+
40+
@Override
41+
public boolean equals(Object o) {
42+
if (this == o) return true;
43+
if (o == null || getClass() != o.getClass()) return false;
44+
45+
Abi abi = (Abi) o;
46+
47+
if (isVerified != abi.isVerified) return false;
48+
return contractAbi != null ? contractAbi.equals(abi.contractAbi) : abi.contractAbi == null;
49+
}
50+
51+
@Override
52+
public int hashCode() {
53+
int result = contractAbi != null ? contractAbi.hashCode() : 0;
54+
result = 31 * result + (isVerified ? 1 : 0);
55+
return result;
56+
}
57+
58+
@Override
59+
public String toString() {
60+
return "Abi{" +
61+
"contractAbi='" + contractAbi + '\'' +
62+
", isVerified=" + isVerified +
63+
'}';
64+
}
3965
}

src/main/java/io/api/etherscan/model/BaseTx.java

+17
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,21 @@ public boolean equals(Object o) {
8484
public int hashCode() {
8585
return hash != null ? hash.hashCode() : 0;
8686
}
87+
88+
@Override
89+
public String toString() {
90+
return "BaseTx{" +
91+
"blockNumber=" + blockNumber +
92+
", timeStamp='" + timeStamp + '\'' +
93+
", _timeStamp=" + _timeStamp +
94+
", hash='" + hash + '\'' +
95+
", from='" + from + '\'' +
96+
", to='" + to + '\'' +
97+
", value=" + value +
98+
", contractAddress='" + contractAddress + '\'' +
99+
", input='" + input + '\'' +
100+
", gas=" + gas +
101+
", gasUsed=" + gasUsed +
102+
'}';
103+
}
87104
}

src/main/java/io/api/etherscan/model/Block.java

+10
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,14 @@ public boolean equals(Object o) {
4949
public int hashCode() {
5050
return (int) (blockNumber ^ (blockNumber >>> 32));
5151
}
52+
53+
@Override
54+
public String toString() {
55+
return "Block{" +
56+
"blockNumber=" + blockNumber +
57+
", blockReward=" + blockReward +
58+
", timeStamp='" + timeStamp + '\'' +
59+
", _timeStamp=" + _timeStamp +
60+
'}';
61+
}
5262
}

src/main/java/io/api/etherscan/model/Log.java

+22
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,26 @@ public Long getLogIndex() {
9797
return _logIndex;
9898
}
9999
//</editor-fold>
100+
101+
@Override
102+
public String toString() {
103+
return "Log{" +
104+
"blockNumber='" + blockNumber + '\'' +
105+
", _blockNumber=" + _blockNumber +
106+
", address='" + address + '\'' +
107+
", transactionHash='" + transactionHash + '\'' +
108+
", transactionIndex='" + transactionIndex + '\'' +
109+
", _transactionIndex=" + _transactionIndex +
110+
", timeStamp='" + timeStamp + '\'' +
111+
", _timeStamp=" + _timeStamp +
112+
", data='" + data + '\'' +
113+
", gasPrice='" + gasPrice + '\'' +
114+
", _gasPrice=" + _gasPrice +
115+
", gasUsed='" + gasUsed + '\'' +
116+
", _gasUsed=" + _gasUsed +
117+
", topics=" + topics +
118+
", logIndex='" + logIndex + '\'' +
119+
", _logIndex=" + _logIndex +
120+
'}';
121+
}
100122
}

src/main/java/io/api/etherscan/model/Price.java

+12
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,16 @@ public LocalDateTime btcTimestamp() {
3737
_ethbtc_timestamp = LocalDateTime.ofEpochSecond(Long.valueOf(ethbtc_timestamp), 0, ZoneOffset.UTC);
3838
return _ethbtc_timestamp;
3939
}
40+
41+
@Override
42+
public String toString() {
43+
return "Price{" +
44+
"ethusd=" + ethusd +
45+
", ethbtc=" + ethbtc +
46+
", ethusd_timestamp='" + ethusd_timestamp + '\'' +
47+
", ethbtc_timestamp='" + ethbtc_timestamp + '\'' +
48+
", _ethusd_timestamp=" + _ethusd_timestamp +
49+
", _ethbtc_timestamp=" + _ethbtc_timestamp +
50+
'}';
51+
}
4052
}

src/main/java/io/api/etherscan/model/Status.java

+8
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,12 @@ public boolean haveError() {
2121
public String getErrDescription() {
2222
return errDescription;
2323
}
24+
25+
@Override
26+
public String toString() {
27+
return "Status{" +
28+
"isError=" + isError +
29+
", errDescription='" + errDescription + '\'' +
30+
'}';
31+
}
2432
}

src/main/java/io/api/etherscan/model/TokenBalance.java

+7
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,11 @@ public int hashCode() {
3838
result = 31 * result + (tokenContract != null ? tokenContract.hashCode() : 0);
3939
return result;
4040
}
41+
42+
@Override
43+
public String toString() {
44+
return "TokenBalance{" +
45+
"tokenContract='" + tokenContract + '\'' +
46+
'}';
47+
}
4148
}

src/main/java/io/api/etherscan/model/Tx.java

+14
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,18 @@ public long getConfirmations() {
5454
return confirmations;
5555
}
5656
//</editor-fold>
57+
58+
@Override
59+
public String toString() {
60+
return "Tx{" +
61+
"nonce=" + nonce +
62+
", blockHash='" + blockHash + '\'' +
63+
", transactionIndex=" + transactionIndex +
64+
", gasPrice=" + gasPrice +
65+
", cumulativeGasUsed=" + cumulativeGasUsed +
66+
", confirmations=" + confirmations +
67+
", isError='" + isError + '\'' +
68+
", txreceipt_status='" + txreceipt_status + '\'' +
69+
'}';
70+
}
5771
}

src/main/java/io/api/etherscan/model/TxInternal.java

+10
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,14 @@ public String getErrCode() {
3030
return errCode;
3131
}
3232
//</editor-fold>
33+
34+
@Override
35+
public String toString() {
36+
return "TxInternal{" +
37+
"type='" + type + '\'' +
38+
", traceId=" + traceId +
39+
", isError=" + isError +
40+
", errCode='" + errCode + '\'' +
41+
'}';
42+
}
3343
}

src/main/java/io/api/etherscan/model/TxToken.java

+15
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,19 @@ public long getConfirmations() {
5555
return confirmations;
5656
}
5757
//</editor-fold>
58+
59+
@Override
60+
public String toString() {
61+
return "TxToken{" +
62+
"nonce=" + nonce +
63+
", blockHash='" + blockHash + '\'' +
64+
", tokenName='" + tokenName + '\'' +
65+
", tokenSymbol='" + tokenSymbol + '\'' +
66+
", tokenDecimal='" + tokenDecimal + '\'' +
67+
", transactionIndex=" + transactionIndex +
68+
", gasPrice=" + gasPrice +
69+
", cumulativeGasUsed=" + cumulativeGasUsed +
70+
", confirmations=" + confirmations +
71+
'}';
72+
}
5873
}

src/main/java/io/api/etherscan/model/Uncle.java

+9
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,13 @@ public int getUnclePosition() {
2727
return unclePosition;
2828
}
2929
//</editor-fold>
30+
31+
@Override
32+
public String toString() {
33+
return "Uncle{" +
34+
"miner='" + miner + '\'' +
35+
", blockreward=" + blockreward +
36+
", unclePosition=" + unclePosition +
37+
'}';
38+
}
3039
}

0 commit comments

Comments
 (0)