Skip to content

Commit 10186ae

Browse files
authored
Merge pull request #146 from loonydevil/master
added X-RateLimit* fields to RateLimit exception
2 parents 1176b1b + 15230da commit 10186ae

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

intercom-java/src/main/java/io/intercom/api/HttpClient.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import com.google.common.collect.Lists;
77
import com.google.common.collect.Maps;
88
import com.google.common.io.CharStreams;
9+
import com.google.common.primitives.Ints;
10+
import com.google.common.primitives.Longs;
11+
912
import org.apache.commons.codec.binary.Base64;
1013
import org.slf4j.Logger;
1114
import org.slf4j.LoggerFactory;
@@ -34,6 +37,10 @@ class HttpClient {
3437

3538
private static final String APPLICATION_JSON = "application/json";
3639

40+
public static final String RATE_LIMIT_HEADER = "X-RateLimit-Limit";
41+
public static final String RATE_LIMIT_REMAINING_HEADER = "X-RateLimit-Remaining";
42+
public static final String RATE_LIMIT_RESET_HEADER = "X-RateLimit-Reset";
43+
3744
private static String clientAgentDetails() {
3845
final HashMap<String, String> map = Maps.newHashMap();
3946
final ArrayList<String> propKeys = Lists.newArrayList(
@@ -159,7 +166,7 @@ private <T> T handleError(URI uri, HttpURLConnection conn, int responseCode) thr
159166
if (logger.isDebugEnabled()) {
160167
logger.debug("error json follows --\n{}\n-- ", objectMapper.writeValueAsString(errors));
161168
}
162-
return throwException(responseCode, errors);
169+
return throwException(responseCode, errors, conn);
163170
}
164171

165172
private <T> T handleSuccess(JavaType javaType, HttpURLConnection conn, int responseCode) throws IOException {
@@ -189,12 +196,14 @@ private <T> T readEntity(HttpURLConnection conn, int responseCode, JavaType java
189196
}
190197
}
191198

192-
private <T> T throwException(int responseCode, ErrorCollection errors) {
199+
private <T> T throwException(int responseCode, ErrorCollection errors, HttpURLConnection conn) {
193200
// bind some well known response codes to exceptions
194201
if (responseCode == 403 || responseCode == 401) {
195202
throw new AuthorizationException(errors);
196203
} else if (responseCode == 429) {
197-
throw new RateLimitException(errors);
204+
throw new RateLimitException(errors, Ints.tryParse(conn.getHeaderField(RATE_LIMIT_HEADER)),
205+
Ints.tryParse(conn.getHeaderField(RATE_LIMIT_REMAINING_HEADER)),
206+
Longs.tryParse(conn.getHeaderField(RATE_LIMIT_RESET_HEADER)));
198207
} else if (responseCode == 404) {
199208
throw new NotFoundException(errors);
200209
} else if (responseCode == 422) {

intercom-java/src/main/java/io/intercom/api/RateLimitException.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ public class RateLimitException extends IntercomException {
44

55
private static final long serialVersionUID = -6100754169056165622L;
66

7+
private int rateLimit;
8+
9+
private int remainingRequests;
10+
11+
private long resetTime;
12+
713
public RateLimitException(String message) {
814
super(message);
915
}
@@ -16,7 +22,39 @@ public RateLimitException(ErrorCollection errorCollection) {
1622
super(errorCollection);
1723
}
1824

25+
public RateLimitException(ErrorCollection errorCollection, int rateLimit,
26+
int remainingRequests, long resetTime) {
27+
super(errorCollection);
28+
this.rateLimit = rateLimit;
29+
this.remainingRequests = remainingRequests;
30+
this.resetTime = resetTime;
31+
}
32+
1933
public RateLimitException(ErrorCollection errorCollection, Throwable cause) {
2034
super(errorCollection, cause);
2135
}
36+
37+
public int getRateLimit() {
38+
return rateLimit;
39+
}
40+
41+
public void setRateLimit(int rateLimit) {
42+
this.rateLimit = rateLimit;
43+
}
44+
45+
public int getRemainingRequests() {
46+
return remainingRequests;
47+
}
48+
49+
public void setRemainingRequests(int remainingRequests) {
50+
this.remainingRequests = remainingRequests;
51+
}
52+
53+
public long getResetTime() {
54+
return resetTime;
55+
}
56+
57+
public void setResetTime(long resetTime) {
58+
this.resetTime = resetTime;
59+
}
2260
}

0 commit comments

Comments
 (0)