summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-04-24 01:35:22 +0200
committerPeter Wu <peter@lekensteyn.nl>2014-04-24 01:35:22 +0200
commitccb4ba6f9df7dc018719f5f1b407386bfe6ed172 (patch)
treee31ff838f333ffb1044a2f807ef75a6349796bf2
parent5bb859152e5d9f160baf588e1555e79a4617fa8c (diff)
downloadTwitterDataAnalytics-ccb4ba6f9df7dc018719f5f1b407386bfe6ed172.tar.gz
Extend Requester interface with a way to ignore errors
-rw-r--r--src/mining/AbstractRequester.java12
-rw-r--r--src/mining/Requester.java20
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;
}