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);
}
}
Aggregations