Skip to content
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

Issue #52: #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 11 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,31 @@
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
<version>2.11.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
<version>4.12.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>32.1.2-jre</version>
<version>33.3.0-jre</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.2.0</version>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -108,7 +108,7 @@
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.13</version>
<version>1.7.0</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
Expand All @@ -119,15 +119,15 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<version>3.13.0</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<version>3.3.1</version>
<executions>
<execution>
<id>attach-sources</id>
Expand All @@ -141,7 +141,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.5.0</version>
<version>3.10.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand All @@ -154,15 +154,15 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>3.0.1</version>
<version>3.1.0</version>
<configuration>
<goals>deploy</goals>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.1.0</version>
<version>3.2.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
Expand Down
71 changes: 34 additions & 37 deletions src/main/java/io/ipinfo/api/IPinfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;

public class IPinfo {
private static final int batchMaxSize = 1000;
Expand All @@ -35,6 +34,7 @@ public class IPinfo {
.setTimeoutPerBatch(batchReqTimeoutDefault)
.build();
private final static Gson gson = new Gson();
private static final MediaType JSON = MediaType.parse("application/json");

private final OkHttpClient client;
private final Context context;
Expand Down Expand Up @@ -215,7 +215,7 @@ private ConcurrentHashMap<String, Object> getBatchGeneric(
}

// everything cached; exit early.
if (lookupUrls.size() == 0) {
if (lookupUrls.isEmpty()) {
return result;
}

Expand All @@ -234,7 +234,7 @@ private ConcurrentHashMap<String, Object> getBatchGeneric(
}

// prep URL we'll target.
// add `filter=1` as qparam for filtering out empty results on server.
// add `filter=1` as param for filtering out empty results on server.
String postUrl;
if (opts.filter) {
postUrl = "https://ipinfo.io/batch?filter=1";
Expand All @@ -243,7 +243,7 @@ private ConcurrentHashMap<String, Object> getBatchGeneric(
}

// prepare latch & common request.
// each request, when complete, will countdown the latch.
// each request, when complete, will count down the latch.
CountDownLatch latch = new CountDownLatch((int)Math.ceil(lookupUrls.size()/1000.0));
Request.Builder reqCommon = new Request.Builder()
.url(postUrl)
Expand All @@ -261,7 +261,7 @@ private ConcurrentHashMap<String, Object> getBatchGeneric(

// prepare & queue up request.
String urlListJson = gson.toJson(urlsChunk);
RequestBody requestBody = RequestBody.create(null, urlListJson);
RequestBody requestBody = RequestBody.create(urlListJson, JSON);
Request req = reqCommon.post(requestBody).build();
OkHttpClient chunkClient = client.newBuilder()
.connectTimeout(timeoutPerBatch, TimeUnit.SECONDS)
Expand All @@ -284,22 +284,19 @@ public void onResponse(Call call, Response response) throws IOException {
Type respType = new TypeToken<HashMap<String, Object>>() {}.getType();
HashMap<String, Object> localResult
= gson.fromJson(response.body().string(), respType);
localResult.forEach(new BiConsumer<String, Object>() {
@Override
public void accept(String k, Object v) {
if (k.startsWith("AS")) {
String vStr = gson.toJson(v);
ASNResponse vCasted = gson.fromJson(vStr, ASNResponse.class);
vCasted.setContext(context);
result.put(k, vCasted);
} else if (InetAddresses.isInetAddress(k)) {
String vStr = gson.toJson(v);
IPResponse vCasted = gson.fromJson(vStr, IPResponse.class);
vCasted.setContext(context);
result.put(k, vCasted);
} else {
result.put(k, v);
}
localResult.forEach((key, value) -> {
if (key.startsWith("AS")) {
String vStr = gson.toJson(value);
ASNResponse vCasted = gson.fromJson(vStr, ASNResponse.class);
vCasted.setContext(context);
result.put(key, vCasted);
} else if (InetAddresses.isInetAddress(key)) {
String vStr = gson.toJson(value);
IPResponse vCasted = gson.fromJson(vStr, IPResponse.class);
vCasted.setContext(context);
result.put(key, vCasted);
} else {
result.put(key, value);
}
});

Expand All @@ -315,15 +312,15 @@ public void accept(String k, Object v) {
} else {
boolean success = latch.await(opts.timeoutTotal, TimeUnit.SECONDS);
if (!success) {
if (result.size() == 0) {
if (result.isEmpty()) {
return null;
} else {
return result;
}
}
}
} catch (InterruptedException e) {
if (result.size() == 0) {
if (result.isEmpty()) {
return null;
} else {
return result;
Expand Down Expand Up @@ -403,14 +400,14 @@ public static class Builder {
private boolean filter = false;

/**
* batchSize is the internal batch size used per API request; the IPinfo
* <p>batchSize is the internal batch size used per API request; the IPinfo
* API has a maximum batch size, but the batch request functions available
* in this library do not. Therefore the library chunks the input slices
* in this library do not. Therefore, the library chunks the input slices
* internally into chunks of size `batchSize`, clipping to the maximum
* allowed by the IPinfo API.
* allowed by the IPinfo API.</p>
*
* 0 means to use the default batch size which is the max allowed by the
* IPinfo API.
* <p>0 means to use the default batch size which is the max allowed by the
* IPinfo API.</p>
*
* @param batchSize see description.
* @return the builder.
Expand All @@ -421,11 +418,11 @@ public Builder setBatchSize(int batchSize) {
}

/**
* timeoutPerBatch is the timeout in seconds that each batch of size
* `BatchSize` will have for its own request.
* <p>timeoutPerBatch is the timeout in seconds that each batch of size
* `BatchSize` will have for its own request.</p>
*
* 0 means to use a default of 5 seconds; any negative number will turn it
* off; turning it off does _not_ disable the effects of `timeoutTotal`.
* <p>0 means to use a default of 5 seconds; any negative number will turn it
* off; turning it off does _not_ disable the effects of `timeoutTotal`.</p>
*
* @param timeoutPerBatch see description.
* @return the builder.
Expand All @@ -436,10 +433,10 @@ public Builder setTimeoutPerBatch(int timeoutPerBatch) {
}

/**
* timeoutTotal is the total timeout in seconds for all batch requests in a
* batch request function to complete.
* <p>timeoutTotal is the total timeout in seconds for all batch requests in a
* batch request function to complete.</p>
*
* 0 means no total timeout; `timeoutPerBatch` will still apply.
* <p>0 means no total timeout; `timeoutPerBatch` will still apply.</p>
*
* @param timeoutTotal see description.
* @return the builder.
Expand All @@ -450,8 +447,8 @@ public Builder setTimeoutTotal(int timeoutTotal) {
}

/**
* filter, if turned on, will filter out a URL whose value was deemed empty
* on the server.
* <p>filter, if turned on, will filter out a URL whose value was deemed empty
* on the server.</p>
*
* @param filter see description.
* @return the builder.
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/io/ipinfo/api/request/BaseRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.ipinfo.api.errors.ErrorResponseException;
import io.ipinfo.api.errors.RateLimitedException;
import okhttp3.Credentials;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
Expand All @@ -12,6 +13,7 @@ public abstract class BaseRequest<T> {
protected final static Gson gson = new Gson();
private final OkHttpClient client;
private final String token;
protected static final MediaType JSON = MediaType.parse("application/json");

protected BaseRequest(OkHttpClient client, String token) {
this.client = client;
Expand All @@ -34,11 +36,6 @@ public Response handleRequest(Request.Builder request) throws RateLimitedExcepti
throw new ErrorResponseException(e);
}

// Sanity check
if (response == null) {
return null;
}

if (response.code() == 429) {
throw new RateLimitedException();
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/io/ipinfo/api/request/IPRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public IPResponse handle() throws RateLimitedException {
}
}

static IpAddressMatcher[] IpAddressMatcherList = {
private static final IpAddressMatcher[] IpAddressMatcherList = {
// IPv4
new IpAddressMatcher("0.0.0.0/8"),
new IpAddressMatcher("10.0.0.0/8"),
Expand Down Expand Up @@ -106,8 +106,7 @@ public IPResponse handle() throws RateLimitedException {
};

static boolean isBogon(String ip) {
for (int i = 0; i < IpAddressMatcherList.length; i++) {
IpAddressMatcher ipAddressMatcher = IpAddressMatcherList[i];
for (IpAddressMatcher ipAddressMatcher : IpAddressMatcherList) {
if (ipAddressMatcher.matches(ip)) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/ipinfo/api/request/MapRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public MapRequest(OkHttpClient client, String token, List<String> ips) {
@Override
public MapResponse handle() throws RateLimitedException {
String jsonIpList = gson.toJson(ips);
RequestBody requestBody = RequestBody.create(null, jsonIpList);
RequestBody requestBody = RequestBody.create(jsonIpList, JSON);
Request.Builder request = new Request.Builder().url(URL).post(requestBody);

try (Response response = handleRequest(request)) {
Expand Down
Loading