Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Sawicki2012-04-17 17:01:57 -0400
committerKevin Sawicki2012-04-17 17:10:03 -0400
commit4c729e19506e4266184c2b9ef9ce84cda6d19424 (patch)
tree3c4541c53bc67b3c033a0e01ef73d51851587cdb
parent8401d7cb64886144711cae1f43912e97f551a92e (diff)
downloadegit-github-4c729e19506e4266184c2b9ef9ce84cda6d19424.tar.gz
egit-github-4c729e19506e4266184c2b9ef9ce84cda6d19424.tar.xz
egit-github-4c729e19506e4266184c2b9ef9ce84cda6d19424.zip
Store current rate limit headers after each request made
Provide these parsed values so callers know how close they are to approaching the rate limit on client requests Change-Id: I30ba72d01abbf0cb820634105c3c780b07d6ad9c
-rw-r--r--org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubClient.java62
1 files changed, 62 insertions, 0 deletions
diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubClient.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubClient.java
index 83279b74..26f84b2e 100644
--- a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubClient.java
+++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubClient.java
@@ -147,6 +147,10 @@ public class GitHubClient {
private int bufferSize = 8192;
+ private int requestLimit = -1;
+
+ private int remainingRequests = -1;
+
/**
* Create default client
*/
@@ -585,6 +589,7 @@ public class GitHubClient {
final Object params, final Type type) throws IOException {
sendParams(request, params);
final int code = request.getResponseCode();
+ updateRateLimits(request);
if (isOk(code))
if (type != null)
return parseJson(getStream(request), type);
@@ -648,6 +653,7 @@ public class GitHubClient {
if (accept != null)
httpRequest.setRequestProperty(HEADER_ACCEPT, accept);
final int code = httpRequest.getResponseCode();
+ updateRateLimits(httpRequest);
if (isOk(code))
return new GitHubResponse(httpRequest, getBody(request,
getStream(httpRequest)));
@@ -702,7 +708,63 @@ public class GitHubClient {
HttpURLConnection request = createDelete(uri);
sendParams(request, params);
final int code = request.getResponseCode();
+ updateRateLimits(request);
if (!isEmpty(code))
throw new RequestException(parseError(getStream(request)), code);
}
+
+ /**
+ * Update rate limits present in response headers
+ *
+ * @param request
+ * @return this client
+ */
+ protected GitHubClient updateRateLimits(HttpURLConnection request) {
+ String limit = request.getHeaderField("X-RateLimit-Limit");
+ if (limit != null && limit.length() > 0)
+ try {
+ requestLimit = Integer.parseInt(limit);
+ } catch (NumberFormatException nfe) {
+ requestLimit = -1;
+ }
+ else
+ requestLimit = -1;
+
+ String remaining = request.getHeaderField("X-RateLimit-Remaining");
+ if (remaining != null && remaining.length() > 0)
+ try {
+ remainingRequests = Integer.parseInt(remaining);
+ } catch (NumberFormatException nfe) {
+ remainingRequests = -1;
+ }
+ else
+ remainingRequests = -1;
+
+ return this;
+ }
+
+ /**
+ * Get number of requests remaining before rate limiting occurs
+ * <p>
+ * This will be the value of the 'X-RateLimit-Remaining' header from the
+ * last request made
+ *
+ * @return remainingRequests or -1 if not present in the response
+ */
+ public int getRemainingRequests() {
+ return remainingRequests;
+ }
+
+ /**
+ * Get number of requests that {@link #getRemainingRequests()} counts down
+ * from as each request is made
+ * <p>
+ * This will be the value of the 'X-RateLimit-Limit' header from the last
+ * request made
+ *
+ * @return requestLimit or -1 if not present in the response
+ */
+ public int getRequestLimit() {
+ return requestLimit;
+ }
}

Back to the top