Skip to content

Commit dd53a86

Browse files
authored
Merge pull request #60 from ipinfo/silvano/eng-490-add-core-bundle-support-in-ipinfojava-library
Add support for Core bundle
2 parents 6053f0b + 3891734 commit dd53a86

File tree

5 files changed

+501
-0
lines changed

5 files changed

+501
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package io.ipinfo.api;
2+
3+
import io.ipinfo.api.cache.Cache;
4+
import io.ipinfo.api.cache.SimpleCache;
5+
import io.ipinfo.api.context.Context;
6+
import io.ipinfo.api.errors.RateLimitedException;
7+
import io.ipinfo.api.model.IPResponseCore;
8+
import io.ipinfo.api.request.IPRequestCore;
9+
import java.time.Duration;
10+
import okhttp3.OkHttpClient;
11+
12+
public class IPinfoCore {
13+
14+
private final OkHttpClient client;
15+
private final Context context;
16+
private final String token;
17+
private final Cache cache;
18+
19+
IPinfoCore(
20+
OkHttpClient client,
21+
Context context,
22+
String token,
23+
Cache cache
24+
) {
25+
this.client = client;
26+
this.context = context;
27+
this.token = token;
28+
this.cache = cache;
29+
}
30+
31+
public static void main(String[] args) throws RateLimitedException {
32+
System.out.println("Running IPinfo Core client");
33+
}
34+
35+
/**
36+
* Lookup IP information using the IP. This is a blocking call.
37+
*
38+
* @param ip IP address to query information for.
39+
* @return Response containing IP information.
40+
* @throws RateLimitedException if the user has exceeded the rate limit.
41+
*/
42+
public IPResponseCore lookupIP(String ip) throws RateLimitedException {
43+
IPRequestCore request = new IPRequestCore(client, token, ip);
44+
IPResponseCore response = request.handle();
45+
46+
if (response != null) {
47+
response.setContext(context);
48+
if (!response.getBogon()) {
49+
cache.set(cacheKey(ip), response);
50+
}
51+
}
52+
53+
return response;
54+
}
55+
56+
public static String cacheKey(String k) {
57+
return "core_" + k;
58+
}
59+
60+
public static class Builder {
61+
62+
private OkHttpClient client;
63+
private String token;
64+
private Cache cache;
65+
66+
public Builder setClient(OkHttpClient client) {
67+
this.client = client;
68+
return this;
69+
}
70+
71+
public Builder setToken(String token) {
72+
this.token = token;
73+
return this;
74+
}
75+
76+
public Builder setCache(Cache cache) {
77+
this.cache = cache;
78+
return this;
79+
}
80+
81+
public IPinfoCore build() {
82+
if (client == null) {
83+
client = new OkHttpClient();
84+
}
85+
if (cache == null) {
86+
cache = new SimpleCache(Duration.ofDays(1));
87+
}
88+
return new IPinfoCore(client, new Context(), token, cache);
89+
}
90+
}
91+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package io.ipinfo.api.model;
2+
3+
public class Geo {
4+
private final String city;
5+
private final String region;
6+
private final String region_code;
7+
private final String country;
8+
private final String country_code;
9+
private final String continent;
10+
private final String continent_code;
11+
private final Double latitude;
12+
private final Double longitude;
13+
private final String timezone;
14+
private final String postal_code;
15+
16+
public Geo(
17+
String city,
18+
String region,
19+
String region_code,
20+
String country,
21+
String country_code,
22+
String continent,
23+
String continent_code,
24+
Double latitude,
25+
Double longitude,
26+
String timezone,
27+
String postal_code
28+
) {
29+
this.city = city;
30+
this.region = region;
31+
this.region_code = region_code;
32+
this.country = country;
33+
this.country_code = country_code;
34+
this.continent = continent;
35+
this.continent_code = continent_code;
36+
this.latitude = latitude;
37+
this.longitude = longitude;
38+
this.timezone = timezone;
39+
this.postal_code = postal_code;
40+
}
41+
42+
public String getCity() {
43+
return city;
44+
}
45+
46+
public String getRegion() {
47+
return region;
48+
}
49+
50+
public String getRegionCode() {
51+
return region_code;
52+
}
53+
54+
public String getCountry() {
55+
return country;
56+
}
57+
58+
public String getCountryCode() {
59+
return country_code;
60+
}
61+
62+
public String getContinent() {
63+
return continent;
64+
}
65+
66+
public String getContinentCode() {
67+
return continent_code;
68+
}
69+
70+
public Double getLatitude() {
71+
return latitude;
72+
}
73+
74+
public Double getLongitude() {
75+
return longitude;
76+
}
77+
78+
public String getTimezone() {
79+
return timezone;
80+
}
81+
82+
public String getPostalCode() {
83+
return postal_code;
84+
}
85+
86+
@Override
87+
public String toString() {
88+
return "Geo{" +
89+
"city='" + city + '\'' +
90+
", region='" + region + '\'' +
91+
", region_code='" + region_code + '\'' +
92+
", country='" + country + '\'' +
93+
", country_code='" + country_code + '\'' +
94+
", continent='" + continent + '\'' +
95+
", continent_code='" + continent_code + '\'' +
96+
", latitude=" + latitude +
97+
", longitude=" + longitude +
98+
", timezone='" + timezone + '\'' +
99+
", postal_code='" + postal_code + '\'' +
100+
'}';
101+
}
102+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package io.ipinfo.api.model;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import io.ipinfo.api.context.Context;
5+
6+
public class IPResponseCore {
7+
private final String ip;
8+
private final Geo geo;
9+
@SerializedName("as")
10+
private final ASN asn;
11+
private final Boolean is_anonymous;
12+
private final Boolean is_anycast;
13+
private final Boolean is_hosting;
14+
private final Boolean is_mobile;
15+
private final Boolean is_satellite;
16+
private final boolean bogon;
17+
private transient Context context;
18+
19+
public IPResponseCore(
20+
String ip,
21+
Geo geo,
22+
ASN asn,
23+
Boolean is_anonymous,
24+
Boolean is_anycast,
25+
Boolean is_hosting,
26+
Boolean is_mobile,
27+
Boolean is_satellite
28+
) {
29+
this.ip = ip;
30+
this.geo = geo;
31+
this.asn = asn;
32+
this.is_anonymous = is_anonymous;
33+
this.is_anycast = is_anycast;
34+
this.is_hosting = is_hosting;
35+
this.is_mobile = is_mobile;
36+
this.is_satellite = is_satellite;
37+
this.bogon = false;
38+
}
39+
40+
public IPResponseCore(String ip, boolean bogon) {
41+
this.ip = ip;
42+
this.bogon = bogon;
43+
this.geo = null;
44+
this.asn = null;
45+
this.is_anonymous = null;
46+
this.is_anycast = null;
47+
this.is_hosting = null;
48+
this.is_mobile = null;
49+
this.is_satellite = null;
50+
}
51+
52+
/**
53+
* Set by the library for extra utility functions
54+
*
55+
* @param context for country information
56+
*/
57+
public void setContext(Context context) {
58+
this.context = context;
59+
}
60+
61+
public String getIp() {
62+
return ip;
63+
}
64+
65+
public Geo getGeo() {
66+
return geo;
67+
}
68+
69+
public ASN getAsn() {
70+
return asn;
71+
}
72+
73+
public Boolean getIsAnonymous() {
74+
return is_anonymous;
75+
}
76+
77+
public Boolean getIsAnycast() {
78+
return is_anycast;
79+
}
80+
81+
public Boolean getIsHosting() {
82+
return is_hosting;
83+
}
84+
85+
public Boolean getIsMobile() {
86+
return is_mobile;
87+
}
88+
89+
public Boolean getIsSatellite() {
90+
return is_satellite;
91+
}
92+
93+
public boolean getBogon() {
94+
return bogon;
95+
}
96+
97+
public String getCountryName() {
98+
return context != null && geo != null ? context.getCountryName(geo.getCountryCode()) : (geo != null ? geo.getCountry() : null);
99+
}
100+
101+
public Boolean isEU() {
102+
return context != null && geo != null ? context.isEU(geo.getCountryCode()) : null;
103+
}
104+
105+
public CountryFlag getCountryFlag() {
106+
return context != null && geo != null ? context.getCountryFlag(geo.getCountryCode()) : null;
107+
}
108+
109+
public String getCountryFlagURL() {
110+
return context != null && geo != null ? context.getCountryFlagURL(geo.getCountryCode()) : null;
111+
}
112+
113+
public CountryCurrency getCountryCurrency() {
114+
return context != null && geo != null ? context.getCountryCurrency(geo.getCountryCode()) : null;
115+
}
116+
117+
public Continent getContinentInfo() {
118+
return context != null && geo != null ? context.getContinent(geo.getCountryCode()) : null;
119+
}
120+
121+
@Override
122+
public String toString() {
123+
if (bogon) {
124+
return "IPResponseCore{" +
125+
"ip='" + ip + '\'' +
126+
", bogon=" + bogon +
127+
'}';
128+
}
129+
return "IPResponseCore{" +
130+
"ip='" + ip + '\'' +
131+
", geo=" + geo +
132+
", asn=" + asn +
133+
", is_anonymous=" + is_anonymous +
134+
", is_anycast=" + is_anycast +
135+
", is_hosting=" + is_hosting +
136+
", is_mobile=" + is_mobile +
137+
", is_satellite=" + is_satellite +
138+
'}';
139+
}
140+
}
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.IPResponseCore;
6+
import okhttp3.OkHttpClient;
7+
import okhttp3.Request;
8+
import okhttp3.Response;
9+
10+
public class IPRequestCore extends BaseRequest<IPResponseCore> {
11+
private final static String URL_FORMAT = "https://api.ipinfo.io/lookup/%s";
12+
private final String ip;
13+
14+
public IPRequestCore(OkHttpClient client, String token, String ip) {
15+
super(client, token);
16+
this.ip = ip;
17+
}
18+
19+
@Override
20+
public IPResponseCore handle() throws RateLimitedException {
21+
if (IPRequest.isBogon(ip)) {
22+
try {
23+
return new IPResponseCore(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(), IPResponseCore.class);
39+
} catch (Exception ex) {
40+
throw new ErrorResponseException(ex);
41+
}
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)