|
6 | 6 | import com.google.common.collect.Lists; |
7 | 7 | import com.google.common.collect.Maps; |
8 | 8 | import com.google.common.io.CharStreams; |
| 9 | +import com.google.common.primitives.Ints; |
| 10 | +import com.google.common.primitives.Longs; |
| 11 | + |
9 | 12 | import org.apache.commons.codec.binary.Base64; |
10 | 13 | import org.slf4j.Logger; |
11 | 14 | import org.slf4j.LoggerFactory; |
@@ -34,6 +37,10 @@ class HttpClient { |
34 | 37 |
|
35 | 38 | private static final String APPLICATION_JSON = "application/json"; |
36 | 39 |
|
| 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 | + |
37 | 44 | private static String clientAgentDetails() { |
38 | 45 | final HashMap<String, String> map = Maps.newHashMap(); |
39 | 46 | final ArrayList<String> propKeys = Lists.newArrayList( |
@@ -159,7 +166,7 @@ private <T> T handleError(URI uri, HttpURLConnection conn, int responseCode) thr |
159 | 166 | if (logger.isDebugEnabled()) { |
160 | 167 | logger.debug("error json follows --\n{}\n-- ", objectMapper.writeValueAsString(errors)); |
161 | 168 | } |
162 | | - return throwException(responseCode, errors); |
| 169 | + return throwException(responseCode, errors, conn); |
163 | 170 | } |
164 | 171 |
|
165 | 172 | 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 |
189 | 196 | } |
190 | 197 | } |
191 | 198 |
|
192 | | - private <T> T throwException(int responseCode, ErrorCollection errors) { |
| 199 | + private <T> T throwException(int responseCode, ErrorCollection errors, HttpURLConnection conn) { |
193 | 200 | // bind some well known response codes to exceptions |
194 | 201 | if (responseCode == 403 || responseCode == 401) { |
195 | 202 | throw new AuthorizationException(errors); |
196 | 203 | } 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))); |
198 | 207 | } else if (responseCode == 404) { |
199 | 208 | throw new NotFoundException(errors); |
200 | 209 | } else if (responseCode == 422) { |
|
0 commit comments