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

Commit3795b6d

Browse files
committed
HttpExecutor gzip comparison support
EtherScan constructor testsMinor improvements & fixes
1 parent2af17f0 commit3795b6d

File tree

8 files changed

+217
-14
lines changed

8 files changed

+217
-14
lines changed

‎README.md‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,89 @@
11
#java-etherscan-api
2+
3+
[Etherscan](https://etherscan.io/apis) Java API implementation.
4+
5+
6+
![](https://media.giphy.com/media/1msHfmVdtuwkXww4ZC/giphy.gif)
7+
8+
##Dependency:rocket:
9+
**Maven**
10+
```xml
11+
<dependency>
12+
<groupId>com.github.goodforgod</groupId>
13+
<artifactId>java-etherscan-api</artifactId>
14+
<version>1.0.0</version>
15+
</dependency>
16+
```
17+
18+
**Gradle**
19+
```groovy
20+
dependencies {
21+
compile 'com.github.goodforgod:java-etherscan-api:1.0.0'
22+
}
23+
```
24+
25+
##Content
26+
-[Overall](#overall)
27+
-[API examples](#api-examples)
28+
-[Account](#account-api)
29+
-[Block](#block-api)
30+
-[Contract](#contract-api)
31+
-[Logs](#logs-api)
32+
-[Proxy](#proxy-api)
33+
-[Stats](#stats-api)
34+
-[Transactions](#transaction-api)
35+
-[Version History](#version-history)
36+
37+
##Overall
38+
39+
How all is linked together:
40+
41+
##Api Examples
42+
43+
You can read about all API methods on[Etherscan](https://etherscan.io/apis)
44+
45+
You can you API with you key or without key as well.
46+
```java
47+
EtherScanApi api=newEtherScanApi();
48+
EtherScanApi api=newEtherScanApi("YourApiKey");
49+
```
50+
51+
###Mainnet and Testnets
52+
API support:*MAINNET, ROPSTEN, KOVAN, RINKEBY* networks.
53+
```java
54+
EtherScanApi api=newEtherScanApi(EthNetwork.MAINNET);
55+
EtherScanApi api=newEtherScanApi("YourApiKey",EthNetwork.KOVAN);
56+
```
57+
58+
###Account Api
59+
**Get Ether Balance for a single Address Example**
60+
```java
61+
EtherScanApi api=newEtherScanApi();
62+
Balance balance= api.account().balance("0x8d4426f94e42f721C7116E81d6688cd935cB3b4F");
63+
```
64+
65+
###Block Api
66+
67+
###Contract Api
68+
69+
###Logs Api
70+
71+
###Proxy Api
72+
73+
###Stats Api
74+
75+
###Transaction Api
76+
77+
78+
###Token Api
79+
You can read account API[here](https://etherscan.io/apis#accounts)
80+
81+
Token API migrated to account & stats respectfully.
82+
83+
##Version History
84+
85+
**1.0.0** - Initial project with all API functionality.
86+
87+
##License
88+
89+
This project is licensed under the MIT - see the[LICENSE](LICENSE) file for details.

‎src/main/java/io/api/etherscan/App.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
importio.api.etherscan.core.impl.EtherScanApi;
44
importio.api.etherscan.model.Balance;
5+
importio.api.etherscan.model.EthNetwork;
56

67
/**
78
*
89
*/
910
publicclassApp {
1011
publicstaticvoidmain(String[]args) {
11-
EtherScanApiapi =newEtherScanApi();
12+
EtherScanApiapi =newEtherScanApi(EthNetwork.MAINNET);
1213
Balancebalance =api.account().balance("0x8d4426f94e42f721C7116E81d6688cd935cB3b4F");
1314
System.out.println("Test");
1415
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
packageio.api.etherscan.core.impl;
22

33
importio.api.etherscan.core.*;
4+
importio.api.etherscan.error.ApiKeyException;
45
importio.api.etherscan.executor.IHttpExecutor;
56
importio.api.etherscan.executor.impl.HttpExecutor;
67
importio.api.etherscan.manager.IQueueManager;
@@ -50,8 +51,8 @@ public EtherScanApi(final String apiKey,
5051
publicEtherScanApi(finalStringapiKey,
5152
finalEthNetworknetwork,
5253
finalSupplier<IHttpExecutor>executorSupplier) {
53-
if (BasicUtils.isEmpty(apiKey))
54-
thrownewNullPointerException("API key can not be null");
54+
if (BasicUtils.isBlank(apiKey))
55+
thrownewApiKeyException("API key can not be null or empty");
5556

5657
// EtherScan 5request\sec limit support by queue manager
5758
finalIQueueManagermasterQueue = (apiKey.equals("YourApiKeyToken"))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
packageio.api.etherscan.error;
2+
3+
/**
4+
* ! NO DESCRIPTION !
5+
*
6+
* @author GoodforGod
7+
* @since 05.11.2018
8+
*/
9+
publicclassApiKeyExceptionextendsApiException {
10+
11+
publicApiKeyException(Stringmessage) {
12+
super(message);
13+
}
14+
}

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
importjava.net.URL;
1313
importjava.util.HashMap;
1414
importjava.util.Map;
15+
importjava.util.zip.GZIPInputStream;
1516

1617
importstaticjava.net.HttpURLConnection.HTTP_MOVED_PERM;
1718
importstaticjava.net.HttpURLConnection.HTTP_MOVED_TEMP;
@@ -28,10 +29,11 @@ public class HttpExecutor implements IHttpExecutor {
2829
privatestaticfinalMap<String,String>DEFAULT_HEADERS =newHashMap<>();
2930

3031
static {
31-
DEFAULT_HEADERS.put("accept-language","en,ru;q=0.9");
32-
DEFAULT_HEADERS.put("accept-encoding","gzip, deflate, br");
33-
DEFAULT_HEADERS.put("user-agent","Mozilla/5.0 (Windows NT 10.0; WOW64) Chrome/68.0.3440.106");
34-
DEFAULT_HEADERS.put("content-type","application/x-www-form-urlencoded");
32+
DEFAULT_HEADERS.put("Accept-Language","en;q=0.9");
33+
DEFAULT_HEADERS.put("Accept-Encoding","deflate, gzip");
34+
DEFAULT_HEADERS.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) Chrome/68.0.3440.106");
35+
DEFAULT_HEADERS.put("Content-Type","application/x-www-form-urlencoded");
36+
DEFAULT_HEADERS.put("Accept-Charset","UTF-8");
3537
}
3638

3739
privatefinalMap<String,String>headers;
@@ -113,7 +115,7 @@ public String post(final String urlAsString, final String dataToPost) {
113115

114116
privateStringreadData(finalHttpURLConnectionconnection)throwsIOException {
115117
finalStringBuildercontent =newStringBuilder();
116-
try (BufferedReaderin =newBufferedReader(newInputStreamReader(connection.getInputStream()))) {
118+
try (BufferedReaderin =newBufferedReader(getStreamReader(connection))) {
117119
StringinputLine;
118120
while ((inputLine =in.readLine()) !=null)
119121
content.append(inputLine);
@@ -123,4 +125,10 @@ private String readData(final HttpURLConnection connection) throws IOException {
123125

124126
returncontent.toString();
125127
}
128+
129+
privateInputStreamReadergetStreamReader(finalHttpURLConnectionconnection)throwsIOException {
130+
return ("gzip".equals(connection.getContentEncoding()))
131+
?newInputStreamReader(newGZIPInputStream(connection.getInputStream()),"utf-8")
132+
:newInputStreamReader(connection.getInputStream(),"utf-8");
133+
}
126134
}

‎src/main/java/io/api/etherscan/util/BasicUtils.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
importjava.util.regex.Pattern;
1313

1414
/**
15-
*! NO DESCRIPTION !
15+
*Basic utils for library
1616
*
1717
* @author GoodforGod
1818
* @since 28.10.2018
@@ -30,7 +30,7 @@ public static boolean isEmpty(String value) {
3030
returnvalue ==null ||value.isEmpty();
3131
}
3232

33-
publicstaticbooleanisBlack(Stringvalue) {
33+
publicstaticbooleanisBlank(Stringvalue) {
3434
returnvalue ==null ||value.isEmpty() ||value.trim().isEmpty();
3535
}
3636

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
packageio.api.etherscan;
2+
3+
importio.api.etherscan.core.impl.EtherScanApi;
4+
importio.api.etherscan.error.ApiKeyException;
5+
importio.api.etherscan.model.EthNetwork;
6+
importorg.junit.Assert;
7+
importorg.junit.Test;
8+
9+
/**
10+
* ! NO DESCRIPTION !
11+
*
12+
* @author GoodforGod
13+
* @since 05.11.2018
14+
*/
15+
publicclassEtherScanApiTestextendsAssert {
16+
17+
privateEthNetworknetwork =EthNetwork.KOVAN;
18+
privateStringvalidKey ="YourKey";
19+
20+
@Test
21+
publicvoidvalidKey() {
22+
EtherScanApiapi =newEtherScanApi(validKey,network);
23+
assertNotNull(api);
24+
}
25+
26+
@Test(expected =ApiKeyException.class)
27+
publicvoidemptyKey() {
28+
StringemptyKey ="";
29+
EtherScanApiapi =newEtherScanApi(emptyKey,network);
30+
}
31+
32+
@Test(expected =ApiKeyException.class)
33+
publicvoidblankKey() {
34+
StringblankKey =" ";
35+
EtherScanApiapi =newEtherScanApi(blankKey,network);
36+
}
37+
38+
@Test
39+
publicvoidnullNetwork() {
40+
EtherScanApiapi =newEtherScanApi(validKey,null);
41+
assertNotNull(api);
42+
}
43+
}

‎src/test/java/io/api/etherscan/account/AccountBalanceTest.java‎

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,70 @@
33
importio.api.etherscan.core.impl.EtherScanApi;
44
importio.api.etherscan.error.InvalidAddressException;
55
importio.api.etherscan.model.Balance;
6+
importio.api.etherscan.model.EthNetwork;
67
importorg.junit.Assert;
78
importorg.junit.Test;
9+
importorg.junit.runner.RunWith;
10+
importorg.junit.runners.Parameterized;
11+
importorg.junit.runners.Parameterized.Parameters;
12+
13+
importjava.util.Arrays;
14+
importjava.util.Collection;
815

916
/**
1017
* ! NO DESCRIPTION !
1118
*
1219
* @author GoodforGod
1320
* @since 03.11.2018
1421
*/
22+
@RunWith(Parameterized.class)
1523
publicclassAccountBalanceTestextendsAssert {
1624

17-
privatefinalEtherScanApiapi =newEtherScanApi();
25+
privateEtherScanApiapi;
26+
privateStringaddressCorrect;
27+
privateStringaddressInvalid;
28+
privateStringaddressNoResponse;
29+
30+
publicAccountBalanceTest(EtherScanApiapi,StringaddressCorrect,StringaddressInvalid,StringaddressNoResponse) {
31+
this.api =api;
32+
this.addressCorrect =addressCorrect;
33+
this.addressInvalid =addressInvalid;
34+
this.addressNoResponse =addressNoResponse;
35+
}
36+
37+
@Parameters
38+
publicstaticCollectiondata() {
39+
returnArrays.asList(newObject[][]{
40+
{
41+
newEtherScanApi(),
42+
"0x8d4426f94e42f721C7116E81d6688cd935cB3b4F",
43+
"8d4426f94e42f721C7116E81d6688cd935cB3b4F",
44+
"0x1d4426f94e42f721C7116E81d6688cd935cB3b4F"
45+
},
46+
{
47+
newEtherScanApi(EthNetwork.ROPSTEN),
48+
"0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a",
49+
"xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a",
50+
"0x1dbd2b932c763ba5b1b7ae3b362eac3e8d40121a"
51+
},
52+
{
53+
newEtherScanApi(EthNetwork.RINKEBY),
54+
"0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a",
55+
"xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a",
56+
"0x1dbd2b932c763ba5b1b7ae3b362eac3e8d40121a"
57+
},
58+
{
59+
newEtherScanApi(EthNetwork.KOVAN),
60+
"0xB9F36EE9df7E2A24B61b1738F4127BFDe8bA1A87",
61+
"xB9F36EE9df7E2A24B61b1738F4127BFDe8bA1A87",
62+
"0xB1F36EE9df7E2A24B61b1738F4127BFDe8bA1A87"
63+
},
64+
});
65+
}
1866

1967
@Test
2068
publicvoidcorrect() {
21-
Balancebalance =api.account().balance("0x8d4426f94e42f721C7116E81d6688cd935cB3b4F");
69+
Balancebalance =api.account().balance(addressCorrect);
2270
assertNotNull(balance);
2371
assertNotNull(balance.getWei());
2472
assertNotNull(balance.getMwei());
@@ -31,12 +79,12 @@ public void correct() {
3179

3280
@Test(expected =InvalidAddressException.class)
3381
publicvoidinvalidParamWithError() {
34-
Balancebalance =api.account().balance("8d4426f94e42f721C7116E81d6688cd935cB3b4F");
82+
Balancebalance =api.account().balance(addressInvalid);
3583
}
3684

3785
@Test
3886
publicvoidcorrectParamWithEmptyExpectedResult() {
39-
Balancebalance =api.account().balance("0x8d4426f94e42f722C7116E81d6688cd935cB3b4F");
87+
Balancebalance =api.account().balance(addressNoResponse);
4088
assertNotNull(balance);
4189
assertNotNull(balance.getWei());
4290
assertNotNull(balance.getAddress());

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp