@@ -36,119 +36,53 @@ public class HttpUtils {
3636 * Send a GET url request to the url provided, returns a JsonObject of the response
3737 */
3838 public JsonObject urlRequestGET (String url ) {
39- HttpClient client = HttpClient .newHttpClient ();
40- HttpRequest request = HttpRequest .newBuilder ()
41- .uri (URI .create (url ))
42- .timeout (Duration .ofMinutes (2 ))
43- .header ("X-API-KEY" , apiKey )
44- .GET ()
45- .build ();
46- CompletableFuture <String > response = client .sendAsync (request , HttpResponse .BodyHandlers .ofString ()).thenApplyAsync (HttpResponse ::body );
47- JsonElement parse = null ;
48- JsonObject jsonObject = null ;
49- try {
50- String responseString = response .get ();
51- if (DestinyAPI .isDebugEnabled ()) {
52- System .out .println (responseString );
53- }
54- parse = new JsonParser ().parse (response .get ()); // Parse response to JSON
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 ) {
61- e .printStackTrace ();
62- return null ;
63- }
64- return jsonObject ;
65- }
66-
67- public Object urlRequestGETstring (String url ) throws ExecutionException , InterruptedException {
68- HttpClient client = HttpClient .newHttpClient ();
69- HttpRequest request = HttpRequest .newBuilder ()
70- .uri (URI .create (url ))
71- .timeout (Duration .ofMinutes (1 ))
72- .header ("X-API-KEY" , apiKey )
73- .GET ()
74- .build ();
75- HttpResponse <String > response = client .sendAsync (request , HttpResponse .BodyHandlers .ofString ()).get ();
76- return response ;
39+ return getJsonObject (getStringResponse (getRequest (true , url , starter -> {
40+ starter .GET ();
41+ return starter ;
42+ })));
7743 }
7844
7945 public JsonObject urlRequestGETOauth (String url ) {
8046 setTokenViaRefresh ();
81-
82- HttpClient client = HttpClient .newHttpClient ();
83- HttpRequest request = HttpRequest .newBuilder ()
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 ();
90- CompletableFuture <String > response = client .sendAsync (request , HttpResponse .BodyHandlers .ofString ()).thenApplyAsync (HttpResponse ::body );
91- JsonElement parse = null ;
92- try {
93- checkFor401 (response .get ());
94- parse = new JsonParser ().parse (response .get ());
95- } catch (InterruptedException | ExecutionException e ) {
96- e .printStackTrace ();
97- }
98- return parse .getAsJsonObject ();
47+ return getJsonObject (getStringResponse (getRequest (true , url , starter -> {
48+ starter .GET ()
49+ .setHeader ("Authorization" , "Bearer " + HttpUtils .bearerToken );
50+ return starter ;
51+ })));
9952 }
10053
10154 public String urlRequestPOST (String url , String body ) {
10255 if (body .isEmpty ()) { body = "{\" message\" : \" \" ,}" ; }
103- HttpClient client = HttpClient .newHttpClient ();
104- HttpRequest request = HttpRequest .newBuilder ()
105- .uri (URI .create (url ))
106- .timeout (Duration .ofMinutes (1 ))
107- .setHeader ("X-API-KEY" , apiKey )
108- .setHeader ("Content-Type" , "application/json" )
109- .POST (HttpRequest .BodyPublishers .ofString (body ))
110- .build ();
111- CompletableFuture <String > response = client .sendAsync (request , HttpResponse .BodyHandlers .ofString ()).thenApplyAsync (HttpResponse ::body );
112- try {
113- return response .get ();
114- } catch (InterruptedException | ExecutionException e ) {
115- e .printStackTrace ();
116- }
117- return null ;
56+ String finalBody = body ;
57+ return getStringResponse (getRequest (true , url , starter -> {
58+ starter .setHeader ("Content-Type" , "application/json" )
59+ .POST (HttpRequest .BodyPublishers .ofString (finalBody ));
60+
61+ return starter ;
62+ }));
11863 }
11964
12065 public String urlRequestPOSTOauth (String url , String body ) {
12166 setTokenViaRefresh ();
122-
12367 if (body .isEmpty ()) { body = "{\" message\" : \" \" ,}" ; }
124- HttpClient client = HttpClient .newHttpClient ();
125- HttpRequest request = HttpRequest .newBuilder ()
126- .uri (URI .create (url ))
127- .timeout (Duration .ofMinutes (1 ))
128- .setHeader ("X-API-KEY" , apiKey )
129- .setHeader ("Authorization" , "Bearer " + HttpUtils .bearerToken )
130- .setHeader ("Content-Type" , "application/json" )
131- .POST (HttpRequest .BodyPublishers .ofString (body ))
132- .build ();
133- CompletableFuture <String > response = client .sendAsync (request , HttpResponse .BodyHandlers .ofString ()).thenApplyAsync (HttpResponse ::body );
134- try {
135- checkFor401 (response .get ());
136- return response .get ();
137- } catch (InterruptedException | ExecutionException e ) {
138- e .printStackTrace ();
139- }
140- return null ;
68+
69+ String finalBody = body ;
70+ return getStringResponse (getRequest (true , url , starter -> {
71+ starter .setHeader ("Authorization" , "Bearer " + HttpUtils .bearerToken )
72+ .setHeader ("Content-Type" , "application/json" )
73+ .POST (HttpRequest .BodyPublishers .ofString (finalBody ));
74+
75+ return starter ;
76+ }));
14177 }
14278
79+ /**
80+ * Make a request to the Bungie manifest to reveal information about a hash-identified item
81+ */
14382 public JsonObject manifestGET (ManifestEntityTypes entityType , String hashIdentifier ) {
14483 return urlRequestGET ("https://www.bungie.net/Platform/Destiny2/Manifest/" + entityType .getBungieEntityValue () + "/" + hashIdentifier + "/" );
14584 }
14685
147- public String generateLineGraph () {
148- String body = "{\" chart\" : {\" type\" : \" line\" , \" data\" : {\" labels\" : [\" Hello\" , \" World\" ], \" datasets\" : [{\" label\" : \" Foo\" , \" data\" : [1, 2]}]}}}" ;
149- return urlRequestPOST ("https://quickchart.io/chart/create" , body );
150- }
151-
15286 /**
15387 * Gets an access token using the refresh token in storage and replaces the old refresh token with the new one
15488 *
@@ -159,51 +93,41 @@ public String setTokenViaRefresh() {
15993
16094 String requestBody = "grant_type=refresh_token&refresh_token=" + DestinyAPI .getRefreshToken ();
16195
162- HttpClient client = HttpClient .newHttpClient ();
163- HttpRequest request = HttpRequest .newBuilder ()
164- .uri (URI .create (url ))
165- .timeout (Duration .ofMinutes (1 ))
166- .setHeader ("Content-Type" , "application/x-www-form-urlencoded" )
167- .setHeader ("Authorization" , "Basic " + Base64 .getEncoder ().encodeToString ((DestinyAPI .getClientId () + ":" + DestinyAPI .getClientSecret ()).getBytes ()))
168- .POST (HttpRequest .BodyPublishers .ofString (requestBody ))
169- .build ();
170- CompletableFuture <String > response = client .sendAsync (request , HttpResponse .BodyHandlers .ofString ()).thenApplyAsync (HttpResponse ::body );
171- JsonElement parse = null ;
172- try {
173- parse = new JsonParser ().parse (response .get ());
174- } catch (InterruptedException | ExecutionException e ) {
175- e .printStackTrace ();
176- }
177- String at = parse .getAsJsonObject ().get ("access_token" ).getAsString ();
178- String rt = parse .getAsJsonObject ().get ("refresh_token" ).getAsString ();
96+ JsonObject response = getJsonObject (getStringResponse (getRequest (false , url , starter -> {
97+ starter .setHeader ("Content-Type" , "application/x-www-form-urlencoded" )
98+ .setHeader ("Authorization" , "Basic " + Base64 .getEncoder ().encodeToString ((DestinyAPI .getClientId () + ":" + DestinyAPI .getClientSecret ()).getBytes ()))
99+ .POST (HttpRequest .BodyPublishers .ofString (requestBody ));
100+
101+ return starter ;
102+ })));
103+
104+ String at = response .get ("access_token" ).getAsString ();
105+ String rt = response .get ("refresh_token" ).getAsString ();
179106 bearerToken = at ;
180107 new DestinyAPI ().setAccessToken (at ).setRefreshToken (rt );
181108
182109 return at ;
183110 }
184111
185112 public void setTokenViaAuth () {
113+ setTokenViaAuth (DestinyAPI .getOauthCode ());
114+ }
115+
116+ public void setTokenViaAuth (String oAuthCode ) {
186117 String url = "https://www.bungie.net/Platform/App/OAuth/Token/" ;
187118
188- String requestBody = "grant_type=authorization_code&code=" + DestinyAPI .getOauthCode ();
189-
190- HttpClient client = HttpClient .newHttpClient ();
191- HttpRequest request = HttpRequest .newBuilder ()
192- .uri (URI .create (url ))
193- .timeout (Duration .ofMinutes (1 ))
194- .setHeader ("Authorization" , "Basic " + Base64 .getEncoder ().encodeToString ((DestinyAPI .getClientId () + ":" + DestinyAPI .getClientSecret ()).getBytes ()))
195- .setHeader ("Content-Type" , "application/x-www-form-urlencoded" )
196- .POST (HttpRequest .BodyPublishers .ofString (requestBody ))
197- .build ();
198- CompletableFuture <String > response = client .sendAsync (request , HttpResponse .BodyHandlers .ofString ()).thenApplyAsync (HttpResponse ::body );
199- JsonElement parse = null ;
200- try {
201- parse = new JsonParser ().parse (response .get ());
202- } catch (InterruptedException | ExecutionException e ) {
203- e .printStackTrace ();
204- }
205- String accessToken = parse .getAsJsonObject ().get ("access_token" ).getAsString ();
206- String refreshToken = parse .getAsJsonObject ().get ("refresh_token" ).getAsString ();
119+ String requestBody = "grant_type=authorization_code&code=" + oAuthCode ;
120+
121+ JsonObject jsonObject = getJsonObject (getStringResponse (getRequest (false , url , starter -> {
122+ starter .setHeader ("Authorization" , "Basic " + Base64 .getEncoder ().encodeToString ((DestinyAPI .getClientId () + ":" + DestinyAPI .getClientSecret ()).getBytes ()))
123+ .setHeader ("Content-Type" , "application/x-www-form-urlencoded" )
124+ .POST (HttpRequest .BodyPublishers .ofString (requestBody ));
125+
126+ return starter ;
127+ })));
128+
129+ String accessToken = jsonObject .get ("access_token" ).getAsString ();
130+ String refreshToken = jsonObject .get ("refresh_token" ).getAsString ();
207131
208132 new DestinyAPI ().setAccessToken (accessToken ).setRefreshToken (refreshToken );
209133
@@ -224,18 +148,44 @@ public boolean checkFor401(String input) {
224148 return false ;
225149 }
226150
151+ private JsonObject getJsonObject (String stringResponse ) {
152+ JsonObject jsonObject = new JsonParser ().parse (stringResponse ).getAsJsonObject ();
153+
154+ if (jsonObject .has ("ErrorCode" ) && jsonObject .get ("ErrorCode" ).getAsInt () == 5 ) {
155+ try {
156+ throw new APIOfflineException (jsonObject .get ("Message" ).getAsString ());
157+ } catch (APIOfflineException exception ) {
158+ exception .printStackTrace ();
159+ }
160+ }
161+ return jsonObject ;
162+ }
163+
227164 private String getStringResponse (HttpRequest httpRequest ) {
228165 HttpClient httpClient = HttpClient .newHttpClient ();
229166 try {
230- return httpClient .sendAsync (httpRequest , HttpResponse .BodyHandlers .ofString ()).thenApplyAsync (HttpResponse ::body ).get ();
167+ String responseString = httpClient .sendAsync (httpRequest , HttpResponse .BodyHandlers .ofString ()).thenApplyAsync (HttpResponse ::body ).get ();
168+
169+ if (DestinyAPI .isDebugEnabled ()) {
170+ System .out .println (responseString );
171+ }
172+ return responseString ;
231173 } catch (InterruptedException | ExecutionException e ) {
232174 e .printStackTrace ();
233175 }
234176
235177 return null ;
236178 }
237179
238- private HttpRequest getRequest (HttpRequestModifier httpRequestModifier ) {
239- return httpRequestModifier .modifyRequest (HttpRequest .newBuilder ().build ());
180+ private HttpRequest getRequest (boolean standardRequest , String url , HttpRequestModifier httpRequestModifier ) {
181+ HttpRequest .Builder builder = httpRequestModifier .modifyRequest (HttpRequest .newBuilder ());
182+
183+ builder .uri (URI .create (url )).timeout (Duration .ofMinutes (2 ));
184+
185+ if (standardRequest ) {
186+ builder .setHeader ("X-API-KEY" , apiKey );
187+ }
188+
189+ return builder .build ();
240190 }
241191}
0 commit comments