Skip to content

Commit 7e73368

Browse files
committed
Some changes based on feedback
1 parent 0c84e3b commit 7e73368

File tree

9 files changed

+86
-20
lines changed

9 files changed

+86
-20
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public class Main {
150150
System.out.println(response.getCountryCode());
151151

152152
// Print out the country name
153-
System.out.println(ipInfo.lookupCountryName(response.getCountryCode()));
153+
System.out.println(response.getCountryName());
154154
} catch (RateLimitedException ex) {
155155
// Handle rate limits here.
156156
}
@@ -162,6 +162,34 @@ This file must follow the same layout as seen [here](https://github.com/ipinfo/j
162162

163163
More language files can be found [here](https://country.io/data)
164164

165+
### Location Information
166+
167+
This library provides an easy way to get the latitude and longitude of an IP Address:
168+
169+
```java
170+
import io.ipinfo.IPInfo;
171+
import io.ipinfo.errors.RateLimitedException;
172+
import io.ipinfo.model.IPResponse;
173+
174+
public class Main {
175+
176+
public static void main(String... args) {
177+
IPInfo ipInfo = IPInfo.builder().setToken("YOUR TOKEN").setCountryFile(new File("path/to/file.json")).build();
178+
179+
try {
180+
IPResponse response = ipInfo.lookupIP("8.8.8.8");
181+
182+
// Print out the Latitude and Longitude
183+
System.out.println(response.getLatitude());
184+
System.out.println(response.getLongitude());
185+
186+
} catch (RateLimitedException ex) {
187+
// Handle rate limits here.
188+
}
189+
}
190+
}
191+
```
192+
165193
## Extra Information
166194

167195
- This library is thread safe. Feel free to call the different endpoints from different threads.

src/main/java/io/ipinfo/IPInfo.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
package io.ipinfo;
22

33
import io.ipinfo.cache.Cache;
4+
import io.ipinfo.context.Context;
45
import io.ipinfo.errors.RateLimitedException;
56
import io.ipinfo.model.ASNResponse;
67
import io.ipinfo.model.IPResponse;
78
import io.ipinfo.request.ASNRequest;
89
import io.ipinfo.request.IPRequest;
910
import okhttp3.OkHttpClient;
1011

11-
import java.util.Map;
12-
1312
public class IPInfo {
1413
private final OkHttpClient client;
14+
private final Context context;
1515
private final String token;
16-
private final Map<String, String> countryMap;
1716
private final Cache cache;
1817

19-
IPInfo(OkHttpClient client, String token, Map<String, String> countryMap, Cache cache) {
18+
IPInfo(OkHttpClient client, Context context, String token, Cache cache) {
2019
this.client = client;
20+
this.context = context;
2121
this.token = token;
22-
this.countryMap = countryMap;
2322
this.cache = cache;
2423
}
2524

@@ -50,6 +49,8 @@ public IPResponse lookupIP(String ip) throws RateLimitedException {
5049
if (response != null) return response;
5150

5251
response = new IPRequest(client, token, ip).handle();
52+
response.setContext(context);
53+
5354
cache.setIp(ip, response);
5455
return response;
5556
}
@@ -66,17 +67,9 @@ public ASNResponse lookupASN(String asn) throws RateLimitedException {
6667
if (response != null) return response;
6768

6869
response = new ASNRequest(client, token, asn).handle();
70+
response.setContext(context);
71+
6972
cache.setAsn(asn, response);
7073
return response;
7174
}
72-
73-
/**
74-
* Looks up a country name.
75-
*
76-
* @param countryCode
77-
* @return The name of the country.
78-
*/
79-
public String lookupCountryName(String countryCode) {
80-
return countryMap.getOrDefault(countryCode, null);
81-
}
8275
}

src/main/java/io/ipinfo/IPInfoBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.google.gson.reflect.TypeToken;
55
import io.ipinfo.cache.Cache;
66
import io.ipinfo.cache.SimpleCache;
7+
import io.ipinfo.context.Context;
78
import okhttp3.OkHttpClient;
89

910
import java.io.File;
@@ -53,6 +54,6 @@ public IPInfo build() {
5354
map = Collections.unmodifiableMap(new HashMap<>());
5455
}
5556

56-
return new IPInfo(client, token, map, cache);
57+
return new IPInfo(client, new Context(map), token, cache);
5758
}
5859
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.ipinfo.context;
2+
3+
import java.util.Map;
4+
5+
public class Context {
6+
private final Map<String, String> countryMap;
7+
8+
public Context(Map<String, String> countryMap) {
9+
this.countryMap = countryMap;
10+
}
11+
12+
public String getCountryName(String countryCode) {
13+
return countryMap.get(countryCode);
14+
}
15+
}

src/main/java/io/ipinfo/model/ASNResponse.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import com.google.gson.annotations.SerializedName;
5+
import io.ipinfo.context.Context;
56

67
import java.util.List;
78

@@ -16,6 +17,7 @@ public class ASNResponse {
1617
private final String numIps;
1718
private final List<Prefix> prefixes;
1819
private final List<Prefix> prefixes6;
20+
private transient Context context;
1921

2022
public ASNResponse(String asn, String name, String country, String allocated, String registry, String domain, String numIps, List<Prefix> prefixes, List<Prefix> prefixes6) {
2123
this.asn = asn;
@@ -29,6 +31,15 @@ public ASNResponse(String asn, String name, String country, String allocated, St
2931
this.prefixes6 = prefixes6;
3032
}
3133

34+
/**
35+
* Set by the library for extra utility functions
36+
*
37+
* @param context
38+
*/
39+
public void setContext(Context context) {
40+
this.context = context;
41+
}
42+
3243
public String getAsn() {
3344
return asn;
3445
}

src/main/java/io/ipinfo/model/IPResponse.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.ipinfo.model;
22

3+
import io.ipinfo.context.Context;
4+
35
public class IPResponse {
46
private final String ip;
57
private final String hostname;
@@ -13,6 +15,7 @@ public class IPResponse {
1315
private final ASN asn;
1416
private final Company company;
1517
private final Carrier carrier;
18+
private transient Context context;
1619

1720
public IPResponse(String ip, String hostname, String city, String region, String country, String loc, String postal, String org, String phone, ASN asn, Company company, Carrier carrier) {
1821
this.ip = ip;
@@ -29,6 +32,15 @@ public IPResponse(String ip, String hostname, String city, String region, String
2932
this.carrier = carrier;
3033
}
3134

35+
/**
36+
* Set by the library for extra utility functions
37+
*
38+
* @param context
39+
*/
40+
public void setContext(Context context) {
41+
this.context = context;
42+
}
43+
3244
public String getIp() {
3345
return ip;
3446
}
@@ -49,6 +61,10 @@ public String getCountryCode() {
4961
return country;
5062
}
5163

64+
public String getCountryName() {
65+
return context.getCountryName(getCountryCode());
66+
}
67+
5268
public String getLocation() {
5369
return loc;
5470
}

src/main/java/io/ipinfo/request/ASNRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
public class ASNRequest extends BaseRequest<ASNResponse> {
12-
private final static String URL_FORMAT = "https://ipinfo.io/%s/json";
12+
private final static String URL_FORMAT = "https://ipinfo.io/%s";
1313
private final String asn;
1414

1515
public ASNRequest(OkHttpClient client, String token, String asn) {

src/main/java/io/ipinfo/request/BaseRequest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ protected BaseRequest(OkHttpClient client, String token) {
2323
public Response handleRequest(Request.Builder request) {
2424
request
2525
.addHeader("Authorization", Credentials.basic(token, ""))
26-
.addHeader("user-agent", "IPinfoClient/Java/1.0");
26+
.addHeader("user-agent", "IPinfoClient/Java/1.0")
27+
.addHeader("Content-Type", "application/json");
28+
2729
try {
2830
return client.newCall(request.build()).execute();
2931
} catch (Exception e) {

src/main/java/io/ipinfo/request/IPRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import okhttp3.Response;
99

1010
public class IPRequest extends BaseRequest<IPResponse> {
11-
private final static String URL_FORMAT = "https://ipinfo.io/%s/json";
11+
private final static String URL_FORMAT = "https://ipinfo.io/%s";
1212
private final String ip;
1313

1414
public IPRequest(OkHttpClient client, String token, String ip) {

0 commit comments

Comments
 (0)