Skip to content

Commit 76ccd80

Browse files
committed
APIOfflineException functionality and beginnings of a streamlined HttpUtils
1 parent 707811c commit 76ccd80

File tree

3 files changed

+59
-15
lines changed

3 files changed

+59
-15
lines changed

src/main/java/exceptions/APIOfflineException.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88

99
package exceptions;
1010

11+
/**
12+
* Indicates that the request returned error code 5 (The API has been disabled by Bungie)
13+
*/
1114
public class APIOfflineException extends Exception {
1215

13-
public APIOfflineException() {
14-
super("The Bungie API is currently not responding. ");
16+
public APIOfflineException(String returnMessage) {
17+
super("The Bungie API returned this message: " + returnMessage);
1518
}
1619
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (c) dec4234 2021. Access is granted, without any express warranties or guarantees of
3+
* any kind, to all wishing to use this software for their benefit. No one may specifically claim credit, or
4+
* ownership of this software without the explicit permission of the author.
5+
*
6+
* GitHub -> https://github.com/dec4234/JavaDestinyAPI
7+
*/
8+
9+
package utils;
10+
11+
import java.net.http.HttpRequest;
12+
13+
@FunctionalInterface
14+
public interface HttpRequestModifier {
15+
16+
HttpRequest modifyRequest(HttpRequest starter);
17+
}

src/main/java/utils/HttpUtils.java

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package utils;
1010

1111
import com.google.gson.*;
12+
import exceptions.APIOfflineException;
1213
import exceptions.AccessTokenInvalidException;
1314
import material.DestinyAPI;
1415
import material.manifest.ManifestEntityTypes;
@@ -44,16 +45,23 @@ public JsonObject urlRequestGET(String url) {
4445
.build();
4546
CompletableFuture<String> response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenApplyAsync(HttpResponse::body);
4647
JsonElement parse = null;
48+
JsonObject jsonObject = null;
4749
try {
48-
if(DestinyAPI.isDebugEnabled()) {
49-
System.out.println(response.get());
50+
String responseString = response.get();
51+
if (DestinyAPI.isDebugEnabled()) {
52+
System.out.println(responseString);
5053
}
5154
parse = new JsonParser().parse(response.get()); // Parse response to JSON
52-
} catch (InterruptedException | ExecutionException e) {
55+
jsonObject = parse.getAsJsonObject();
56+
if(jsonObject.has("ErrorCode") && jsonObject.get("ErrorCode").getAsInt() == 5) {
57+
throw new APIOfflineException(jsonObject.get("Message").getAsString());
58+
}
59+
60+
} catch (InterruptedException | ExecutionException | APIOfflineException e) {
5361
e.printStackTrace();
5462
return null;
5563
}
56-
return parse.getAsJsonObject();
64+
return jsonObject;
5765
}
5866

5967
public Object urlRequestGETstring(String url) throws ExecutionException, InterruptedException {
@@ -73,12 +81,12 @@ public JsonObject urlRequestGETOauth(String url) {
7381

7482
HttpClient client = HttpClient.newHttpClient();
7583
HttpRequest request = HttpRequest.newBuilder()
76-
.uri(URI.create(url))
77-
.timeout(Duration.ofMinutes(1))
78-
.setHeader("X-API-KEY", apiKey)
79-
.setHeader("Authorization", "Bearer " + HttpUtils.bearerToken)
80-
.GET()
81-
.build();
84+
.uri(URI.create(url))
85+
.timeout(Duration.ofMinutes(1))
86+
.setHeader("X-API-KEY", apiKey)
87+
.setHeader("Authorization", "Bearer " + HttpUtils.bearerToken)
88+
.GET()
89+
.build();
8290
CompletableFuture<String> response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenApplyAsync(HttpResponse::body);
8391
JsonElement parse = null;
8492
try {
@@ -91,7 +99,7 @@ public JsonObject urlRequestGETOauth(String url) {
9199
}
92100

93101
public String urlRequestPOST(String url, String body) {
94-
if(body.isEmpty()) body = "{\"message\": \"\",}";
102+
if (body.isEmpty()) { body = "{\"message\": \"\",}"; }
95103
HttpClient client = HttpClient.newHttpClient();
96104
HttpRequest request = HttpRequest.newBuilder()
97105
.uri(URI.create(url))
@@ -112,7 +120,7 @@ public String urlRequestPOST(String url, String body) {
112120
public String urlRequestPOSTOauth(String url, String body) {
113121
setTokenViaRefresh();
114122

115-
if(body.isEmpty()) body = "{\"message\": \"\",}";
123+
if (body.isEmpty()) { body = "{\"message\": \"\",}"; }
116124
HttpClient client = HttpClient.newHttpClient();
117125
HttpRequest request = HttpRequest.newBuilder()
118126
.uri(URI.create(url))
@@ -143,6 +151,7 @@ public String generateLineGraph() {
143151

144152
/**
145153
* Gets an access token using the refresh token in storage and replaces the old refresh token with the new one
154+
*
146155
* @return Returns the new access token
147156
*/
148157
public String setTokenViaRefresh() {
@@ -202,7 +211,7 @@ public void setTokenViaAuth() {
202211
}
203212

204213
public boolean checkFor401(String input) {
205-
if(input.contains("401 - Unauthorized")) {
214+
if (input.contains("401 - Unauthorized")) {
206215
try {
207216
setTokenViaRefresh();
208217
throw new AccessTokenInvalidException("The access token used in this OAuth request was not accepted by the server \nI've already taken the liberty of getting a new access token for you :D");
@@ -214,4 +223,19 @@ public boolean checkFor401(String input) {
214223

215224
return false;
216225
}
226+
227+
private String getStringResponse(HttpRequest httpRequest) {
228+
HttpClient httpClient = HttpClient.newHttpClient();
229+
try {
230+
return httpClient.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString()).thenApplyAsync(HttpResponse::body).get();
231+
} catch (InterruptedException | ExecutionException e) {
232+
e.printStackTrace();
233+
}
234+
235+
return null;
236+
}
237+
238+
private HttpRequest getRequest(HttpRequestModifier httpRequestModifier) {
239+
return httpRequestModifier.modifyRequest(HttpRequest.newBuilder().build());
240+
}
217241
}

0 commit comments

Comments
 (0)