-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathIPinfoPlus.java
More file actions
91 lines (76 loc) · 2.41 KB
/
IPinfoPlus.java
File metadata and controls
91 lines (76 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package io.ipinfo.api;
import io.ipinfo.api.cache.Cache;
import io.ipinfo.api.cache.SimpleCache;
import io.ipinfo.api.context.Context;
import io.ipinfo.api.errors.RateLimitedException;
import io.ipinfo.api.model.IPResponsePlus;
import io.ipinfo.api.request.IPRequestPlus;
import java.time.Duration;
import okhttp3.OkHttpClient;
public class IPinfoPlus {
private final OkHttpClient client;
private final Context context;
private final String token;
private final Cache cache;
IPinfoPlus(
OkHttpClient client,
Context context,
String token,
Cache cache
) {
this.client = client;
this.context = context;
this.token = token;
this.cache = cache;
}
public static void main(String[] args) throws RateLimitedException {
System.out.println("Running IPinfo Plus client");
}
/**
* Lookup IP information using the IP. This is a blocking call.
*
* @param ip IP address to query information for.
* @return Response containing IP information.
* @throws RateLimitedException if the user has exceeded the rate limit.
*/
public IPResponsePlus lookupIP(String ip) throws RateLimitedException {
IPRequestPlus request = new IPRequestPlus(client, token, ip);
IPResponsePlus response = request.handle();
if (response != null) {
response.setContext(context);
if (!response.getBogon()) {
cache.set(cacheKey(ip), response);
}
}
return response;
}
public static String cacheKey(String k) {
return "plus_" + k;
}
public static class Builder {
private OkHttpClient client;
private String token;
private Cache cache;
public Builder setClient(OkHttpClient client) {
this.client = client;
return this;
}
public Builder setToken(String token) {
this.token = token;
return this;
}
public Builder setCache(Cache cache) {
this.cache = cache;
return this;
}
public IPinfoPlus build() {
if (client == null) {
client = new OkHttpClient();
}
if (cache == null) {
cache = new SimpleCache(Duration.ofDays(1));
}
return new IPinfoPlus(client, new Context(), token, cache);
}
}
}