Skip to content

Commit 3a82e62

Browse files
silvanocerzamax-ipinfo
authored andcommitted
Add support for Lite API
1 parent fdba6f4 commit 3a82e62

File tree

5 files changed

+632
-1
lines changed

5 files changed

+632
-1
lines changed

README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ The free plan is limited to 50,000 requests per month, and doesn't include some
1919

2020
[Click here to view the Java SDK's API documentation](https://ipinfo.github.io/java/).
2121

22-
⚠️ Note: This library does not currently support our newest free API https://ipinfo.io/lite. If you’d like to use IPinfo Lite, you can call the [endpoint directly](https://ipinfo.io/developers/lite-api) using your preferred HTTP client. Developers are also welcome to contribute support for Lite by submitting a pull request.
22+
The library also supports the Lite API, see the [Lite API section](#lite-api) for more info.
2323

2424
#### Installation
2525

@@ -100,6 +100,41 @@ public class Main {
100100
recover from this exception.
101101
- `RateLimitedException` An exception signaling that you've been rate limited.
102102

103+
##### Lite API
104+
105+
The library gives the possibility to use the [Lite API](https://ipinfo.io/developers/lite-api) too, authentication with your token is still required.
106+
107+
The returned details are slightly different from the Core API.
108+
109+
```java
110+
import io.ipinfo.api.IPinfoLite;
111+
import io.ipinfo.api.errors.RateLimitedException;
112+
import io.ipinfo.api.model.IPResponseLite;
113+
114+
public class Main {
115+
public static void main(String... args) {
116+
IPinfoLite ipInfoLite = new IPinfoLite.Builder()
117+
.setToken("YOUR TOKEN")
118+
.build();
119+
120+
try {
121+
IPResponseLite response = ipInfoLite.lookupIP("8.8.8.8");
122+
123+
// Print out the ASN
124+
System.out.println(response.getAsn()); // AS15169
125+
126+
// Print out the country code
127+
System.out.println(response.getCountryCode()); // US
128+
129+
// Print out the country name
130+
System.out.println(response.getCountry()); // United States
131+
} catch (RateLimitedException ex) {
132+
// Handle rate limits here.
133+
}
134+
}
135+
}
136+
```
137+
103138
#### Caching
104139

105140
This library provides a very simple caching system accessible in `SimpleCache`.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package io.ipinfo.api;
2+
3+
import com.google.common.net.InetAddresses;
4+
import io.ipinfo.api.cache.Cache;
5+
import io.ipinfo.api.cache.SimpleCache;
6+
import io.ipinfo.api.context.Context;
7+
import io.ipinfo.api.errors.RateLimitedException;
8+
import io.ipinfo.api.model.IPResponseLite;
9+
import io.ipinfo.api.request.IPRequestLite;
10+
import java.io.IOException;
11+
import java.lang.reflect.Type;
12+
import java.time.Duration;
13+
import java.util.ArrayList;
14+
import java.util.HashMap;
15+
import java.util.List;
16+
import java.util.concurrent.ConcurrentHashMap;
17+
import java.util.concurrent.CountDownLatch;
18+
import java.util.concurrent.TimeUnit;
19+
import java.util.function.BiConsumer;
20+
import javax.annotation.ParametersAreNonnullByDefault;
21+
import okhttp3.*;
22+
23+
public class IPinfoLite {
24+
25+
private final OkHttpClient client;
26+
private final Context context;
27+
private final String token;
28+
private final Cache cache;
29+
30+
IPinfoLite(
31+
OkHttpClient client,
32+
Context context,
33+
String token,
34+
Cache cache
35+
) {
36+
this.client = client;
37+
this.context = context;
38+
this.token = token;
39+
this.cache = cache;
40+
}
41+
42+
public static void main(String[] args) throws RateLimitedException {
43+
System.out.println("Running IPinfo Lite client");
44+
}
45+
46+
/**
47+
* Lookup IP information using the IP. This is a blocking call.
48+
*
49+
* @param ip IP address to query information for.
50+
* @return Response containing IP information.
51+
* @throws RateLimitedException if the user has exceeded the rate limit.
52+
*/
53+
public IPResponseLite lookupIP(String ip) throws RateLimitedException {
54+
IPRequestLite request = new IPRequestLite(client, token, ip);
55+
IPResponseLite response = request.handle();
56+
57+
if (response != null) {
58+
response.setContext(context);
59+
if (!response.getBogon()) {
60+
cache.set(cacheKey(ip), response);
61+
}
62+
}
63+
64+
return response;
65+
}
66+
67+
public static String cacheKey(String k) {
68+
return "lite_" + k;
69+
}
70+
71+
public static class Builder {
72+
73+
private OkHttpClient client;
74+
private String token;
75+
private Cache cache;
76+
77+
public Builder setClient(OkHttpClient client) {
78+
this.client = client;
79+
return this;
80+
}
81+
82+
public Builder setToken(String token) {
83+
this.token = token;
84+
return this;
85+
}
86+
87+
public Builder setCache(Cache cache) {
88+
this.cache = cache;
89+
return this;
90+
}
91+
92+
public IPinfoLite build() {
93+
if (client == null) {
94+
client = new OkHttpClient();
95+
}
96+
if (cache == null) {
97+
cache = new SimpleCache(Duration.ofDays(1));
98+
}
99+
return new IPinfoLite(client, new Context(), token, cache);
100+
}
101+
}
102+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package io.ipinfo.api.model;
2+
3+
import io.ipinfo.api.context.Context;
4+
5+
public class IPResponseLite {
6+
private final String ip;
7+
private final String asn;
8+
private final String as_name;
9+
private final String as_domain;
10+
private final String country_code;
11+
private final String country;
12+
private final String continent_code;
13+
private final String continent;
14+
private final boolean bogon;
15+
private transient Context context;
16+
17+
public IPResponseLite(
18+
String ip,
19+
String asn,
20+
String as_name,
21+
String as_domain,
22+
String country_code,
23+
String country,
24+
String continent_code,
25+
String continent
26+
) {
27+
this.ip = ip;
28+
this.asn = asn;
29+
this.as_name = as_name;
30+
this.as_domain = as_domain;
31+
this.country_code = country_code;
32+
this.country = country;
33+
this.continent_code = continent_code;
34+
this.continent = continent;
35+
this.bogon = false;
36+
}
37+
38+
public IPResponseLite(String ip, boolean bogon) {
39+
this.ip = ip;
40+
this.bogon = bogon;
41+
this.asn = null;
42+
this.as_name = null;
43+
this.as_domain = null;
44+
this.country_code = null;
45+
this.country = null;
46+
this.continent_code = null;
47+
this.continent = null;
48+
}
49+
50+
/**
51+
* Set by the library for extra utility functions
52+
*
53+
* @param context for country information
54+
*/
55+
public void setContext(Context context) {
56+
this.context = context;
57+
}
58+
59+
public String getIp() {
60+
return ip;
61+
}
62+
63+
public String getAsn() {
64+
return asn;
65+
}
66+
67+
public String getAsName() {
68+
return as_name;
69+
}
70+
71+
public String getAsDomain() {
72+
return as_domain;
73+
}
74+
75+
public String getCountryCode() {
76+
return country_code;
77+
}
78+
79+
public String getCountry() {
80+
return country;
81+
}
82+
83+
public String getCountryName() {
84+
return context != null ? context.getCountryName(getCountryCode()) : country;
85+
}
86+
87+
public String getContinentCode() {
88+
return continent_code;
89+
}
90+
91+
public String getContinent() {
92+
return continent;
93+
}
94+
95+
public boolean getBogon() {
96+
return bogon;
97+
}
98+
99+
public Boolean isEU() {
100+
return context != null ? context.isEU(getCountryCode()) : null;
101+
}
102+
103+
public CountryFlag getCountryFlag() {
104+
return context != null ? context.getCountryFlag(getCountryCode()) : null;
105+
}
106+
107+
public String getCountryFlagURL() {
108+
return context != null ? context.getCountryFlagURL(getCountryCode()) : null;
109+
}
110+
111+
public CountryCurrency getCountryCurrency() {
112+
return context != null ? context.getCountryCurrency(getCountryCode()) : null;
113+
}
114+
115+
public Continent getContinentInfo() {
116+
return context != null ? context.getContinent(getCountryCode()) : null;
117+
}
118+
119+
@Override
120+
public String toString() {
121+
return (bogon ?
122+
"IPResponseLite{" +
123+
"ip='" + ip + '\'' +
124+
", bogon=" + bogon +
125+
"}"
126+
:
127+
"IPResponseLite{" +
128+
"ip='" + ip + '\'' +
129+
", asn='" + asn + '\'' +
130+
", as_name='" + as_name + '\'' +
131+
", as_domain='" + as_domain + '\'' +
132+
", country_code='" + country_code + '\'' +
133+
", country='" + country + '\'' +
134+
", continent_code='" + continent_code + '\'' +
135+
", continent='" + continent + '\'' +
136+
'}');
137+
}
138+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.ipinfo.api.request;
2+
3+
import io.ipinfo.api.errors.ErrorResponseException;
4+
import io.ipinfo.api.errors.RateLimitedException;
5+
import io.ipinfo.api.model.IPResponseLite;
6+
import okhttp3.OkHttpClient;
7+
import okhttp3.Request;
8+
import okhttp3.Response;
9+
10+
public class IPRequestLite extends BaseRequest<IPResponseLite> {
11+
private final static String URL_FORMAT = "https://api.ipinfo.io/lite/%s";
12+
private final String ip;
13+
14+
public IPRequestLite(OkHttpClient client, String token, String ip) {
15+
super(client, token);
16+
this.ip = ip;
17+
}
18+
19+
@Override
20+
public IPResponseLite handle() throws RateLimitedException {
21+
if (IPRequest.isBogon(ip)) {
22+
try {
23+
return new IPResponseLite(ip, true);
24+
} catch (Exception ex) {
25+
throw new ErrorResponseException(ex);
26+
}
27+
}
28+
29+
String url = String.format(URL_FORMAT, ip);
30+
Request.Builder request = new Request.Builder().url(url).get();
31+
32+
try (Response response = handleRequest(request)) {
33+
if (response == null || response.body() == null) {
34+
return null;
35+
}
36+
37+
try {
38+
return gson.fromJson(response.body().string(), IPResponseLite.class);
39+
} catch (Exception ex) {
40+
throw new ErrorResponseException(ex);
41+
}
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)