Skip to content

Commit db1992e

Browse files
committed
Remove duplicate code
1 parent 7e73368 commit db1992e

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
package io.ipinfo.errors;
22

33
public class ErrorResponseException extends RuntimeException {
4+
5+
public ErrorResponseException() {
6+
7+
}
8+
9+
public ErrorResponseException(Exception ex) {
10+
super(ex);
11+
}
412
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ public ASNResponse handle() throws RateLimitedException {
2525
try (Response response = handleRequest(request)) {
2626
if (response == null || response.body() == null) return null;
2727

28-
if (response.code() == 429) {
29-
throw new RateLimitedException();
30-
}
31-
3228
try {
3329
return gson.fromJson(response.body().string(), ASNResponse.class);
3430
} catch (Exception ex) {

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

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

33
import com.google.gson.Gson;
4+
import io.ipinfo.errors.ErrorResponseException;
45
import io.ipinfo.errors.RateLimitedException;
6+
import io.ipinfo.model.ASNResponse;
57
import okhttp3.Credentials;
68
import okhttp3.OkHttpClient;
79
import okhttp3.Request;
@@ -20,17 +22,28 @@ protected BaseRequest(OkHttpClient client, String token) {
2022

2123
public abstract T handle() throws RateLimitedException;
2224

23-
public Response handleRequest(Request.Builder request) {
25+
public Response handleRequest(Request.Builder request) throws RateLimitedException {
2426
request
2527
.addHeader("Authorization", Credentials.basic(token, ""))
2628
.addHeader("user-agent", "IPinfoClient/Java/1.0")
2729
.addHeader("Content-Type", "application/json");
2830

31+
Response response;
32+
2933
try {
30-
return client.newCall(request.build()).execute();
34+
response = client.newCall(request.build()).execute();
3135
} catch (Exception e) {
32-
return null;
36+
throw new ErrorResponseException(e);
3337
}
38+
39+
// Sanity check
40+
if (response == null) return null;
41+
42+
if (response.code() == 429) {
43+
throw new RateLimitedException();
44+
}
45+
46+
return response;
3447
}
3548
}
3649

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ public IPResponse handle() throws RateLimitedException {
2424
try (Response response = handleRequest(request)) {
2525
if (response == null || response.body() == null) return null;
2626

27-
if (response.code() == 429) {
28-
throw new RateLimitedException();
29-
}
30-
3127
try {
3228
return gson.fromJson(response.body().string(), IPResponse.class);
3329
} catch (Exception ex) {

0 commit comments

Comments
 (0)