use of com.mashape.unirest.request.HttpRequest in project zeppelin by apache.
the class HttpBasedClient method delete.
@Override
public ActionResponse delete(String index, String type, String id) {
ActionResponse response = null;
try {
final HttpRequest request = Unirest.delete(getUrl(index, type, id, true));
if (StringUtils.isNotEmpty(username)) {
request.basicAuth(username, password);
}
final HttpResponse<String> result = request.asString();
final boolean isSucceeded = isSucceeded(result);
if (isSucceeded) {
final JsonNode body = new JsonNode(result.getBody());
response = new ActionResponse().succeeded(true).hit(new HitWrapper(getFieldAsString(body, "_index"), getFieldAsString(body, "_type"), getFieldAsString(body, "_id"), null));
} else {
throw new ActionException(result.getBody());
}
} catch (final UnirestException e) {
throw new ActionException(e);
}
return response;
}
use of com.mashape.unirest.request.HttpRequest in project zeppelin by apache.
the class HttpBasedClient method get.
@Override
public ActionResponse get(String index, String type, String id) {
ActionResponse response = null;
try {
final HttpRequest request = Unirest.get(getUrl(index, type, id, true));
if (StringUtils.isNotEmpty(username)) {
request.basicAuth(username, password);
}
final HttpResponse<String> result = request.asString();
final boolean isSucceeded = isSucceeded(result);
if (isSucceeded) {
final JsonNode body = new JsonNode(result.getBody());
if (body.getObject().has("_index")) {
response = new ActionResponse().succeeded(true).hit(new HitWrapper(getFieldAsString(body, "_index"), getFieldAsString(body, "_type"), getFieldAsString(body, "_id"), getFieldAsString(body, "_source")));
} else {
final JSONArray hits = getFieldAsArray(body.getObject(), "hits/hits");
final JSONObject hit = (JSONObject) hits.iterator().next();
response = new ActionResponse().succeeded(true).hit(new HitWrapper(hit.getString("_index"), hit.getString("_type"), hit.getString("_id"), hit.opt("_source").toString()));
}
} else {
if (result.getStatus() == 404) {
response = new ActionResponse().succeeded(false);
} else {
throw new ActionException(result.getBody());
}
}
} catch (final UnirestException e) {
throw new ActionException(e);
}
return response;
}
Aggregations