Skip to content

Commit 0f440ac

Browse files
authored
Merge pull request #26 from ipinfo/silvano/eng-296-add-lite-api-support-to-ipinfospring
Add Lite API support
2 parents 9857717 + 306d044 commit 0f440ac

File tree

9 files changed

+592
-13
lines changed

9 files changed

+592
-13
lines changed

.github/workflows/test.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches:
6+
- "master"
7+
pull_request:
8+
branches:
9+
- "master"
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- uses: actions/checkout@v4
17+
18+
- name: Set up JDK 17
19+
uses: actions/setup-java@v4
20+
with:
21+
java-version: "17"
22+
distribution: "temurin"
23+
cache: maven
24+
25+
- name: Run Maven Tests
26+
run: mvn -B test

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fields and additional request volumes see
2525

2626
[Click here to view the Java Spring SDK's API documentation](https://ipinfo.github.io/spring/).
2727

28-
⚠️ 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.
28+
The library also supports the Lite API, see the [Lite API section](#lite-api) for more info.
2929

3030
## Usage
3131

@@ -174,6 +174,30 @@ The `AttributeStrategy` allows the middleware to know where to store the
174174
Any exceptions such as `RateLimitedException` is passed through Spring's error
175175
handling system.
176176

177+
### Lite API
178+
179+
The library gives the possibility to use the [Lite API](https://ipinfo.io/developers/lite-api) too, authentication with your token is still required.
180+
181+
The returned details are slightly different from the Core API.
182+
183+
To use the Lite API you must use the `IPinfoLiteSpring`, it works in the same way as the `IPinfoSpring` class.
184+
185+
```java
186+
IPinfoLiteSpring ipinfoSpring = new IPinfoLiteSpring.Builder()
187+
// Set the IPinfo instance. By default we provide one, however you're
188+
// allowed to change this here. Also provide your IPinfo Access Token here.
189+
.setIPinfo(new IPinfoLite.Builder().setToken("IPINFO ACCESS TOKEN").build())
190+
// Set the InterceptorStrategy. By default we use
191+
// BotInterceptorStrategy.
192+
.interceptorStrategy(new BotInterceptorStrategy())
193+
// Set the IPStrategy. By default we use SimpleIPStrategy.
194+
.ipStrategy(new SimpleIPStrategy())
195+
// Set the AttributeStrategy. By default we use SessionAttributeStrategy.
196+
.attributeStrategy(new SessionAttributeStrategy())
197+
// Finally build it.
198+
.build();
199+
```
200+
177201
### Other Libraries
178202

179203
There are [official IPinfo client libraries](https://ipinfo.io/developers/libraries) available for many languages including PHP, Python, Go, Java, Ruby, and many popular frameworks such as Django, Rails, and Laravel. There are also many third-party libraries and integrations available for our API.

pom.xml

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,35 +68,59 @@
6868
<dependency>
6969
<groupId>io.ipinfo</groupId>
7070
<artifactId>ipinfo-api</artifactId>
71-
<version>2.2.1</version>
71+
<version>3.1.0</version>
7272
<scope>compile</scope>
7373
</dependency>
7474
<dependency>
7575
<groupId>com.google.code.gson</groupId>
7676
<artifactId>gson</artifactId>
77-
<version>2.8.7</version>
77+
<version>2.13.1</version>
7878
<scope>compile</scope>
7979
</dependency>
8080
<dependency>
8181
<groupId>org.junit.jupiter</groupId>
8282
<artifactId>junit-jupiter-api</artifactId>
83-
<version>5.2.0</version>
83+
<version>6.0.0-M2</version>
8484
<scope>test</scope>
8585
</dependency>
8686
<dependency>
8787
<groupId>org.springframework</groupId>
8888
<artifactId>spring-webmvc</artifactId>
89-
<version>6.0.10</version>
89+
<version>7.0.0-M7</version>
9090
</dependency>
9191
<dependency>
9292
<groupId>org.springframework</groupId>
9393
<artifactId>spring-core</artifactId>
94-
<version>6.0.10</version>
94+
<version>7.0.0-M7</version>
9595
</dependency>
9696
<dependency>
9797
<groupId>org.apache.tomcat.embed</groupId>
9898
<artifactId>tomcat-embed-core</artifactId>
99-
<version>10.1.11</version>
99+
<version>11.0.9</version>
100+
</dependency>
101+
<dependency>
102+
<groupId>org.junit.jupiter</groupId>
103+
<artifactId>junit-jupiter-engine</artifactId>
104+
<version>6.0.0-M2</version>
105+
<scope>test</scope>
106+
</dependency>
107+
<dependency>
108+
<groupId>org.mockito</groupId>
109+
<artifactId>mockito-core</artifactId>
110+
<version>5.18.0</version>
111+
<scope>test</scope>
112+
</dependency>
113+
<dependency>
114+
<groupId>org.mockito</groupId>
115+
<artifactId>mockito-junit-jupiter</artifactId>
116+
<version>5.18.0</version>
117+
<scope>test</scope>
118+
</dependency>
119+
<dependency>
120+
<groupId>org.springframework</groupId>
121+
<artifactId>spring-test</artifactId>
122+
<version>7.0.0-M7</version>
123+
<scope>test</scope>
100124
</dependency>
101125
</dependencies>
102126

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package io.ipinfo.spring;
2+
3+
import io.ipinfo.api.IPinfoLite;
4+
import io.ipinfo.api.model.IPResponseLite;
5+
import io.ipinfo.spring.strategies.attribute.AttributeStrategy;
6+
import io.ipinfo.spring.strategies.attribute.SessionAttributeStrategy;
7+
import io.ipinfo.spring.strategies.interceptor.BotInterceptorStrategy;
8+
import io.ipinfo.spring.strategies.interceptor.InterceptorStrategy;
9+
import io.ipinfo.spring.strategies.ip.IPStrategy;
10+
import io.ipinfo.spring.strategies.ip.SimpleIPStrategy;
11+
import jakarta.servlet.http.HttpServletRequest;
12+
import jakarta.servlet.http.HttpServletResponse;
13+
import org.springframework.web.servlet.HandlerInterceptor;
14+
15+
public class IPinfoLiteSpring implements HandlerInterceptor {
16+
17+
public static final String ATTRIBUTE_KEY =
18+
"IPinfoOfficialSparkWrapper.IPResponseLite";
19+
private final IPinfoLite ii;
20+
private final AttributeStrategy attributeStrategy;
21+
private final IPStrategy ipStrategy;
22+
private final InterceptorStrategy interceptorStrategy;
23+
24+
IPinfoLiteSpring(
25+
IPinfoLite ii,
26+
AttributeStrategy attributeStrategy,
27+
IPStrategy ipStrategy,
28+
InterceptorStrategy interceptorStrategy
29+
) {
30+
this.ii = ii;
31+
this.attributeStrategy = attributeStrategy;
32+
this.ipStrategy = ipStrategy;
33+
this.interceptorStrategy = interceptorStrategy;
34+
}
35+
36+
public static void main(String... args) {
37+
System.out.println(
38+
"This library is not meant to be run as a standalone jar."
39+
);
40+
System.exit(0);
41+
}
42+
43+
@Override
44+
public boolean preHandle(
45+
HttpServletRequest request,
46+
HttpServletResponse response,
47+
Object handler
48+
) throws Exception {
49+
if (!interceptorStrategy.shouldRun(request)) {
50+
return true;
51+
}
52+
53+
// Don't waste an API call if we already have it.
54+
// This should only happen for RequestAttributeStrategy and potentially
55+
// other implementations.
56+
if (attributeStrategy.hasLiteAttribute(request)) {
57+
return true;
58+
}
59+
60+
String ip = ipStrategy.getIPAddress(request);
61+
if (ip == null) {
62+
return true;
63+
}
64+
65+
IPResponseLite ipResponse = ii.lookupIP(ip);
66+
attributeStrategy.storeLiteAttribute(request, ipResponse);
67+
68+
return true;
69+
}
70+
71+
public static class Builder {
72+
73+
private IPinfoLite ii = new IPinfoLite.Builder().build();
74+
private AttributeStrategy attributeStrategy =
75+
new SessionAttributeStrategy();
76+
private IPStrategy ipStrategy = new SimpleIPStrategy();
77+
private InterceptorStrategy interceptorStrategy =
78+
new BotInterceptorStrategy();
79+
80+
public Builder setIPinfo(IPinfoLite ii) {
81+
this.ii = ii;
82+
return this;
83+
}
84+
85+
public Builder attributeStrategy(AttributeStrategy attributeStrategy) {
86+
this.attributeStrategy = attributeStrategy;
87+
return this;
88+
}
89+
90+
public Builder ipStrategy(IPStrategy ipStrategy) {
91+
this.ipStrategy = ipStrategy;
92+
return this;
93+
}
94+
95+
public Builder interceptorStrategy(
96+
InterceptorStrategy interceptorStrategy
97+
) {
98+
this.interceptorStrategy = interceptorStrategy;
99+
return this;
100+
}
101+
102+
public IPinfoLiteSpring build() {
103+
return new IPinfoLiteSpring(
104+
ii,
105+
attributeStrategy,
106+
ipStrategy,
107+
interceptorStrategy
108+
);
109+
}
110+
}
111+
}

src/main/java/io/ipinfo/spring/strategies/attribute/AttributeStrategy.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.ipinfo.spring.strategies.attribute;
22

33
import io.ipinfo.api.model.IPResponse;
4-
4+
import io.ipinfo.api.model.IPResponseLite;
55
import jakarta.servlet.http.HttpServletRequest;
66

77
public interface AttributeStrategy {
@@ -16,4 +16,23 @@ default boolean hasAttribute(HttpServletRequest request) {
1616

1717
return false;
1818
}
19+
20+
default void storeLiteAttribute(
21+
HttpServletRequest request,
22+
IPResponseLite response
23+
) {
24+
throw new UnsupportedOperationException(
25+
"This strategy does not support IPResponseLite."
26+
);
27+
}
28+
29+
default IPResponseLite getLiteAttribute(HttpServletRequest request) {
30+
throw new UnsupportedOperationException(
31+
"This strategy does not support IPResponseLite."
32+
);
33+
}
34+
35+
default boolean hasLiteAttribute(HttpServletRequest request) {
36+
return getLiteAttribute(request) != null;
37+
}
1938
}
Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
package io.ipinfo.spring.strategies.attribute;
22

33
import io.ipinfo.api.model.IPResponse;
4+
import io.ipinfo.api.model.IPResponseLite;
5+
import io.ipinfo.spring.IPinfoLiteSpring;
46
import io.ipinfo.spring.IPinfoSpring;
5-
67
import jakarta.servlet.http.HttpServletRequest;
78

89
public class RequestAttributeStrategy implements AttributeStrategy {
10+
911
@Override
10-
public void storeAttribute(HttpServletRequest request, IPResponse response) {
12+
public void storeAttribute(
13+
HttpServletRequest request,
14+
IPResponse response
15+
) {
1116
request.setAttribute(IPinfoSpring.ATTRIBUTE_KEY, response);
1217
}
1318

1419
@Override
1520
public IPResponse getAttribute(HttpServletRequest request) {
1621
return (IPResponse) request.getAttribute(IPinfoSpring.ATTRIBUTE_KEY);
1722
}
23+
24+
@Override
25+
public void storeLiteAttribute(
26+
HttpServletRequest request,
27+
IPResponseLite response
28+
) {
29+
request.setAttribute(IPinfoLiteSpring.ATTRIBUTE_KEY, response);
30+
}
31+
32+
@Override
33+
public IPResponseLite getLiteAttribute(HttpServletRequest request) {
34+
return (IPResponseLite) request.getAttribute(
35+
IPinfoLiteSpring.ATTRIBUTE_KEY
36+
);
37+
}
1838
}
Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,42 @@
11
package io.ipinfo.spring.strategies.attribute;
22

33
import io.ipinfo.api.model.IPResponse;
4+
import io.ipinfo.api.model.IPResponseLite;
5+
import io.ipinfo.spring.IPinfoLiteSpring;
46
import io.ipinfo.spring.IPinfoSpring;
5-
67
import jakarta.servlet.http.HttpServletRequest;
78

89
public class SessionAttributeStrategy implements AttributeStrategy {
10+
911
@Override
10-
public void storeAttribute(HttpServletRequest request, IPResponse response) {
12+
public void storeAttribute(
13+
HttpServletRequest request,
14+
IPResponse response
15+
) {
1116
request.getSession().setAttribute(IPinfoSpring.ATTRIBUTE_KEY, response);
1217
}
1318

1419
@Override
1520
public IPResponse getAttribute(HttpServletRequest request) {
16-
return (IPResponse) request.getSession().getAttribute(IPinfoSpring.ATTRIBUTE_KEY);
21+
return (IPResponse) request
22+
.getSession()
23+
.getAttribute(IPinfoSpring.ATTRIBUTE_KEY);
24+
}
25+
26+
@Override
27+
public void storeLiteAttribute(
28+
HttpServletRequest request,
29+
IPResponseLite response
30+
) {
31+
request
32+
.getSession()
33+
.setAttribute(IPinfoLiteSpring.ATTRIBUTE_KEY, response);
34+
}
35+
36+
@Override
37+
public IPResponseLite getLiteAttribute(HttpServletRequest request) {
38+
return (IPResponseLite) request
39+
.getSession()
40+
.getAttribute(IPinfoLiteSpring.ATTRIBUTE_KEY);
1741
}
1842
}

0 commit comments

Comments
 (0)