return performApiGetRequest(sb.toString(), true);
}
private BatchRequestResponse convertToBatchRequestResponse(ClientResponse clientResponse) {
BatchRequestResponse batchRequestResponse = new BatchRequestResponse();
int retCode = clientResponse.getStatus();
batchRequestResponse.setReturnCode(retCode);
String responseString = clientResponse.getEntity(String.class);
LOG.debug("Processing API response: status={}, body={}", retCode, responseString);
Map httpResponseMap;
try {
httpResponseMap = gson.fromJson(responseString, Map.class);
LOG.debug("Processing responce as JSON");
} catch (JsonSyntaxException e) {
LOG.debug("Response is not valid JSON object. Recording as is");
httpResponseMap = new HashMap();
httpResponseMap.put("message", responseString);
}
if (retCode < 300) {
if (httpResponseMap == null) {
//Empty response on successful scenario
batchRequestResponse.setStatus(HostRoleStatus.COMPLETED.toString());
return batchRequestResponse;
}
Map requestMap = null;
Object requestMapObject = httpResponseMap.get("Requests");
if (requestMapObject instanceof Map) {
requestMap = (Map) requestMapObject;
}
if (requestMap != null) {
batchRequestResponse.setRequestId((
(Double) requestMap.get(REQUESTS_ID_KEY)).longValue());
//TODO fix different names for field
String status = null;
if (requestMap.get(REQUESTS_STATUS_KEY) != null) {
status = requestMap.get(REQUESTS_STATUS_KEY).toString();
}
if (requestMap.get("status") != null) {
status = requestMap.get("status").toString();
}
if (requestMap.get(REQUESTS_ABORTED_TASKS_KEY) != null) {
batchRequestResponse.setAbortedTaskCount(
((Double) requestMap.get(REQUESTS_ABORTED_TASKS_KEY)).intValue());
}
if (requestMap.get(REQUESTS_FAILED_TASKS_KEY) != null) {
batchRequestResponse.setFailedTaskCount(
((Double) requestMap.get(REQUESTS_FAILED_TASKS_KEY)).intValue());
}
if (requestMap.get(REQUESTS_TIMEDOUT_TASKS_KEY) != null) {
batchRequestResponse.setTimedOutTaskCount(
((Double) requestMap.get(REQUESTS_TIMEDOUT_TASKS_KEY)).intValue());
}
if (requestMap.get(REQUESTS_TOTAL_TASKS_KEY) != null) {
batchRequestResponse.setTotalTaskCount(
((Double) requestMap.get(REQUESTS_TOTAL_TASKS_KEY)).intValue());
}
batchRequestResponse.setStatus(status);
}
} else {
//unsuccessful response
batchRequestResponse.setReturnMessage((String) httpResponseMap.get("message"));
batchRequestResponse.setStatus(HostRoleStatus.FAILED.toString());
}
return batchRequestResponse;
}