-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTBA.java
More file actions
112 lines (91 loc) · 3.65 KB
/
TBA.java
File metadata and controls
112 lines (91 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import java.io.*;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.*;
import org.apache.http.util.EntityUtils;
import org.json.*;
// This class processes requests to and from The Blue Alliance API
public class TBA {
// The base URL from which all calls are added to the end
private final static String baseURL = "http://www.thebluealliance.com/api/v3";
// The team key for team 2264, Wayzata Robotics
private final static String homeTeamKey = "frc2264";
// The event key for the 2018 Duluth, Minnesota regional
private final static String eventKey = "2018mndu";
// The year of the current competition
private final static int currentYear = 2018;
// The header that communicates the user to the API
private final static String userAgent = "test-scout";
// The authentication key that allows the application to interact with the API
private final static String authKey = "YXwa7Fm6N0mks7XhnRMIVnJzsjE3frXe30GZAMp5r3rDmhdLFcZjsFkFTpxxRUtR";
// Returns the JSONObject returned by the call to the API
public static JSONObject getJSON(String call) {
JSONObject json = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
// Readies a request to be sent to the API
HttpGet httpGet = new HttpGet(baseURL + call);
httpGet.addHeader("User-Agent", userAgent);
httpGet.addHeader("X-TBA-Auth-Key", authKey);
try {
// Executes the request
response = httpClient.execute(httpGet);
// Converts the response to a JSONObject
String jsonString = EntityUtils.toString(response.getEntity());
json = new JSONObject(jsonString);
} catch(ClientProtocolException e) {
System.err.println("ClientProtocolException when executing http request to " + baseURL + call);
e.printStackTrace();
} catch(IOException e) {
System.err.println("IOException when executing http request to " + baseURL + call);
e.printStackTrace();
} finally {
try {
httpClient.close();
response.close();
} catch(IOException e) {
System.out.println("IOException when closing " + httpClient.getClass() +" and "+ response.getClass());
e.printStackTrace();
}
}
return json;
}
public static String getString(String call) {
String jsonString = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
// Readies a request to be sent to the API
HttpGet httpGet = new HttpGet(baseURL + call);
httpGet.addHeader("User-Agent", userAgent);
httpGet.addHeader("X-TBA-Auth-Key", authKey);
try {
// Executes the request
response = httpClient.execute(httpGet);
jsonString = EntityUtils.toString(response.getEntity());
} catch(ClientProtocolException e) {
System.err.println("ClientProtocolException when executing http request to " + baseURL + call);
e.printStackTrace();
} catch(IOException e) {
System.err.println("IOException when executing http request to " + baseURL + call);
e.printStackTrace();
} finally {
try {
httpClient.close();
response.close();
} catch(IOException e) {
System.out.println("IOException when closing " + httpClient.getClass() +" and "+ response.getClass());
e.printStackTrace();
}
}
return jsonString;
}
public static String getMatch(int matchNum) {
return String.format("/match/%1$s_qm%2$d", eventKey, matchNum);
}
public static String getTeamStatus(int teamNumber) {
return String.format("/team/frc%1$d/events/%2$d/statuses", teamNumber, currentYear);
}
public static String getTeamMatches(int teamNumber) {
return String.format("/team/frc%1$d/event/%2$s/matches", teamNumber, eventKey);
}
}