1313import com .google .gson .JsonSyntaxException ;
1414import net .dec4234 .javadestinyapi .exceptions .APIException ;
1515import net .dec4234 .javadestinyapi .exceptions .APIOfflineException ;
16+ import net .dec4234 .javadestinyapi .exceptions .AccessTokenExpiredException ;
1617import net .dec4234 .javadestinyapi .exceptions .ConnectionException ;
1718import net .dec4234 .javadestinyapi .exceptions .InvalidConditionException ;
1819import net .dec4234 .javadestinyapi .exceptions .JsonParsingError ;
20+ import net .dec4234 .javadestinyapi .exceptions .OAuthUnauthorizedException ;
21+ import net .dec4234 .javadestinyapi .exceptions .RefreshTokenExpiredException ;
1922import net .dec4234 .javadestinyapi .material .DestinyAPI ;
2023import net .dec4234 .javadestinyapi .material .manifest .ManifestEntityTypes ;
2124
@@ -50,19 +53,28 @@ public void setApiKey(String apiKey) {
5053 * Send a GET url request to the url provided, returns a JsonObject of the response
5154 */
5255 public JsonObject urlRequestGET (String url ) throws APIException {
53- return getJsonObject (getStringResponse ( getRequest (true , url , starter -> {
56+ return getJsonObject (getRequest (true , url , starter -> {
5457 starter .GET ();
5558 return starter ;
56- }))) ;
59+ }));
5760 }
5861
5962 public JsonObject urlRequestGETOauth (String url ) throws APIException {
60- setTokenViaRefresh ();
61- return getJsonObject (getStringResponse (getRequest (true , url , starter -> {
62- starter .GET ()
63- .setHeader ("Authorization" , "Bearer " + HttpUtils .bearerToken );
64- return starter ;
65- })));
63+ try {
64+ return getJsonObject (getRequest (true , url , starter -> {
65+ starter .GET ()
66+ .setHeader ("Authorization" , "Bearer " + HttpUtils .bearerToken );
67+ return starter ;
68+ }));
69+ } catch (AccessTokenExpiredException e ) {
70+ setTokenViaRefresh ();
71+
72+ return getJsonObject (getRequest (true , url , starter -> {
73+ starter .GET ()
74+ .setHeader ("Authorization" , "Bearer " + HttpUtils .bearerToken );
75+ return starter ;
76+ }));
77+ }
6678 }
6779
6880 public JsonObject urlRequestPOST (String url , JsonObject body ) throws APIException {
@@ -77,29 +89,10 @@ public JsonObject urlRequestPOST(String url, String body) throws APIException {
7789 System .out .println ("Body: " + finalBody );
7890 }
7991
80- return getJsonObject (getStringResponse ( getRequest (true , url , starter -> {
92+ return getJsonObject (getRequest (true , url , starter -> {
8193 starter .setHeader ("Content-Type" , "application/json" )
8294 .POST (HttpRequest .BodyPublishers .ofString (finalBody ));
8395
84- return starter ;
85- })));
86- }
87-
88- public String urlRequestPOSTOauth (String url , String body ) throws APIException {
89- setTokenViaRefresh ();
90- if (body .isEmpty ()) { body = "{\" message\" : \" \" ,}" ; }
91-
92- String finalBody = body ;
93-
94- if (DestinyAPI .isDebugEnabled ()) {
95- System .out .println ("Body: " + finalBody );
96- }
97-
98- return getStringResponse (getRequest (true , url , starter -> {
99- starter .setHeader ("Authorization" , "Bearer " + HttpUtils .bearerToken )
100- .setHeader ("Content-Type" , "application/json" )
101- .POST (HttpRequest .BodyPublishers .ofString (finalBody ));
102-
10396 return starter ;
10497 }));
10598 }
@@ -117,13 +110,28 @@ public JsonObject urlRequestPOSTOauth(String url, JsonObject body) throws APIExc
117110 System .out .println ("Body: " + finalBody );
118111 }
119112
120- return getJsonObject (getStringResponse (getRequest (true , url , starter -> {
121- starter .setHeader ("Authorization" , "Bearer " + HttpUtils .bearerToken )
122- .setHeader ("Content-Type" , "application/json" )
123- .POST (HttpRequest .BodyPublishers .ofString (finalBody ));
113+ try {
114+ return getJsonObject (getRequest (true , url , starter -> {
115+ starter .setHeader ("Authorization" , "Bearer " + HttpUtils .bearerToken )
116+ .setHeader ("Content-Type" , "application/json" )
117+ .POST (HttpRequest .BodyPublishers .ofString (finalBody ));
118+
119+ return starter ;
120+ }));
121+ } catch (AccessTokenExpiredException e ) {
122+ setTokenViaRefresh ();
123+ return getJsonObject (getRequest (true , url , starter -> {
124+ starter .setHeader ("Authorization" , "Bearer " + HttpUtils .bearerToken )
125+ .setHeader ("Content-Type" , "application/json" )
126+ .POST (HttpRequest .BodyPublishers .ofString (finalBody ));
127+
128+ return starter ;
129+ }));
130+ }
131+ }
124132
125- return starter ;
126- }) ));
133+ public JsonObject urlRequestPOSTOauth ( String url ) throws APIException {
134+ return urlRequestPOSTOauth ( url , new JsonObject ( ));
127135 }
128136
129137 /**
@@ -146,13 +154,13 @@ public String setTokenViaRefresh() throws APIException {
146154
147155 String requestBody = "grant_type=refresh_token&refresh_token=" + DestinyAPI .getRefreshToken ();
148156
149- JsonObject response = getJsonObject (getStringResponse ( getRequest (false , url , starter -> {
157+ JsonObject response = getJsonObject (getRequest (false , url , starter -> {
150158 starter .setHeader ("Content-Type" , "application/x-www-form-urlencoded" )
151159 .setHeader ("Authorization" , "Basic " + Base64 .getEncoder ().encodeToString ((DestinyAPI .getClientId () + ":" + DestinyAPI .getClientSecret ()).getBytes ()))
152160 .POST (HttpRequest .BodyPublishers .ofString (requestBody ));
153161
154162 return starter ;
155- }))) ;
163+ }));
156164
157165 if (response .has ("error_description" ) && response .get ("error_description" ).getAsString ().equals ("ApplicationTokenKeyIdDoesNotExist" )) {
158166 throw new InvalidConditionException ("The refresh token is invalid, you likely need to generate new tokens" );
@@ -182,13 +190,13 @@ public void setTokenViaAuth(String oAuthCode) throws APIException {
182190
183191 String requestBody = "grant_type=authorization_code&code=" + oAuthCode ;
184192
185- JsonObject jsonObject = getJsonObject (getStringResponse ( getRequest (false , url , starter -> {
193+ JsonObject jsonObject = getJsonObject (getRequest (false , url , starter -> {
186194 starter .setHeader ("Authorization" , "Basic " + Base64 .getEncoder ().encodeToString ((DestinyAPI .getClientId () + ":" + DestinyAPI .getClientSecret ()).getBytes ()))
187195 .setHeader ("Content-Type" , "application/x-www-form-urlencoded" )
188196 .POST (HttpRequest .BodyPublishers .ofString (requestBody ));
189197
190198 return starter ;
191- }))) ;
199+ }));
192200
193201 String accessToken = jsonObject .get ("access_token" ).getAsString ();
194202 String refreshToken = jsonObject .get ("refresh_token" ).getAsString ();
@@ -198,18 +206,41 @@ public void setTokenViaAuth(String oAuthCode) throws APIException {
198206 HttpUtils .bearerToken = accessToken ;
199207 }
200208
201- private JsonObject getJsonObject (String stringResponse ) throws APIException {
209+ private JsonObject getJsonObject (HttpRequest httpRequest ) throws APIException {
210+ HttpClient httpClient = HttpClient .newHttpClient ();
211+ String responseString ;
212+
213+ try { // TODO: are we even taking advantage of async? this seems pointless to just block right away
214+ responseString = httpClient .sendAsync (httpRequest , HttpResponse .BodyHandlers .ofString ()).thenApplyAsync (HttpResponse ::body ).get ();
215+
216+ if (DestinyAPI .isDebugEnabled ()) {
217+ System .out .println (httpRequest .method () + " " + httpRequest .uri ().toString ());
218+ System .out .println (responseString );
219+ }
220+ } catch (InterruptedException | ExecutionException e ) {
221+ throw new ConnectionException (e );
222+ }
223+
202224 JsonObject jsonObject ;
203225
204226 try {
205- jsonObject = new JsonParser ().parse (stringResponse ).getAsJsonObject ();
227+ jsonObject = new JsonParser ().parse (responseString ).getAsJsonObject ();
206228 } catch (JsonSyntaxException e ) {
207229 throw new JsonParsingError (e );
208230 }
209231
210- // API Offline Check
211- if (jsonObject .has ("ErrorCode" ) && jsonObject .get ("ErrorCode" ).getAsInt () == 5 ) {
212- throw new APIOfflineException (jsonObject .get ("Message" ).getAsString ());
232+ // Check for API errors - https://bungie-net.github.io/multi/schema_Exceptions-PlatformErrorCodes.html#schema_Exceptions-PlatformErrorCodes
233+ if (jsonObject .has ("ErrorCode" )) {
234+ switch (jsonObject .get ("ErrorCode" ).getAsInt ()) { //TODO: lots of errors we could catch here
235+ case 5 : // APIOffline
236+ throw new APIOfflineException (jsonObject .get ("Message" ).getAsString ());
237+ case 99 : // WebAuthRequired
238+ throw new OAuthUnauthorizedException ("OAuth - access denied. Try authenticating." );
239+ case 2111 | 2115 : // AccessTokenHasExpired, OAuthAccessTokenExpired
240+ throw new AccessTokenExpiredException ();
241+ case 2118 : // RefreshTokenExpired -- need to reauth using oauth
242+ throw new RefreshTokenExpiredException ();
243+ }
213244 }
214245
215246 return jsonObject ;
0 commit comments