Search in sources :

Example 1 with HttpRequest

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;
}
Also used : HttpRequest(com.mashape.unirest.request.HttpRequest) HitWrapper(org.apache.zeppelin.elasticsearch.action.HitWrapper) ActionException(org.apache.zeppelin.elasticsearch.action.ActionException) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) JsonNode(com.mashape.unirest.http.JsonNode) ActionResponse(org.apache.zeppelin.elasticsearch.action.ActionResponse)

Example 2 with HttpRequest

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;
}
Also used : HttpRequest(com.mashape.unirest.request.HttpRequest) HitWrapper(org.apache.zeppelin.elasticsearch.action.HitWrapper) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ActionException(org.apache.zeppelin.elasticsearch.action.ActionException) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) JsonNode(com.mashape.unirest.http.JsonNode) ActionResponse(org.apache.zeppelin.elasticsearch.action.ActionResponse)

Aggregations

JsonNode (com.mashape.unirest.http.JsonNode)2 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)2 HttpRequest (com.mashape.unirest.request.HttpRequest)2 ActionException (org.apache.zeppelin.elasticsearch.action.ActionException)2 ActionResponse (org.apache.zeppelin.elasticsearch.action.ActionResponse)2 HitWrapper (org.apache.zeppelin.elasticsearch.action.HitWrapper)2 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1