diff options
author | Peter Wu <peter@lekensteyn.nl> | 2014-04-24 01:54:36 +0200 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2014-04-24 01:54:36 +0200 |
commit | 4d22f09283ad6ac66c0e76cc41747e0b8e492436 (patch) | |
tree | 1b9dc493acfa176423789029b8d87269e760c023 /src | |
parent | c01a66426c00392dd7f8711d561b92af4da80779 (diff) | |
download | TwitterDataAnalytics-4d22f09283ad6ac66c0e76cc41747e0b8e492436.tar.gz |
Implement isValid, allow version spec for resource
Diffstat (limited to 'src')
-rw-r--r-- | src/mining/AbstractRequester.java | 8 | ||||
-rw-r--r-- | src/mining/BearerRequester.java | 10 | ||||
-rw-r--r-- | src/mining/OAuthRequester.java | 9 | ||||
-rw-r--r-- | src/mining/Requester.java | 5 |
4 files changed, 28 insertions, 4 deletions
diff --git a/src/mining/AbstractRequester.java b/src/mining/AbstractRequester.java index 9f8b175..d8edc79 100644 --- a/src/mining/AbstractRequester.java +++ b/src/mining/AbstractRequester.java @@ -47,6 +47,8 @@ public abstract class AbstractRequester implements Requester { } catch (JSONException ex) { } } + // TODO: what if there is an internal server error? Technically we + // should always treat that as "don't know what the server thinks". if (checkStatusCode && conn.getResponseCode() != 200) { // TODO: print more helpful details throw new IOException("Unexpected response code"); @@ -56,9 +58,11 @@ public abstract class AbstractRequester implements Requester { protected final URL buildUrl(String resource) throws IOException { String spec = API_URL; - if (!resource.startsWith("oauth/") && !resource.startsWith("oauth2/")) { + if (!resource.startsWith("oauth/") && !resource.startsWith("oauth2/") + && !resource.startsWith("1")) { // manual inspection shows that at least oauth/ and oauth2/ do not - // have a version prefixed. + // have a version prefixed. Do not add version if already given + // (e.g. "1/account/rate_limit_status"). spec += "1.1/"; } spec += resource; diff --git a/src/mining/BearerRequester.java b/src/mining/BearerRequester.java index bc82cd9..894fe91 100644 --- a/src/mining/BearerRequester.java +++ b/src/mining/BearerRequester.java @@ -24,6 +24,8 @@ public class BearerRequester extends AbstractRequester { private final String access_token; /** + * Instantiates a requester with the given access token. The caller should + * test the token for validity. * * @param access_token The bearer token that authenticates a request. */ @@ -79,4 +81,12 @@ public class BearerRequester extends AbstractRequester { public String getAccessToken() { return access_token; } + + @Override + public boolean isValid() throws IOException { + // NOTE: this actually contributes to the ratelimit (12/minute) + // TODO: find alternative that does not hit the ratelimit + JSONObject obj = getJSONRelax("application/rate_limit_status"); + return !obj.has("errors"); + } } diff --git a/src/mining/OAuthRequester.java b/src/mining/OAuthRequester.java index e72360e..f5b6a10 100644 --- a/src/mining/OAuthRequester.java +++ b/src/mining/OAuthRequester.java @@ -5,6 +5,7 @@ import java.net.URLConnection; import oauth.signpost.OAuthConsumer; import oauth.signpost.basic.DefaultOAuthConsumer; import oauth.signpost.exception.OAuthException; +import org.json.JSONObject; /** * An API requester that uses OAuth to sign its requests. @@ -32,4 +33,12 @@ public class OAuthRequester extends AbstractRequester { throw new IOException(ex); } } + + @Override + public boolean isValid() throws IOException { + // NOTE: this actually contributes to the ratelimit (12/minute) + // TODO: find alternative that does not hit the ratelimit + JSONObject obj = getJSONRelax("1/application/rate_limit_status"); + return !obj.has("errors"); + } } diff --git a/src/mining/Requester.java b/src/mining/Requester.java index 685a03a..9ae01e1 100644 --- a/src/mining/Requester.java +++ b/src/mining/Requester.java @@ -12,8 +12,9 @@ public interface Requester { /** * Performs an API request for a resource, for example - * "statuses/mentions_timeline" (note that there is no version or leading - * slash nor ".json" extension). + * "statuses/mentions_timeline" (note, no leading slash nor ".json" suffix). + * Prefix the resource with "1/" when you really need to target the old 1.0 + * API. * * @param resource The REST resource. * @return A JSON object resulting from the request. |