Skip to content
This repository was archived by the owner on Aug 9, 2022. It is now read-only.

Commit af8e479

Browse files
author
Taylor Caldwell
committed
Merge remote-tracking branch 'origin/master'
2 parents 4808bc4 + 2e6adad commit af8e479

4 files changed

Lines changed: 33 additions & 36 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<dependency>
2626
<groupId>com.google.code.gson</groupId>
2727
<artifactId>gson</artifactId>
28-
<version>2.3.1</version>
28+
<version>2.5</version>
2929
</dependency>
3030
</dependencies>
3131

src/main/java/riotapi/MatchApi.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ public static MatchDetail getMatch(Region region, String key, long matchId, bool
4646

4747
return matchDetail;
4848
}
49-
50-
public static MatchDetail getMatchForTournament(Region region, String key, long matchId, String tournamentCode, boolean includeTimeline) throws RiotApiException {
49+
50+
public static MatchDetail getMatchForTournament(Region region, String key, long matchId, String tournamentCode, boolean includeTimeline)
51+
throws RiotApiException {
5152
String url = region.getEndpoint() + VERSION + "match/for-tournament/" + matchId + "?tournamentCode=" + tournamentCode + "&api_key=" + key;
5253
if (includeTimeline) {
5354
url += "&includeTimeline=" + includeTimeline;
5455
}
55-
56+
5657
MatchDetail matchDetail = null;
5758
try {
5859
matchDetail = new Gson().fromJson(Request.sendGet(url, null), MatchDetail.class);
@@ -65,7 +66,7 @@ public static MatchDetail getMatchForTournament(Region region, String key, long
6566

6667
return matchDetail;
6768
}
68-
69+
6970
public static List<Long> getMatchesByTournament(Region region, String key, String tournamentCode) throws RiotApiException {
7071
String url = region.getEndpoint() + VERSION + "match/by-tournament/" + tournamentCode + "/ids?api_key=" + key;
7172

src/main/java/riotapi/Request.java

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.io.BufferedReader;
44
import java.io.DataOutputStream;
55
import java.io.IOException;
6-
import java.io.InputStream;
76
import java.io.InputStreamReader;
87
import java.net.HttpURLConnection;
98
import java.net.URL;
@@ -12,22 +11,24 @@
1211

1312
public 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
}

src/main/java/riotapi/RiotApi.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2829,7 +2829,7 @@ public MatchDetail getMatchForTournament(Region region, long matchId, String tou
28292829
public MatchDetail getMatchForTournament(long matchId, String tournamentCode, boolean includeTimeline) throws RiotApiException {
28302830
return getMatchForTournament(getRegion(), matchId, tournamentCode, includeTimeline);
28312831
}
2832-
2832+
28332833
/**
28342834
* Retrieve match by match ID and tournament code.
28352835
*
@@ -2865,7 +2865,7 @@ public MatchDetail getMatchForTournament(Region region, long matchId, String tou
28652865
* if the API returns an error or unparsable result
28662866
*/
28672867
public MatchDetail getMatchForTournament(long matchId, String tournamentCode) throws RiotApiException {
2868-
return getMatchForTournament(getRegion(), matchId, tournamentCode, false);
2868+
return getMatchForTournament(getRegion(), matchId, tournamentCode);
28692869
}
28702870

28712871
/**

0 commit comments

Comments
 (0)