use of com.github.jamesnetherton.zulip.client.exception.ZulipRateLimitExceededException in project zulip-java-client by jamesnetherton.
the class ZulipCommonsHttpClient method doRequest.
private <T extends ZulipApiResponse> T doRequest(HttpUriRequest request, Class<T> responseAs) throws ZulipClientException {
try {
ResponseHolder response = client.execute(request, new ResponseHandler<ResponseHolder>() {
@Override
public ResponseHolder handleResponse(HttpResponse response) throws IOException {
Header header = response.getFirstHeader("x-ratelimit-reset");
int status = response.getStatusLine().getStatusCode();
if ((status >= 200 && status < 300) || (status == 400)) {
HttpEntity entity = response.getEntity();
if (entity != null) {
String json = EntityUtils.toString(entity);
ZulipApiResponse zulipApiResponse = JsonUtils.getMapper().readValue(json, responseAs);
return new ResponseHolder(zulipApiResponse, status, header);
} else {
return new ResponseHolder(null, status, header);
}
} else if (status == 429) {
return new ResponseHolder(null, status, header);
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
}
}, context);
if (response.getStatusCode() == 429) {
ZulipRateLimitExceededException rateLimitExceededException = new ZulipRateLimitExceededException(response.getRateLimitReset());
throw new ZulipClientException(rateLimitExceededException);
}
ZulipApiResponse zulipApiResponse = response.getResponse();
if (zulipApiResponse == null) {
throw new ZulipClientException("Response was empty");
}
if (!zulipApiResponse.isSuccess()) {
throw new ZulipClientException(zulipApiResponse.getMessage(), zulipApiResponse.getCode());
}
return responseAs.cast(zulipApiResponse);
} catch (IOException e) {
throw new ZulipClientException(e);
}
}
use of com.github.jamesnetherton.zulip.client.exception.ZulipRateLimitExceededException in project zulip-java-client by jamesnetherton.
the class ZulipCommonsHttpClientTest method invalidRateLimitReset.
@Test
public void invalidRateLimitReset() throws Exception {
server.stubFor(request("GET", urlPathEqualTo("/api/v1/")).willReturn(aResponse().withStatus(429).withHeader("x-ratelimit-reset", "").withBody((String) null)));
URL zulipUrl = new URL(server.baseUrl());
ZulipConfiguration configuration = new ZulipConfiguration(zulipUrl, "test@test.com", "abc123");
ZulipCommonsHttpClient client = new ZulipCommonsHttpClient(configuration);
try {
client.get("/", Collections.emptyMap(), ZulipApiResponse.class);
} catch (ZulipClientException e) {
ZulipRateLimitExceededException cause = (ZulipRateLimitExceededException) e.getCause();
assertEquals(0, cause.getReteLimitReset());
}
}
Aggregations