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

Commit cd06811

Browse files
Merge pull request #6 from iSnow/master
Better connection exception reporting and small changes
2 parents 9ce8f07 + f7bb00c commit cd06811

File tree

10 files changed

+106
-9
lines changed

10 files changed

+106
-9
lines changed

‎.gitignore‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,32 @@
1-
.idea/
1+
# Compiled class files
2+
*.class
3+
4+
# Log file
5+
*.log
6+
**/.log
7+
8+
# IntelliJ
29
*.iml
10+
/.idea
11+
12+
# Package Files #
13+
*.jar
14+
*.war
15+
*.nar
16+
*.ear
17+
*.zip
18+
*.tar.gz
19+
*.rar
20+
21+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
22+
hs_err_pid*
23+
24+
# other
25+
/bin/
26+
/.classpath
27+
/.project
28+
/target/
29+
/out/
30+
/.DS_Store
31+
/.settings/
32+

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# java-etherscan-api
22

3-
![travis](https://travis-ci.org/GoodforGod/java-etherscan-api.svg?branch=master)
3+
[![travis](https://travis-ci.org/GoodforGod/java-etherscan-api.svg?branch=master)](https://travis-ci.com/iSnow/java-etherscan-api)
44
[![Maintainability](https://api.codeclimate.com/v1/badges/808997be2e69ff1ae8fe/maintainability)](https://codeclimate.com/github/GoodforGod/java-etherscan-api/maintainability)
55
[![codecov](https://codecov.io/gh/GoodforGod/java-etherscan-api/branch/master/graph/badge.svg)](https://codecov.io/gh/GoodforGod/java-etherscan-api)
6+
[![Jitpack](https://jitpack.io/v/iSnow/java-etherscan-api.svg)](https://jitpack.io/#iSnow/java-etherscan-api)
67

78
[Etherscan](https://etherscan.io/apis) Java API implementation.
89

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package io.api.etherscan.core.impl;
22

33
import com.google.gson.Gson;
4+
import com.google.gson.JsonSyntaxException;
45
import io.api.etherscan.error.EtherScanException;
56
import io.api.etherscan.error.ParseException;
7+
import io.api.etherscan.error.RateLimitException;
68
import io.api.etherscan.executor.IHttpExecutor;
79
import io.api.etherscan.manager.IQueueManager;
810
import io.api.etherscan.util.BasicUtils;
911

12+
import java.util.Map;
13+
1014
/**
1115
* Base provider for API Implementations
1216
*
@@ -42,7 +46,20 @@ <T> T convert(final String json, final Class<T> tClass) {
4246
try {
4347
return gson.fromJson(json, tClass);
4448
} catch (Exception e) {
45-
throw new ParseException(e.getMessage(), e.getCause());
49+
if (e instanceof JsonSyntaxException) {
50+
Map<String, Object> map = gson.fromJson(json, Map.class);
51+
Object statusCode = map.get("status");
52+
if ((statusCode instanceof String) && (statusCode.equals("0"))) {
53+
Object message = map.get("message");
54+
if ((message instanceof String) && (message.equals("NOTOK"))) {
55+
Object result = map.get("result");
56+
if ((result instanceof String) && (result.equals("Max rate limit reached"))) {
57+
throw new RateLimitException ("Max rate limit reached");
58+
}
59+
}
60+
}
61+
}
62+
throw new ParseException(e.getMessage(), e.getCause(), json);
4663
}
4764
}
4865

‎src/main/java/io/api/etherscan/error/ConnectionException.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
*/
99
public class ConnectionException extends ApiException {
1010

11+
public ConnectionException(String message) {
12+
super(message);
13+
}
14+
1115
public ConnectionException(String message, Throwable cause) {
1216
super(message, cause);
1317
}

‎src/main/java/io/api/etherscan/error/ParseException.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77
* @since 29.10.2018
88
*/
99
public class ParseException extends ApiException {
10+
String json;
1011

11-
public ParseException(String message, Throwable cause) {
12+
public ParseException(String message, Throwable cause, Stringjson) {
1213
super(message, cause);
14+
this.json = json;
15+
}
16+
17+
public String getJson() {
18+
return json;
1319
}
1420
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.api.etherscan.error;
2+
3+
/**
4+
* ! NO DESCRIPTION !
5+
*
6+
* @author iSnow
7+
* @since 2020年10月06日
8+
*/
9+
public class RateLimitException extends ApiException {
10+
11+
public RateLimitException(String message) {
12+
super(message);
13+
}
14+
15+
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
import java.util.zip.GZIPInputStream;
1919
import java.util.zip.InflaterInputStream;
2020

21-
import static java.net.HttpURLConnection.HTTP_MOVED_PERM;
22-
import static java.net.HttpURLConnection.HTTP_MOVED_TEMP;
21+
import static java.net.HttpURLConnection.*;
2322

2423
/**
2524
* Http client implementation
@@ -88,6 +87,10 @@ public String get(final String urlAsString) {
8887
final int status = connection.getResponseCode();
8988
if (status == HTTP_MOVED_TEMP || status == HTTP_MOVED_PERM) {
9089
return get(connection.getHeaderField("Location"));
90+
} else if ((status >= HTTP_BAD_REQUEST) && (status < HTTP_INTERNAL_ERROR)) {
91+
throw new ConnectionException("Protocol error: "+connection.getResponseMessage());
92+
} else if (status >= HTTP_INTERNAL_ERROR) {
93+
throw new ConnectionException("Server error: "+connection.getResponseMessage());
9194
}
9295

9396
final String data = readData(connection);

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,27 @@ public LocalDateTime getTimeStamp() {
6060
if(_timeStamp == null && !BasicUtils.isEmpty(timeStamp)) {
6161
long formatted = (timeStamp.charAt(0) == '0' && timeStamp.charAt(1) == 'x')
6262
? BasicUtils.parseHex(timeStamp).longValue()
63-
: Long.valueOf(timeStamp);
63+
: Long.parseLong(timeStamp);
6464
_timeStamp = LocalDateTime.ofEpochSecond(formatted, 0, ZoneOffset.UTC);
6565
}
6666
return _timeStamp;
6767
}
6868

69+
/**
70+
* Return the "timeStamp" field of the event record as a long-int representing the milliseconds
71+
* since the Unix epoch (1970年01月01日 00:00:00).
72+
* @return milliseconds between Unix epoch and `timeStamp`. If field is empty or null, returns null
73+
*/
74+
public Long getTimeStampAsMillis() {
75+
if (BasicUtils.isEmpty(timeStamp)) {
76+
return null;
77+
}
78+
long tsSecs = (timeStamp.charAt(0) == '0' && timeStamp.charAt(1) == 'x')
79+
? BasicUtils.parseHex(timeStamp).longValue()
80+
: Long.parseLong(timeStamp);
81+
return tsSecs * 1000;
82+
}
83+
6984
public String getData() {
7085
return data;
7186
}

‎src/main/java/io/api/etherscan/model/query/impl/LogQueryBuilder.java‎

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

33
import io.api.etherscan.core.ILogsApi;
44
import io.api.etherscan.error.LogQueryException;
5+
import io.api.etherscan.model.query.IQueryBuilder;
56
import io.api.etherscan.util.BasicUtils;
67

78
/**
@@ -12,7 +13,7 @@
1213
* @author GoodforGod
1314
* @since 31.10.2018
1415
*/
15-
public class LogQueryBuilder {
16+
public class LogQueryBuilder implementsIQueryBuilder{
1617

1718
private static final long MIN_BLOCK = 0;
1819
private static final long MAX_BLOCK = 99999999999L;
@@ -75,4 +76,9 @@ public LogTopicQuadro topic(String topic0, String topic1, String topic2, String
7576

7677
return new LogTopicQuadro(address, startBlock, endBlock, topic0, topic1, topic2, topic3);
7778
}
79+
80+
@Override
81+
public LogQuery build() throws LogQueryException {
82+
return new LogQuery("&address=" + this.address + "&fromBlock=" + this.startBlock + "&toBlock=" + this.endBlock);
83+
}
7884
}

‎src/test/java/io/api/util/BasicUtilsTests.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,6 @@ public void isResponseNullThrows() {
9898

9999
@Test(expected = ParseException.class)
100100
public void isThrowParseException() {
101-
throw new ParseException("Test", null);
101+
throw new ParseException("Test", null, null);
102102
}
103103
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /