Add check for wrong endpoint for APIFootball

This commit is contained in:
2020-11-09 22:03:20 +01:00
parent 5474e2a4d1
commit c53511939d
5 changed files with 16 additions and 11 deletions

View File

@@ -51,6 +51,7 @@ public class App {
case "APIFootballUpdater": case "APIFootballUpdater":
APIFootballUpdater apiFootballUpdater = new APIFootballUpdater(); APIFootballUpdater apiFootballUpdater = new APIFootballUpdater();
apiFootballUpdater.updateAllFixtures(); apiFootballUpdater.updateAllFixtures();
apiFootballUpdater.updateAllRounds();
break; break;
case "MatchesListGistUpdater": case "MatchesListGistUpdater":
MatchesListForumUpdater matchesListForumUpdater = new MatchesListForumUpdater(); MatchesListForumUpdater matchesListForumUpdater = new MatchesListForumUpdater();

View File

@@ -80,7 +80,7 @@ public class APIFootballConnector {
return this.rounds.get(leagueId); 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 url = "https://v2.api-football.com/teams/league/" + league;
String content = new APIFootballUpdater().getRawData(url); String content = new APIFootballUpdater().getRawData(url);
return stringToJSONObject(content); return stringToJSONObject(content);

View File

@@ -15,12 +15,13 @@ import java.util.HashSet;
public class APIFootballUpdater { public class APIFootballUpdater {
private final String exceededLimitError = "{\"api\":{\"results\":0,\"error\":\"You have reached the request limit for the day\"}}"; 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 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 apiFootballUrl = "https://v2.api-football.com/fixtures/league/" + league + "?timezone=Europe/Berlin";
String content = getRawData(apiFootballUrl); String content = getRawData(apiFootballUrl);
if(!content.equals(this.exceededLimitError)) { if(!content.equals(this.exceededLimitError)) {
@@ -29,21 +30,21 @@ public class APIFootballUpdater {
} }
} }
public void updateAllFixtures() { public void updateAllFixtures() throws Exception {
HashSet<Integer> leagues = getLeagues(); HashSet<Integer> leagues = getLeagues();
for (Integer league : leagues) { for (Integer league : leagues) {
updateFixtures(league); updateFixtures(league);
} }
} }
public void updateAllRounds() { public void updateAllRounds() throws Exception {
HashSet<Integer> leagues = getLeagues(); HashSet<Integer> leagues = getLeagues();
for (Integer league : leagues) { for (Integer league : leagues) {
updateRounds(league); 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 apiFootballUrl = "https://v2.api-football.com/fixtures/rounds/" + league;
String content = getRawData(apiFootballUrl); String content = getRawData(apiFootballUrl);
if(!content.equals(this.exceededLimitError)) { 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(); HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(requestUrl); HttpGet request = new HttpGet(requestUrl);
@@ -94,6 +95,9 @@ public class APIFootballUpdater {
result.append(line); result.append(line);
} }
if (result.toString().equals(wrongEndpointError)) {
throw new Exception(requestUrl + " gave error:" + wrongEndpointError);
}
return result.toString(); return result.toString();
} }

View File

@@ -8,7 +8,7 @@ public class TeamIDMatcherTemplateCreator {
APIFootballConnector con; APIFootballConnector con;
JSONArray teams; JSONArray teams;
public TeamIDMatcherTemplateCreator(int season, int league) { public TeamIDMatcherTemplateCreator(int season, int league) throws Exception {
this.con = APIFootballConnector.getAPIFootballConnectorInstance(season); this.con = APIFootballConnector.getAPIFootballConnectorInstance(season);
JSONObject apiObject = con.getTeamsForLeague(league); JSONObject apiObject = con.getTeamsForLeague(league);
JSONObject teamsObject = (JSONObject) apiObject.get("api"); JSONObject teamsObject = (JSONObject) apiObject.get("api");

View File

@@ -5,19 +5,19 @@ import org.junit.Test;
public class APIFootballUpdaterTest { public class APIFootballUpdaterTest {
@Test @Test
public void updateFixturesTest() { public void updateFixturesTest() throws Exception {
APIFootballUpdater updater = new APIFootballUpdater(); APIFootballUpdater updater = new APIFootballUpdater();
// updater.updateFixtures(1240); updater.updateFixtures(1240);
} }
@Test @Test
public void updateAllFixturesTest() { public void updateAllFixturesTest() throws Exception {
APIFootballUpdater updater = new APIFootballUpdater(); APIFootballUpdater updater = new APIFootballUpdater();
// updater.updateAllFixtures(); // updater.updateAllFixtures();
} }
@Test @Test
public void updateAllRoundsTest() { public void updateAllRoundsTest() throws Exception {
APIFootballUpdater updater = new APIFootballUpdater(); APIFootballUpdater updater = new APIFootballUpdater();
// updater.updateAllRounds(); // updater.updateAllRounds();
} }