diff options
author | Peter Wu <peter@lekensteyn.nl> | 2014-04-24 02:21:20 +0200 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2014-04-24 02:21:20 +0200 |
commit | 38df1e024c3c5229cf65747c7147532bf9a8f825 (patch) | |
tree | f6aa7b198742227d26acfe0620955064001e380b /src | |
parent | f4dfe8640b9cdaa05d7e3d79af7b4455d6cdd70a (diff) | |
download | TwitterDataAnalytics-38df1e024c3c5229cf65747c7147532bf9a8f825.tar.gz |
Fix builder, fix crash on invalid URL, fix ".json" placement
Diffstat (limited to 'src')
-rw-r--r-- | src/mining/AbstractRequester.java | 24 | ||||
-rw-r--r-- | src/mining/TwitterApi.java | 5 |
2 files changed, 24 insertions, 5 deletions
diff --git a/src/mining/AbstractRequester.java b/src/mining/AbstractRequester.java index d8edc79..d8c9673 100644 --- a/src/mining/AbstractRequester.java +++ b/src/mining/AbstractRequester.java @@ -58,15 +58,27 @@ 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/") - && !resource.startsWith("1")) { + String[] split = resource.split("\\?", 2); + boolean isPreV1; + isPreV1 = resource.startsWith("oauth/") || resource.startsWith("oauth2/"); + if (!isPreV1 && !resource.startsWith("1")) { // manual inspection shows that at least oauth/ and oauth2/ do not // have a version prefixed. Do not add version if already given // (e.g. "1/account/rate_limit_status"). spec += "1.1/"; } - spec += resource; - spec += ".json"; + // path part + spec += split[0]; + if (!isPreV1) { + // not sure about the history, but oauth{,2}/ don't have this. + spec += ".json"; + } + + // append parameters after path + if (split.length >= 2) { + spec += "?" + split[1]; + } + return new URL(spec); } @@ -103,6 +115,10 @@ public abstract class AbstractRequester implements Requester { /* 404 (FileNotFoundException) */ is = conn.getErrorStream(); } + // this could happen if the URL was severly malformed + if (is == null) { + throw new IOException("Failed to fetch response"); + } IOUtils.copy(is, writer, Charsets.UTF_8); try { return new JSONObject(writer.toString()); diff --git a/src/mining/TwitterApi.java b/src/mining/TwitterApi.java index 39db3ad..4f8e243 100644 --- a/src/mining/TwitterApi.java +++ b/src/mining/TwitterApi.java @@ -83,7 +83,10 @@ public class TwitterApi { @Override public String toString() { - return URLEncodedUtils.format(params, Charsets.UTF_8); + if (params.isEmpty()) { + return this.resource; + } + return this.resource + "?" + URLEncodedUtils.format(params, Charsets.UTF_8); } public JSONObject request() throws IOException { |