Add check for wrong endpoint for APIFootball
This commit is contained in:
@@ -51,6 +51,7 @@ public class App {
|
||||
case "APIFootballUpdater":
|
||||
APIFootballUpdater apiFootballUpdater = new APIFootballUpdater();
|
||||
apiFootballUpdater.updateAllFixtures();
|
||||
apiFootballUpdater.updateAllRounds();
|
||||
break;
|
||||
case "MatchesListGistUpdater":
|
||||
MatchesListForumUpdater matchesListForumUpdater = new MatchesListForumUpdater();
|
||||
|
||||
@@ -80,7 +80,7 @@ public class APIFootballConnector {
|
||||
return this.rounds.get(leagueId);
|
||||
}
|
||||
|
||||
public JSONObject getTeamsForLeague(int league) {
|
||||
public JSONObject getTeamsForLeague(int league) throws Exception {
|
||||
String url = "https://v2.api-football.com/teams/league/" + league;
|
||||
String content = new APIFootballUpdater().getRawData(url);
|
||||
return stringToJSONObject(content);
|
||||
|
||||
@@ -15,12 +15,13 @@ import java.util.HashSet;
|
||||
public class APIFootballUpdater {
|
||||
|
||||
private final String exceededLimitError = "{\"api\":{\"results\":0,\"error\":\"You have reached the request limit for the day\"}}";
|
||||
private final String wrongEndpointError = "{\"api\":{\"results\":0,\"error\":\"This endpoint does not exist or your parameters are incorrect, contact our support.\"}}";
|
||||
|
||||
public APIFootballUpdater() {
|
||||
|
||||
}
|
||||
|
||||
public void updateFixtures(int league) {
|
||||
public void updateFixtures(int league) throws Exception {
|
||||
String apiFootballUrl = "https://v2.api-football.com/fixtures/league/" + league + "?timezone=Europe/Berlin";
|
||||
String content = getRawData(apiFootballUrl);
|
||||
if(!content.equals(this.exceededLimitError)) {
|
||||
@@ -29,21 +30,21 @@ public class APIFootballUpdater {
|
||||
}
|
||||
}
|
||||
|
||||
public void updateAllFixtures() {
|
||||
public void updateAllFixtures() throws Exception {
|
||||
HashSet<Integer> leagues = getLeagues();
|
||||
for (Integer league : leagues) {
|
||||
updateFixtures(league);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateAllRounds() {
|
||||
public void updateAllRounds() throws Exception {
|
||||
HashSet<Integer> leagues = getLeagues();
|
||||
for (Integer league : leagues) {
|
||||
updateRounds(league);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateRounds(int league) {
|
||||
public void updateRounds(int league) throws Exception {
|
||||
String apiFootballUrl = "https://v2.api-football.com/fixtures/rounds/" + league;
|
||||
String content = getRawData(apiFootballUrl);
|
||||
if(!content.equals(this.exceededLimitError)) {
|
||||
@@ -52,7 +53,7 @@ public class APIFootballUpdater {
|
||||
}
|
||||
}
|
||||
|
||||
public String getRawData(String requestUrl) {
|
||||
public String getRawData(String requestUrl) throws Exception {
|
||||
|
||||
HttpClient client = HttpClientBuilder.create().build();
|
||||
HttpGet request = new HttpGet(requestUrl);
|
||||
@@ -94,6 +95,9 @@ public class APIFootballUpdater {
|
||||
|
||||
result.append(line);
|
||||
}
|
||||
if (result.toString().equals(wrongEndpointError)) {
|
||||
throw new Exception(requestUrl + " gave error:" + wrongEndpointError);
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ public class TeamIDMatcherTemplateCreator {
|
||||
APIFootballConnector con;
|
||||
JSONArray teams;
|
||||
|
||||
public TeamIDMatcherTemplateCreator(int season, int league) {
|
||||
public TeamIDMatcherTemplateCreator(int season, int league) throws Exception {
|
||||
this.con = APIFootballConnector.getAPIFootballConnectorInstance(season);
|
||||
JSONObject apiObject = con.getTeamsForLeague(league);
|
||||
JSONObject teamsObject = (JSONObject) apiObject.get("api");
|
||||
|
||||
@@ -5,19 +5,19 @@ import org.junit.Test;
|
||||
public class APIFootballUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void updateFixturesTest() {
|
||||
public void updateFixturesTest() throws Exception {
|
||||
APIFootballUpdater updater = new APIFootballUpdater();
|
||||
// updater.updateFixtures(1240);
|
||||
updater.updateFixtures(1240);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateAllFixturesTest() {
|
||||
public void updateAllFixturesTest() throws Exception {
|
||||
APIFootballUpdater updater = new APIFootballUpdater();
|
||||
// updater.updateAllFixtures();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateAllRoundsTest() {
|
||||
public void updateAllRoundsTest() throws Exception {
|
||||
APIFootballUpdater updater = new APIFootballUpdater();
|
||||
// updater.updateAllRounds();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user