From ccb4ba6f9df7dc018719f5f1b407386bfe6ed172 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Thu, 24 Apr 2014 01:35:22 +0200 Subject: Extend Requester interface with a way to ignore errors --- src/mining/AbstractRequester.java | 12 +++++++++++- 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; } -- cgit v1.2.1