summaryrefslogtreecommitdiff
path: root/src/io/AbstractRequester.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/io/AbstractRequester.java')
-rw-r--r--src/io/AbstractRequester.java56
1 files changed, 24 insertions, 32 deletions
diff --git a/src/io/AbstractRequester.java b/src/io/AbstractRequester.java
index ac74533..239e75d 100644
--- a/src/io/AbstractRequester.java
+++ b/src/io/AbstractRequester.java
@@ -1,5 +1,7 @@
package io;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonParser;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
@@ -7,11 +9,10 @@ import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
+import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
-import org.json.JSONException;
-import org.json.JSONObject;
/**
* Performs an API Request.
@@ -23,36 +24,29 @@ public abstract class AbstractRequester implements Requester {
private static final String API_URL = "https://api.twitter.com/";
@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 {
+ public Response getJSON(String resource) throws IOException {
HttpURLConnection conn = open(buildUrl(resource));
try {
preconnect(conn);
- JSONObject resp = getResponseAsJson(conn);
- /* print response to stderr for debugging */
- if (resp.has("errors")) {
- try {
- String errors = resp.get("errors").toString();
- getLogger().info("Request failed: " + errors);
- } catch (JSONException ex) {
- }
+ JsonElement resp = getResponseAsJson(conn);
+
+ if(resp.isJsonObject() && resp.getAsJsonObject().has("errors")) {
+ /* print response to stderr for debugging */
+ String errors = resp.getAsJsonObject().get("errors").toString();
+ getLogger().log(Level.INFO, "Request failed: {0}", errors);
}
// 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) {
+ if (conn.getResponseCode() != 200) {
// TODO: print more helpful details
throw new IOException("Unexpected response code");
- }
- return resp;
+ }
+
+ int rateLimit = Integer.parseInt(conn.getHeaderField("X-Rate-Limit-Limit"));
+ int rateLimitRemaining = Integer.parseInt(conn.getHeaderField("X-Rate-Limit-Remaining"));
+ int rateLimitReset = Integer.parseInt(conn.getHeaderField("X-Rate-Limit-Reset"));
+
+ return new Response(resp, rateLimit, rateLimitRemaining, rateLimitReset);
} finally {
conn.disconnect();
}
@@ -92,7 +86,7 @@ public abstract class AbstractRequester implements Requester {
* @throws java.io.IOException on failure to open a connection.
*/
protected final HttpURLConnection open(URL url) throws IOException {
- getLogger().fine("Opening: " + url);
+ getLogger().log(Level.FINE, "Opening: {0}", url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// set default param: fail if no response within 5 seconds
conn.setReadTimeout(5000);
@@ -107,7 +101,7 @@ public abstract class AbstractRequester implements Requester {
* @throws java.io.IOException if the response cannot be retrieved or if the
* response does not contain well-formed JSON.
*/
- protected final JSONObject getResponseAsJson(HttpURLConnection conn)
+ protected final JsonElement getResponseAsJson(HttpURLConnection conn)
throws IOException {
StringWriter writer = new StringWriter();
InputStream is;
@@ -122,13 +116,11 @@ public abstract class AbstractRequester implements Requester {
throw new IOException("Failed to fetch response");
}
IOUtils.copy(is, writer, Charsets.UTF_8);
- try {
- return new JSONObject(writer.toString());
- } catch (JSONException ex) {
- // treat JSON errors as if an I/O error occurred
- throw new IOException(ex);
- }
+
+ JsonParser parser = new JsonParser();
+ return parser.parse(writer.toString());
}
+
/**
* Prepare the request before it gets send.