33import java .io .BufferedReader ;
44import java .io .DataOutputStream ;
55import java .io .IOException ;
6- import java .io .InputStream ;
76import java .io .InputStreamReader ;
87import java .net .HttpURLConnection ;
98import java .net .URL ;
1211
1312public class Request {
1413
15- private static String execute (String method , String URL , String key , String body ) throws RiotApiException {
14+ protected enum RequestMethod {
15+ DELETE , GET , POST , PUT
16+ };
1617
18+ protected static String execute (RequestMethod method , String requestURL , String key , String body ) throws RiotApiException {
1719 HttpURLConnection connection = null ;
18-
1920 try {
20- String requestURL = URL ;
2121 URL url = new URL (requestURL );
22-
2322 connection = (HttpURLConnection ) url .openConnection ();
24- connection .setRequestMethod (method );
23+ connection .setRequestMethod (method . name () );
2524 connection .setInstanceFollowRedirects (false );
26- if (key != null ) {
25+
26+ if (key != null ) {
2727 connection .setRequestProperty ("X-Riot-Token" , key );
2828 }
29-
30- if (method .equals ("PUT" ) || method .equals ("POST" )) {
29+
30+ if (method == RequestMethod .POST || method == RequestMethod .PUT ) {
31+
3132 connection .setRequestProperty ("Content-Type" , "application/json" );
3233 connection .setDoOutput (true );
3334 DataOutputStream wr = new DataOutputStream (connection .getOutputStream ());
@@ -37,33 +38,28 @@ private static String execute(String method, String URL, String key, String body
3738 }
3839
3940 int responseCode = connection .getResponseCode ();
40-
4141 if (responseCode == 429 ) {
4242 String retryAfterString = connection .getHeaderField ("Retry-After" );
4343 String rateLimitType = connection .getHeaderField ("X-Rate-Limit-Type" );
44- if (retryAfterString != null ) {
44+ if (retryAfterString != null ) {
4545 int retryAfter = Integer .parseInt (retryAfterString );
4646 throw new RiotApiException (responseCode , retryAfter , rateLimitType );
4747 } else {
4848 throw new RiotApiException (responseCode , 0 , rateLimitType );
49- }
50- }
51-
52- else if (responseCode != 200 && responseCode != 204 ) {
49+ }
50+ } else if (responseCode != 200 && responseCode != 204 ) {
51+ System .out .println (requestURL );
5352 throw new RiotApiException (responseCode );
5453 }
55-
56- InputStream is = connection .getInputStream ();
57- BufferedReader rd = new BufferedReader (new InputStreamReader (is , "utf-8" ));
58- StringBuilder response = new StringBuilder ();
5954
55+ BufferedReader br = new BufferedReader (new InputStreamReader (connection .getInputStream (), "utf-8" ));
56+ StringBuilder response = new StringBuilder ();
6057 String line ;
61- while ((line = rd .readLine ()) != null ) {
58+ while ((line = br .readLine ()) != null ) {
6259 response .append (line );
63- response .append ('\r' );
60+ response .append (System . lineSeparator () );
6461 }
65- rd .close ();
66-
62+ br .close ();
6763 connection .disconnect ();
6864 return response .toString ();
6965 } catch (IOException ex ) {
@@ -75,19 +71,19 @@ else if (responseCode != 200 && responseCode != 204) {
7571 }
7672 }
7773 }
78-
74+
7975 // HTTP GET request
8076 protected static String sendGet (String URL , String key ) throws RiotApiException {
81- return execute (" GET" , URL , key , null );
77+ return execute (RequestMethod . GET , URL , key , null );
8278 }
83-
79+
8480 // HTTP POST request
8581 protected static String sendPost (String URL , String key , String body ) throws RiotApiException {
86- return execute (" POST" , URL , key , body );
82+ return execute (RequestMethod . POST , URL , key , body );
8783 }
88-
84+
8985 // HTTP PUT request
9086 protected static String sendPut (String URL , String key , String body ) throws RiotApiException {
91- return execute (" PUT" , URL , key , body );
87+ return execute (RequestMethod . PUT , URL , key , body );
9288 }
9389}
0 commit comments