Skip to content

[pull] master from GoodforGod:master #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
.idea/
# Compiled class files
*.class

# Log file
*.log
**/.log

# IntelliJ
*.iml
/.idea

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# other
/bin/
/.classpath
/.project
/target/
/out/
/.DS_Store
/.settings/

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# java-etherscan-api

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

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

Expand Down
19 changes: 18 additions & 1 deletion src/main/java/io/api/etherscan/core/impl/BasicProvider.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package io.api.etherscan.core.impl;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import io.api.etherscan.error.EtherScanException;
import io.api.etherscan.error.ParseException;
import io.api.etherscan.error.RateLimitException;
import io.api.etherscan.executor.IHttpExecutor;
import io.api.etherscan.manager.IQueueManager;
import io.api.etherscan.util.BasicUtils;

import java.util.Map;

/**
* Base provider for API Implementations
*
Expand Down Expand Up @@ -42,7 +46,20 @@ <T> T convert(final String json, final Class<T> tClass) {
try {
return gson.fromJson(json, tClass);
} catch (Exception e) {
throw new ParseException(e.getMessage(), e.getCause());
if (e instanceof JsonSyntaxException) {
Map<String, Object> map = gson.fromJson(json, Map.class);
Object statusCode = map.get("status");
if ((statusCode instanceof String) && (statusCode.equals("0"))) {
Object message = map.get("message");
if ((message instanceof String) && (message.equals("NOTOK"))) {
Object result = map.get("result");
if ((result instanceof String) && (result.equals("Max rate limit reached"))) {
throw new RateLimitException ("Max rate limit reached");
}
}
}
}
throw new ParseException(e.getMessage(), e.getCause(), json);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/io/api/etherscan/error/ConnectionException.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
*/
public class ConnectionException extends ApiException {

public ConnectionException(String message) {
super(message);
}

public ConnectionException(String message, Throwable cause) {
super(message, cause);
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/io/api/etherscan/error/ParseException.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
* @since 29.10.2018
*/
public class ParseException extends ApiException {
String json;

public ParseException(String message, Throwable cause) {
public ParseException(String message, Throwable cause, String json) {
super(message, cause);
this.json = json;
}

public String getJson() {
return json;
}
}
15 changes: 15 additions & 0 deletions src/main/java/io/api/etherscan/error/RateLimitException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.api.etherscan.error;

/**
* ! NO DESCRIPTION !
*
* @author iSnow
* @since 2020-10-06
*/
public class RateLimitException extends ApiException {

public RateLimitException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;

import static java.net.HttpURLConnection.HTTP_MOVED_PERM;
import static java.net.HttpURLConnection.HTTP_MOVED_TEMP;
import static java.net.HttpURLConnection.*;

/**
* Http client implementation
Expand Down Expand Up @@ -88,6 +87,10 @@ public String get(final String urlAsString) {
final int status = connection.getResponseCode();
if (status == HTTP_MOVED_TEMP || status == HTTP_MOVED_PERM) {
return get(connection.getHeaderField("Location"));
} else if ((status >= HTTP_BAD_REQUEST) && (status < HTTP_INTERNAL_ERROR)) {
throw new ConnectionException("Protocol error: "+connection.getResponseMessage());
} else if (status >= HTTP_INTERNAL_ERROR) {
throw new ConnectionException("Server error: "+connection.getResponseMessage());
}

final String data = readData(connection);
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/io/api/etherscan/model/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,27 @@ public LocalDateTime getTimeStamp() {
if(_timeStamp == null && !BasicUtils.isEmpty(timeStamp)) {
long formatted = (timeStamp.charAt(0) == '0' && timeStamp.charAt(1) == 'x')
? BasicUtils.parseHex(timeStamp).longValue()
: Long.valueOf(timeStamp);
: Long.parseLong(timeStamp);
_timeStamp = LocalDateTime.ofEpochSecond(formatted, 0, ZoneOffset.UTC);
}
return _timeStamp;
}

/**
* Return the "timeStamp" field of the event record as a long-int representing the milliseconds
* since the Unix epoch (1970-01-01 00:00:00).
* @return milliseconds between Unix epoch and `timeStamp`. If field is empty or null, returns null
*/
public Long getTimeStampAsMillis() {
if (BasicUtils.isEmpty(timeStamp)) {
return null;
}
long tsSecs = (timeStamp.charAt(0) == '0' && timeStamp.charAt(1) == 'x')
? BasicUtils.parseHex(timeStamp).longValue()
: Long.parseLong(timeStamp);
return tsSecs * 1000;
}

public String getData() {
return data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.api.etherscan.core.ILogsApi;
import io.api.etherscan.error.LogQueryException;
import io.api.etherscan.model.query.IQueryBuilder;
import io.api.etherscan.util.BasicUtils;

/**
Expand All @@ -12,7 +13,7 @@
* @author GoodforGod
* @since 31.10.2018
*/
public class LogQueryBuilder {
public class LogQueryBuilder implements IQueryBuilder {

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

return new LogTopicQuadro(address, startBlock, endBlock, topic0, topic1, topic2, topic3);
}

@Override
public LogQuery build() throws LogQueryException {
return new LogQuery("&address=" + this.address + "&fromBlock=" + this.startBlock + "&toBlock=" + this.endBlock);
}
}
2 changes: 1 addition & 1 deletion src/test/java/io/api/util/BasicUtilsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ public void isResponseNullThrows() {

@Test(expected = ParseException.class)
public void isThrowParseException() {
throw new ParseException("Test", null);
throw new ParseException("Test", null, null);
}
}