Search in sources :

Example 1 with ZulipApiResponse

use of com.github.jamesnetherton.zulip.client.api.core.ZulipApiResponse 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)

Aggregations

ZulipApiResponse (com.github.jamesnetherton.zulip.client.api.core.ZulipApiResponse)1 ZulipClientException (com.github.jamesnetherton.zulip.client.exception.ZulipClientException)1 ZulipRateLimitExceededException (com.github.jamesnetherton.zulip.client.exception.ZulipRateLimitExceededException)1 IOException (java.io.IOException)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