diff --git a/src/main/java/de/jeyp91/apifootball/APIFootballUpdater.java b/src/main/java/de/jeyp91/apifootball/APIFootballUpdater.java index 7b1196f..acadeb9 100644 --- a/src/main/java/de/jeyp91/apifootball/APIFootballUpdater.java +++ b/src/main/java/de/jeyp91/apifootball/APIFootballUpdater.java @@ -8,17 +8,13 @@ import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; import org.json.simple.JSONArray; import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; import java.io.*; import java.util.HashSet; public class APIFootballUpdater { - private final String exceededLimitDayError = "{\"api\":{\"results\":0,\"error\":\"You have reached the request limit for the day\"}}"; - private final String exceededLimitMinuteError = "{\"api\":{\"results\":0,\"error\":\"Too many requests. Your rate limit is 10 requests per minute.\"}}"; - private final String exceededLimitMinuteError2 = "{\"api\":{\"results\":0,\"status\":\"Too many requests. Your rate limit is 10 requests per minute.\"}}"; - private final String wrongEndpointError = "{\"api\":{\"results\":0,\"error\":\"This endpoint does not exist or your parameters are incorrect, contact our support.\"}}"; - public APIFootballUpdater() { } @@ -26,10 +22,8 @@ public class APIFootballUpdater { 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.exceededLimitDayError)) { - S3Provider prov = new S3Provider(); - prov.writeFixturesToS3(league, content); - } + S3Provider prov = new S3Provider(); + prov.writeFixturesToS3(league, content); } public void updateAllFixtures() throws Exception { @@ -95,18 +89,7 @@ public class APIFootballUpdater { result.append(line); } - if (result.toString().equals(wrongEndpointError)) { - throw new Exception(requestUrl + " gave error:" + wrongEndpointError); - } - if (result.toString().equals(exceededLimitDayError)) { - throw new Exception(requestUrl + " gave error:" + exceededLimitDayError); - } - if (result.toString().equals(exceededLimitMinuteError)) { - throw new Exception(requestUrl + " gave error:" + exceededLimitMinuteError); - } - if (result.toString().equals(exceededLimitMinuteError2)) { - throw new Exception(requestUrl + " gave error:" + exceededLimitMinuteError2); - } + checkErrors(requestUrl, result.toString()); return result.toString(); } @@ -120,4 +103,25 @@ public class APIFootballUpdater { } return leagues; } + + public void checkErrors(String requestUrl, String result) throws Exception { + JSONObject resultObject = stringToJSONObject(result); + if(((JSONObject) resultObject.get("api")).containsKey("error")) { + String errorMessage = ((JSONObject) resultObject.get("api")).get("error").toString(); + throw new Exception(requestUrl + " returned error: '" + errorMessage + "'"); + } + } + + private JSONObject stringToJSONObject(String rawData) { + + JSONParser parser = new JSONParser(); + JSONObject data = null; + try { + data = (JSONObject) parser.parse(rawData); + } catch (Exception e) { + /* TODO */ + e.printStackTrace(); + } + return data; + } } diff --git a/src/test/java/de/jeyp91/apifootball/APIFootballUpdaterTest.java b/src/test/java/de/jeyp91/apifootball/APIFootballUpdaterTest.java index 8e50ae5..3811d70 100644 --- a/src/test/java/de/jeyp91/apifootball/APIFootballUpdaterTest.java +++ b/src/test/java/de/jeyp91/apifootball/APIFootballUpdaterTest.java @@ -4,6 +4,18 @@ import org.junit.Test; public class APIFootballUpdaterTest { + @Test + public void checkErrorsTest() { + String exceededLimitDayError = "{\"api\":{\"results\":0,\"error\":\"You have reached the request limit for the day\"}}"; + APIFootballUpdater updater = new APIFootballUpdater(); + try { + updater.checkErrors("https://test.url/api", exceededLimitDayError); + } catch (Exception e) { + System.out.println(e.getMessage()); + assert e.getMessage().equals("https://test.url/api returned error: 'You have reached the request limit for the day'"); + } + } + @Test public void updateFixturesTest() throws Exception { APIFootballUpdater updater = new APIFootballUpdater();