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.java30
1 files changed, 21 insertions, 9 deletions
diff --git a/src/io/AbstractRequester.java b/src/io/AbstractRequester.java
index 913e9bd..333e39d 100644
--- a/src/io/AbstractRequester.java
+++ b/src/io/AbstractRequester.java
@@ -13,6 +13,7 @@ import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
+import java.util.zip.GZIPInputStream;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
@@ -30,15 +31,16 @@ public abstract class AbstractRequester implements Requester {
throws IOException, RateLimitException {
HttpURLConnection conn = open(buildUrl(resource));
try {
+ conn.addRequestProperty("Accept-Encoding", "gzip");
preconnect(conn);
+
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);
+ getLogger().info("Request " + resource + " failed: " + errors);
}
-
// TODO: what if there is an internal server error? Technically we
// should always treat that as "don't know what the server thinks".
@@ -48,14 +50,16 @@ public abstract class AbstractRequester implements Requester {
throw new RateLimitException(Integer.parseInt(conn.getHeaderField("X-Rate-Limit-Reset")));
}
// TODO: print more helpful details
- throw new IOException("Unexpected response code");
+ throw new IOException("Unexpected response code "
+ + conn.getResponseCode() + " for " + resource);
}
-
+
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"));
+ long rateLimitReset = Long.parseLong(conn.getHeaderField("X-Rate-Limit-Reset"));
+
+ return new Response(resp, rateLimit, rateLimitRemaining, rateLimitReset, rateLimitReset * 1000 - conn.getDate());
- return new Response(resp, rateLimit, rateLimitRemaining, rateLimitReset);
} finally {
conn.disconnect();
}
@@ -115,10 +119,10 @@ public abstract class AbstractRequester implements Requester {
StringWriter writer = new StringWriter();
InputStream is;
try {
- is = conn.getInputStream();
- } catch (FileNotFoundException ex) {
+ is = wrapGZip(conn, conn.getInputStream());
+ } catch (IOException ex) {
/* 404 (FileNotFoundException) */
- is = conn.getErrorStream();
+ is = wrapGZip(conn, conn.getErrorStream());
}
// this could happen if the URL was severly malformed
if (is == null) {
@@ -130,6 +134,14 @@ public abstract class AbstractRequester implements Requester {
return parser.parse(writer.toString());
}
+ private InputStream wrapGZip(HttpURLConnection conn, InputStream in) throws IOException {
+ if ("gzip".equals(conn.getContentEncoding())) {
+ return new GZIPInputStream(in);
+ } else {
+ return conn.getInputStream();
+ }
+ }
+
/**
* Prepare the request before it gets send.
*