Skip to content

Commit 611869d

Browse files
committed
Added custom exceptions and CTA interface
1 parent f0110b8 commit 611869d

File tree

13 files changed

+803
-110
lines changed

13 files changed

+803
-110
lines changed

src/main/java/main/CTBA.java

Lines changed: 607 additions & 0 deletions
Large diffs are not rendered by default.

src/main/java/main/TBA.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import models.standard.Match;
1414
import models.standard.Team;
1515
import requests.*;
16+
import utils.Constants;
1617

1718
/**
1819
* This is the main interface for the API, let's talk about that.
@@ -45,6 +46,15 @@ public TBA() {
4546
tr = new TeamRequest();
4647
}
4748

49+
/**
50+
* Sets the authentication token for the API, required for all calls!
51+
* Obtain an auth token from your account page on thebluealliance.com
52+
* @param authToken the auth token to set
53+
*/
54+
public static void setAuthToken(String authToken) {
55+
Constants.AUTH_TOKEN = authToken;
56+
}
57+
4858
/**
4959
* Mirror of: /district/{district_key}/teams
5060
*

src/main/java/models/standard/Event.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class Event extends SEvent {
3333
/**
3434
* The FIRST internal Event ID, used to link to the event on the FRC webpage.
3535
*/
36-
private long firstEventID;
36+
private String firstEventID;
3737
/**
3838
* The TBA Event key that represents the event’s parent. Used to link back to the event from a division event. It is also the inverse relation of divison_keys.
3939
*/

src/main/java/requests/DistrictRequest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import utils.IO;
1010
import utils.Parser;
1111
import utils.Utils;
12+
import utils.exceptions.DataNotFoundException;
1213

1314
/**
1415
* In an attempt to keep this API organized, if you look at the blue alliance v3 documentation, all calls that start with /district/ or /districts/
@@ -30,7 +31,7 @@ public class DistrictRequest extends Parser {
3031
*/
3132
public Team[] getDistrictTeams(String districtKey) {
3233
JSONArray teams = (JSONArray) IO.doRequest("district/"+districtKey+"/teams");
33-
if(teams == null) return null;
34+
if(teams == null) throw new DataNotFoundException("Couldn't find any teams in district with key: "+districtKey);
3435
Team[] toGet = new Team[teams.size()];
3536
for(int i = 0; i < teams.size(); i++) toGet[i] = parseTeam(teams.get(i));
3637
return toGet;
@@ -44,7 +45,7 @@ public Team[] getDistrictTeams(String districtKey) {
4445
*/
4546
public STeam[] getDistrictSTeams(String districtKey) {
4647
JSONArray teams = (JSONArray) IO.doRequest("district/"+districtKey+"/teams/simple");
47-
if(teams == null) return null;
48+
if(teams == null) throw new DataNotFoundException("Couldn't find any simple teams in district with key: "+districtKey);
4849
STeam[] toGet = new STeam[teams.size()];
4950
for(int i = 0; i < teams.size(); i++) toGet[i] = parseSTeam(teams.get(i));
5051
return toGet;
@@ -59,6 +60,7 @@ public STeam[] getDistrictSTeams(String districtKey) {
5960
*/
6061
public String[] getDistrictTeamKeys(String districtKey) {
6162
JSONArray keys = (JSONArray) IO.doRequest("district/"+districtKey+"/teams/keys");
63+
if(keys == null) throw new DataNotFoundException("Couldn't find any team keys in district with key: "+districtKey);
6264
return Utils.jsonArrayToStringArray(keys);
6365
}
6466

@@ -70,7 +72,7 @@ public String[] getDistrictTeamKeys(String districtKey) {
7072
*/
7173
public Event[] getDistrictEvents(String districtKey) {
7274
JSONArray teams = (JSONArray) IO.doRequest("district/"+districtKey+"/events");
73-
if(teams == null) return null;
75+
if(teams == null) throw new DataNotFoundException("Couldn't find any events in district with key: "+districtKey);
7476
Event[] toGet = new Event[teams.size()];
7577
for(int i = 0; i < teams.size(); i++) toGet[i] = parseEvent(teams.get(i));
7678
return toGet;
@@ -84,7 +86,7 @@ public Event[] getDistrictEvents(String districtKey) {
8486
*/
8587
public SEvent[] getDistrictSEvents(String districtKey) {
8688
JSONArray teams = (JSONArray) IO.doRequest("district/"+districtKey+"/events/simple");
87-
if(teams == null) return null;
89+
if(teams == null) throw new DataNotFoundException("Couldn't find any simple events in district with key: "+districtKey);
8890
SEvent[] toGet = new SEvent[teams.size()];
8991
for(int i = 0; i < teams.size(); i++) toGet[i] = parseSEvent(teams.get(i));
9092
return toGet;
@@ -99,6 +101,7 @@ public SEvent[] getDistrictSEvents(String districtKey) {
99101
*/
100102
public String[] getDistrictEventKeys(String districtKey) {
101103
JSONArray keys = (JSONArray) IO.doRequest("district/"+districtKey+"/events/keys");
104+
if(keys == null) throw new DataNotFoundException("Couldn't find any event keys in district with key: "+districtKey);
102105
return Utils.jsonArrayToStringArray(keys);
103106
}
104107

@@ -111,7 +114,7 @@ public String[] getDistrictEventKeys(String districtKey) {
111114
*/
112115
public District[] getDistricts(int year) {
113116
JSONArray districts = (JSONArray) IO.doRequest("districts/"+year);
114-
if(districts == null) return null;
117+
if(districts == null) throw new DataNotFoundException("Couldn't find any districts in year: "+year);
115118
District[] toReturn = new District[districts.size()];
116119
for(int i = 0; i < districts.size(); i++) toReturn[i] = parseDistrict(districts.get(i));
117120
return toReturn;

src/main/java/requests/EventRequest.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import utils.IO;
1313
import utils.Parser;
1414
import utils.Utils;
15+
import utils.exceptions.DataNotFoundException;
16+
17+
import javax.xml.crypto.Data;
1518

1619
/**
1720
* In an attempt to keep this API organized, if you look at the blue alliance v3 documentation, all calls that start with /events/ or /event/
@@ -37,7 +40,7 @@ public class EventRequest extends Parser {
3740
*/
3841
public Team[] getEventTeams(String eventKey) {
3942
JSONArray teams = (JSONArray) IO.doRequest("event/"+eventKey+"/teams");
40-
if(teams == null) return null;
43+
if(teams == null) throw new DataNotFoundException("Couldn't find any teams in event with key: "+eventKey);
4144
Team[] toGet = new Team[teams.size()];
4245
for(int i = 0; i < teams.size(); i++) toGet[i] = parseTeam(teams.get(i));
4346
return toGet;
@@ -52,7 +55,7 @@ public Team[] getEventTeams(String eventKey) {
5255
*/
5356
public STeam[] getSEventTeams(String eventKey) {
5457
JSONArray teams = (JSONArray) IO.doRequest("event/"+eventKey+"/teams/simple");
55-
if(teams == null) return null;
58+
if(teams == null) throw new DataNotFoundException("Couldn't find any simple teams in event with key: "+eventKey);
5659
STeam[] toGet = new STeam[teams.size()];
5760
for(int i = 0; i < teams.size(); i++) toGet[i] = parseSTeam(teams.get(i));
5861
return toGet;
@@ -67,6 +70,7 @@ public STeam[] getSEventTeams(String eventKey) {
6770
*/
6871
public String[] getTeamKeys(String eventKey) {
6972
JSONArray keys = (JSONArray) IO.doRequest("event/"+eventKey+"/teams/keys");
73+
if(keys == null) throw new DataNotFoundException("Couldn't find any team keys in event with key: "+eventKey);
7074
return Utils.jsonArrayToStringArray(keys);
7175
}
7276

@@ -79,7 +83,7 @@ public String[] getTeamKeys(String eventKey) {
7983
*/
8084
public Event[] getEvents(int year) {
8185
JSONArray events = (JSONArray) IO.doRequest("events/"+year);
82-
if(events == null) return null;
86+
if(events == null) throw new DataNotFoundException("Couldn't find any events in year: "+year);
8387
Event[] toGet = new Event[events.size()];
8488
for(int i = 0; i < events.size(); i++) toGet[i] = parseEvent(events.get(i));
8589
return toGet;
@@ -93,8 +97,8 @@ public Event[] getEvents(int year) {
9397
* @return SEvent[] containing all the events in the specified year
9498
*/
9599
public SEvent[] getSEvents(int year) {
96-
JSONArray events = (JSONArray) IO.doRequest("events/"+"year/simple");
97-
if(events == null) return null;
100+
JSONArray events = (JSONArray) IO.doRequest("events/"+year+"/simple");
101+
if(events == null) throw new DataNotFoundException("Couldn't find any simple events in year: "+year);
98102
SEvent[] toGet = new SEvent[events.size()];
99103
for(int i = 0; i < events.size(); i++) toGet[i] = parseSEvent(events.get(i));
100104
return toGet;
@@ -109,6 +113,7 @@ public SEvent[] getSEvents(int year) {
109113
*/
110114
public String[] getEventKeys(int year) {
111115
JSONArray keys = (JSONArray) IO.doRequest("events/"+year+"/keys");
116+
if(keys == null) throw new DataNotFoundException("Couldn't find any event keys in year: "+year);
112117
return Utils.jsonArrayToStringArray(keys);
113118
}
114119

@@ -120,7 +125,9 @@ public String[] getEventKeys(int year) {
120125
* @return Event model representing the event associated with the event key
121126
*/
122127
public Event getEvent(String eventKey) {
123-
return parseEvent(IO.doRequest("event/"+eventKey));
128+
Event event = parseEvent(IO.doRequest("event/"+eventKey));
129+
if(event == null) throw new DataNotFoundException("No event found with key: "+eventKey);
130+
return event;
124131
}
125132

126133
/**
@@ -131,7 +138,9 @@ public Event getEvent(String eventKey) {
131138
* @return Event model representing the event associated with the event key
132139
*/
133140
public SEvent getSEvent(String eventKey) {
134-
return parseSEvent(IO.doRequest("event/"+eventKey+"/simple"));
141+
SEvent event = parseSEvent(IO.doRequest("event/"+eventKey+"/simple"));
142+
if(event == null) throw new DataNotFoundException("No simple event found with key: "+eventKey);
143+
return event;
135144
}
136145

137146

@@ -143,7 +152,9 @@ public SEvent getSEvent(String eventKey) {
143152
* @return EventOPR[] containing an EventOPR for each team
144153
*/
145154
public EventOPR[] getOprs(String eventKey) {
146-
return parseOPRs(IO.doRequest("event/"+eventKey+"/oprs"));
155+
EventOPR[] oprs = parseOPRs(IO.doRequest("event/"+eventKey+"/oprs"));
156+
if(oprs == null) throw new DataNotFoundException("No oprs found for event with key: "+eventKey);
157+
return oprs;
147158
}
148159

149160
/**
@@ -156,7 +167,9 @@ public EventOPR[] getOprs(String eventKey) {
156167
* @return JSON String containing prediction information
157168
*/
158169
public String getPredictions(String eventKey) {
159-
return (String) IO.doRequest("event/"+eventKey+"predictions");
170+
String s = (String) IO.doRequest("event/"+eventKey+"predictions");
171+
if(s == null) throw new DataNotFoundException("No predictions found for event with key: "+eventKey);
172+
return s;
160173
}
161174

162175
/**
@@ -168,7 +181,7 @@ public String getPredictions(String eventKey) {
168181
*/
169182
public Match[] getMatches(String eventKey) {
170183
JSONArray matches = (JSONArray) IO.doRequest("event/"+eventKey+"/matches");
171-
if(matches == null) return null;
184+
if(matches == null) throw new DataNotFoundException("No matches found for event with key: "+eventKey);
172185
Match[] toGet = new Match[matches.size()];
173186
for(int i = 0; i < matches.size(); i++) toGet[i] = parseMatch(matches.get(i));
174187
return toGet;
@@ -182,8 +195,8 @@ public Match[] getMatches(String eventKey) {
182195
* @return Match[] containing a Match object for each match in the specified event
183196
*/
184197
public SMatch[] getSMatches(String eventKey) {
185-
JSONArray matches = (JSONArray) IO.doRequest("events/"+"year/simple");
186-
if(matches == null) return null;
198+
JSONArray matches = (JSONArray) IO.doRequest("event/"+eventKey+"/matches/simple");
199+
if(matches == null) throw new DataNotFoundException("No simple matches found for event with key: "+eventKey);
187200
SMatch[] toGet = new SMatch[matches.size()];
188201
for(int i = 0; i < matches.size(); i++) toGet[i] = parseSMatch(matches.get(i));
189202
return toGet;
@@ -198,6 +211,7 @@ public SMatch[] getSMatches(String eventKey) {
198211
*/
199212
public String[] getMatchKeys(String eventKey) {
200213
JSONArray keys = (JSONArray) IO.doRequest("event/"+eventKey+"/matches/keys");
214+
if(keys == null) throw new DataNotFoundException("No match keys found for event with key: "+eventKey);
201215
return Utils.jsonArrayToStringArray(keys);
202216
}
203217

@@ -210,7 +224,7 @@ public String[] getMatchKeys(String eventKey) {
210224
*/
211225
public Award[] getEventAwards(String eventKey) {
212226
JSONArray array = (JSONArray) IO.doRequest("event/"+eventKey+"/awards");
213-
if(array == null) return null;
227+
if(array == null) throw new DataNotFoundException("No awards found for event with key: "+eventKey);
214228
Award[] toReturn = new Award[array.size()];
215229
for(int i = 0; i < array.size(); i++) toReturn[i] = parseAward(array.get(i));
216230
return toReturn;

src/main/java/requests/MatchRequest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import models.standard.Match;
55
import utils.IO;
66
import utils.Parser;
7+
import utils.exceptions.DataNotFoundException;
78

89
/**
910
* In an attempt to keep this API organized, if you look at the blue alliance v3 documentation, all calls that start with /match/
@@ -22,7 +23,9 @@ public class MatchRequest extends Parser {
2223
* @return Match object represented by the match key
2324
*/
2425
public Match getMatch(String matchKey) {
25-
return parseMatch(IO.doRequest("match/"+matchKey));
26+
Match m = parseMatch(IO.doRequest("match/"+matchKey));
27+
if(m == null) throw new DataNotFoundException("No match found with key: "+matchKey);
28+
return m;
2629
}
2730

2831
/**
@@ -33,7 +36,9 @@ public Match getMatch(String matchKey) {
3336
* @return SMatch object represented by the match key (simple model)
3437
*/
3538
public SMatch getSMatch(String matchKey) {
36-
return parseSMatch(IO.doRequest("match/"+matchKey));
39+
SMatch m = parseSMatch(IO.doRequest("match/"+matchKey+"/simple"));
40+
if(m == null) throw new DataNotFoundException("No match found with key: "+matchKey);
41+
return m;
3742
}
3843

3944

0 commit comments

Comments
 (0)