From 0c07b1d1a83ab20b4c753c40ea8048f73b3e5745 Mon Sep 17 00:00:00 2001 From: Maurice Laveaux Date: Thu, 22 May 2014 09:02:50 +0200 Subject: Changed Request response to a new Response class. * Response contains the JsonElement, tickrate, reset and leftover data. * Removed getJsonRelaxed, because it was not used elsewhere. --- src/io/AbstractRequester.java | 56 +++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 32 deletions(-) (limited to 'src/io/AbstractRequester.java') 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. -- cgit v1.2.1