Add generic error handling for API Football

This commit is contained in:
2021-01-10 20:38:11 +01:00
parent dd9c8cc0af
commit 87c0383db8
2 changed files with 37 additions and 21 deletions

View File

@@ -8,17 +8,13 @@ import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.json.simple.JSONArray; import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.*; import java.io.*;
import java.util.HashSet; import java.util.HashSet;
public class APIFootballUpdater { 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() { public APIFootballUpdater() {
} }
@@ -26,10 +22,8 @@ public class APIFootballUpdater {
public void updateFixtures(int league) throws Exception { 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.exceededLimitDayError)) { S3Provider prov = new S3Provider();
S3Provider prov = new S3Provider(); prov.writeFixturesToS3(league, content);
prov.writeFixturesToS3(league, content);
}
} }
public void updateAllFixtures() throws Exception { public void updateAllFixtures() throws Exception {
@@ -95,18 +89,7 @@ public class APIFootballUpdater {
result.append(line); result.append(line);
} }
if (result.toString().equals(wrongEndpointError)) { checkErrors(requestUrl, result.toString());
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);
}
return result.toString(); return result.toString();
} }
@@ -120,4 +103,25 @@ public class APIFootballUpdater {
} }
return leagues; 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;
}
} }

View File

@@ -4,6 +4,18 @@ import org.junit.Test;
public class APIFootballUpdaterTest { 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 @Test
public void updateFixturesTest() throws Exception { public void updateFixturesTest() throws Exception {
APIFootballUpdater updater = new APIFootballUpdater(); APIFootballUpdater updater = new APIFootballUpdater();