diff options
author | Peter Wu <peter@lekensteyn.nl> | 2014-04-24 01:35:22 +0200 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2014-04-24 01:35:22 +0200 |
commit | ccb4ba6f9df7dc018719f5f1b407386bfe6ed172 (patch) | |
tree | e31ff838f333ffb1044a2f807ef75a6349796bf2 | |
parent | 5bb859152e5d9f160baf588e1555e79a4617fa8c (diff) | |
download | TwitterDataAnalytics-ccb4ba6f9df7dc018719f5f1b407386bfe6ed172.tar.gz |
Extend Requester interface with a way to ignore errors
-rw-r--r-- | src/mining/AbstractRequester.java | 12 | ||||
-rw-r--r-- | src/mining/Requester.java | 20 |
2 files changed, 30 insertions, 2 deletions
diff --git a/src/mining/AbstractRequester.java b/src/mining/AbstractRequester.java index 1e66668..ff4a255 100644 --- a/src/mining/AbstractRequester.java +++ b/src/mining/AbstractRequester.java @@ -26,6 +26,16 @@ public abstract class AbstractRequester implements Requester { @Override public JSONObject getJSON(String resource) throws IOException { + return getJSON(resource, true); + } + + @Override + public JSONObject getJSONRelax(String resource) throws IOException { + return getJSON(resource, false); + } + + private JSONObject getJSON(String resource, boolean checkStatusCode) + throws IOException { HttpURLConnection conn = open(buildUrl(resource)); preconnect(conn); JSONObject resp = getResponseAsJson(conn); @@ -37,7 +47,7 @@ public abstract class AbstractRequester implements Requester { } catch (JSONException ex) { } } - if (conn.getResponseCode() != 200) { + if (checkStatusCode && conn.getResponseCode() != 200) { // TODO: print more helpful details throw new IOException("Unexpected response code"); } diff --git a/src/mining/Requester.java b/src/mining/Requester.java index df6053f..2e0e067 100644 --- a/src/mining/Requester.java +++ b/src/mining/Requester.java @@ -21,5 +21,23 @@ public interface Requester { */ public JSONObject getJSON(String resource) throws IOException; - // TODO: retry (after sleeping) on ratelimit + /** + * Almost equivalent to {@code getJSON(resource, true)}, except that an + * IOException is not thrown if the request reports an non-successful status + * code. + * + * @see Requester#getJSON(java.lang.String, boolean) + * @throws IOException on network errors or if the response could not be + * parsed into a valid JSONObject. + */ + public JSONObject getJSONRelax(String resource) throws IOException; + + /** + * Tests whether this instance can dispatch requests. + * + * @return true if the access tokens are valid, false otherwise. + * @throws java.io.IOException if the status cannot reliably be determined + * (e.g. network error, parsing error). + */ + public boolean isValid() throws IOException; } |