use of com.mashape.unirest.http.HttpResponse in project Javacord by BtoBastian.
the class ImplChannel method sendMessage.
@Override
public Future<Message> sendMessage(final String content, final EmbedBuilder embed, final boolean tts, final String nonce, FutureCallback<Message> callback) {
final MessageReceiver receiver = this;
ListenableFuture<Message> future = api.getThreadPool().getListeningExecutorService().submit(new Callable<Message>() {
@Override
public Message call() throws Exception {
logger.debug("Trying to send message in channel {} (content: \"{}\", tts: {})", ImplChannel.this, content, tts);
api.checkRateLimit(null, RateLimitType.SERVER_MESSAGE, null, ImplChannel.this);
JSONObject body = new JSONObject().put("content", content).put("tts", tts).put("mentions", new String[0]);
if (embed != null) {
body.put("embed", embed.toJSONObject());
}
if (nonce != null) {
body.put("nonce", nonce);
}
HttpResponse<JsonNode> response = Unirest.post("https://discordapp.com/api/v6/channels/" + id + "/messages").header("authorization", api.getToken()).header("content-type", "application/json").body(body.toString()).asJson();
api.checkResponse(response);
api.checkRateLimit(response, RateLimitType.SERVER_MESSAGE, null, ImplChannel.this);
logger.debug("Sent message in channel {} (content: \"{}\", tts: {})", ImplChannel.this, content, tts);
return new ImplMessage(response.getBody().getObject(), api, receiver);
}
});
if (callback != null) {
Futures.addCallback(future, callback);
}
return future;
}
use of com.mashape.unirest.http.HttpResponse in project dataverse by IQSS.
the class DataCaptureModuleUtilTest method testMakeUploadRequest.
@Test
public void testMakeUploadRequest() throws UnsupportedEncodingException {
System.out.println("makeUploadRequest");
HttpResponseFactory factory = new DefaultHttpResponseFactory();
org.apache.http.HttpResponse response = factory.newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null), null);
response.setEntity(new StringEntity("received"));
HttpResponse<String> httpResponse = new HttpResponse<>(response, String.class);
UploadRequestResponse result = DataCaptureModuleUtil.makeUploadRequest(httpResponse);
assertEquals(200, result.getHttpStatusCode());
assertEquals("received", result.getResponse());
}
use of com.mashape.unirest.http.HttpResponse in project javalin by tipsy.
the class TestExceptionMapper method test_typedMappedException_isHandled.
@Test
public void test_typedMappedException_isHandled() throws Exception {
app.get("/typed-exception", ctx -> {
throw new TypedException();
}).exception(TypedException.class, (e, ctx) -> {
ctx.result(e.proofOfType());
});
HttpResponse<String> response = GET_asString("/typed-exception");
assertThat(response.getBody(), is("I'm so typed"));
assertThat(response.getStatus(), is(200));
}
use of com.mashape.unirest.http.HttpResponse in project javalin by tipsy.
the class TestExceptionMapper method test_mappedException_isHandled.
@Test
public void test_mappedException_isHandled() throws Exception {
app.get("/mapped-exception", ctx -> {
throw new Exception();
}).exception(Exception.class, (e, ctx) -> ctx.result("It's been handled."));
HttpResponse<String> response = GET_asString("/mapped-exception");
assertThat(response.getBody(), is("It's been handled."));
assertThat(response.getStatus(), is(200));
}
use of com.mashape.unirest.http.HttpResponse in project DiscordSRV by Scarsz.
the class WebhookUtil method deliverMessage.
public static void deliverMessage(TextChannel channel, Player player, String message) {
if (channel == null)
return;
Webhook targetWebhook = getWebhookToUseForChannel(channel, player.getUniqueId().toString());
if (targetWebhook == null)
return;
new Thread(() -> {
try {
HttpResponse<String> response = Unirest.post(targetWebhook.getUrl()).field("content", message).field("username", DiscordUtil.strip(player.getDisplayName())).field("avatar_url", "https://minotar.net/helm/" + player.getName() + "/100.png").asString();
DiscordSRV.debug("Received API response for webhook message delivery: " + response.getStatus());
} catch (Exception e) {
DiscordSRV.error("Failed to deliver webhook message to Discord: " + e.getMessage());
DiscordSRV.debug(ExceptionUtils.getMessage(e));
e.printStackTrace();
}
}).start();
}
Aggregations