use of com.mashape.unirest.http.exceptions.UnirestException in project zeppelin by apache.
the class HttpBasedClient method search.
@Override
public ActionResponse search(String[] indices, String[] types, String query, int size) {
ActionResponse response = null;
if (!StringUtils.isEmpty(query)) {
// So, try to parse as a JSON => if there is an error, consider the query a Lucene one
try {
gson.fromJson(query, Map.class);
} catch (final JsonParseException e) {
// This is not a JSON (or maybe not well formatted...)
query = QUERY_STRING_TEMPLATE.replace("_Q_", query);
}
}
try {
final HttpRequestWithBody request = Unirest.post(getUrl(indices, types) + "/_search?size=" + size).header("Content-Type", "application/json");
if (StringUtils.isNoneEmpty(query)) {
request.header("Accept", "application/json").body(query);
}
if (StringUtils.isNotEmpty(username)) {
request.basicAuth(username, password);
}
final HttpResponse<JsonNode> result = request.asJson();
final JSONObject body = result.getBody() != null ? result.getBody().getObject() : null;
if (isSucceeded(result)) {
final long total = getFieldAsLong(result, "hits/total");
response = new ActionResponse().succeeded(true).totalHits(total);
if (containsAggs(result)) {
JSONObject aggregationsMap = body.getJSONObject("aggregations");
if (aggregationsMap == null) {
aggregationsMap = body.getJSONObject("aggs");
}
for (final String key : aggregationsMap.keySet()) {
final JSONObject aggResult = aggregationsMap.getJSONObject(key);
if (aggResult.has("buckets")) {
// Multi-bucket aggregations
final Iterator<Object> buckets = aggResult.getJSONArray("buckets").iterator();
while (buckets.hasNext()) {
response.addAggregation(new AggWrapper(AggregationType.MULTI_BUCKETS, buckets.next().toString()));
}
} else {
response.addAggregation(new AggWrapper(AggregationType.SIMPLE, aggregationsMap.toString()));
}
// Keep only one aggregation
break;
}
} else if (size > 0 && total > 0) {
final JSONArray hits = getFieldAsArray(body, "hits/hits");
final Iterator<Object> iter = hits.iterator();
while (iter.hasNext()) {
final JSONObject hit = (JSONObject) iter.next();
final Object data = hit.opt("_source") != null ? hit.opt("_source") : hit.opt("fields");
response.addHit(new HitWrapper(hit.getString("_index"), hit.getString("_type"), hit.getString("_id"), data.toString()));
}
}
} else {
throw new ActionException(body.get("error").toString());
}
} catch (final UnirestException e) {
throw new ActionException(e);
}
return response;
}
use of com.mashape.unirest.http.exceptions.UnirestException 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.http.exceptions.UnirestException 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;
}
use of com.mashape.unirest.http.exceptions.UnirestException in project MantaroBot by Mantaro.
the class ImageCmds method catgirls.
@Command
public static void catgirls(CommandRegistry cr) {
cr.register("catgirl", new SimpleCommand(Category.IMAGE) {
@Override
protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
boolean nsfw = args.length > 0 && args[0].equalsIgnoreCase("nsfw");
if (nsfw && !nsfwCheck(event, true, true))
return;
try {
JSONObject obj = Unirest.get(nsfw ? NSFWURL : BASEURL).asJson().getBody().getObject();
if (!obj.has("url")) {
event.getChannel().sendMessage("Unable to find image").queue();
} else {
event.getChannel().sendFile(CACHE.getInput(obj.getString("url")), "catgirl.png", null).queue();
}
} catch (UnirestException e) {
e.printStackTrace();
event.getChannel().sendMessage("Unable to get image").queue();
}
}
@Override
public MessageEmbed help(GuildMessageReceivedEvent event) {
return helpEmbed(event, "Catgirl command").setDescription("**Sends catgirl images**").addField("Usage", "`~>catgirl` - **Returns catgirl images.**" + "\n´`~>catgirl nsfw` - **Returns lewd or questionable cargirl images.**", false).build();
}
});
}
use of com.mashape.unirest.http.exceptions.UnirestException in project Ardent by adamint.
the class BotlistUpdater method run.
@Override
public void run() {
try {
int guilds = 0;
for (Shard shard : ShardManager.getShards()) {
guilds += shard.jda.getGuilds().size();
}
Unirest.post("https://discordbots.org/api/bots/247093143160356865/stats").header("Authorization", Ardent.discordBotsOrgToken).header("Content-Type", "application/json").body(new JSONObject().append("server_count", guilds).append("shard_count", Ardent.shardCount)).asString().getBody();
Unirest.post("https://bots.discord.pw/api/bots/247093143160356865/stats").header("Authorization", Ardent.botsDiscordPwToken).header("Content-Type", "application/json").body(new JSONObject().append("server_count", guilds).append("shard_count", Ardent.shardCount)).asString().getBody();
} catch (UnirestException ignored) {
}
}
Aggregations