Search in sources :

Example 1 with ZulipRateLimitExceededException

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);
    }
}
Also used : ZulipClientException(com.github.jamesnetherton.zulip.client.exception.ZulipClientException) Header(org.apache.http.Header) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) ZulipRateLimitExceededException(com.github.jamesnetherton.zulip.client.exception.ZulipRateLimitExceededException) IOException(java.io.IOException) ZulipApiResponse(com.github.jamesnetherton.zulip.client.api.core.ZulipApiResponse) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 2 with ZulipRateLimitExceededException

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());
    }
}
Also used : ZulipConfiguration(com.github.jamesnetherton.zulip.client.http.ZulipConfiguration) ZulipClientException(com.github.jamesnetherton.zulip.client.exception.ZulipClientException) ZulipRateLimitExceededException(com.github.jamesnetherton.zulip.client.exception.ZulipRateLimitExceededException) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Aggregations

ZulipClientException (com.github.jamesnetherton.zulip.client.exception.ZulipClientException)2 ZulipRateLimitExceededException (com.github.jamesnetherton.zulip.client.exception.ZulipRateLimitExceededException)2 ZulipApiResponse (com.github.jamesnetherton.zulip.client.api.core.ZulipApiResponse)1 ZulipConfiguration (com.github.jamesnetherton.zulip.client.http.ZulipConfiguration)1 IOException (java.io.IOException)1 URL (java.net.URL)1 Header (org.apache.http.Header)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 Test (org.junit.jupiter.api.Test)1