Search in sources :

Example 11 with HttpResponse

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;
}
Also used : ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) Message(de.btobastian.javacord.entities.message.Message) JSONObject(org.json.JSONObject) MessageReceiver(de.btobastian.javacord.entities.message.MessageReceiver) ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) HttpResponse(com.mashape.unirest.http.HttpResponse) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) JSONException(org.json.JSONException)

Example 12 with HttpResponse

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());
}
Also used : StringEntity(org.apache.http.entity.StringEntity) HttpResponse(com.mashape.unirest.http.HttpResponse) DefaultHttpResponseFactory(org.apache.http.impl.DefaultHttpResponseFactory) HttpResponseFactory(org.apache.http.HttpResponseFactory) DefaultHttpResponseFactory(org.apache.http.impl.DefaultHttpResponseFactory) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 13 with HttpResponse

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));
}
Also used : HttpResponse(com.mashape.unirest.http.HttpResponse) TypedException(io.javalin.util.TypedException) Test(org.junit.Test) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TypedException(io.javalin.util.TypedException) Test(org.junit.Test)

Example 14 with HttpResponse

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));
}
Also used : HttpResponse(com.mashape.unirest.http.HttpResponse) TypedException(io.javalin.util.TypedException) Test(org.junit.Test) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TypedException(io.javalin.util.TypedException) Test(org.junit.Test)

Example 15 with HttpResponse

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();
}
Also used : HttpResponse(com.mashape.unirest.http.HttpResponse) Webhook(net.dv8tion.jda.core.entities.Webhook)

Aggregations

HttpResponse (com.mashape.unirest.http.HttpResponse)15 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)6 Message (de.btobastian.javacord.entities.message.Message)6 MessageReceiver (de.btobastian.javacord.entities.message.MessageReceiver)6 ImplMessage (de.btobastian.javacord.entities.message.impl.ImplMessage)6 JSONException (org.json.JSONException)6 Test (org.junit.Test)5 MultipartBody (com.mashape.unirest.request.body.MultipartBody)4 MalformedURLException (java.net.MalformedURLException)3 HttpResponseFactory (org.apache.http.HttpResponseFactory)3 StringEntity (org.apache.http.entity.StringEntity)3 DefaultHttpResponseFactory (org.apache.http.impl.DefaultHttpResponseFactory)3 BasicStatusLine (org.apache.http.message.BasicStatusLine)3 JSONObject (org.json.JSONObject)3 JsonNode (com.mashape.unirest.http.JsonNode)2 BadResponseException (de.btobastian.javacord.exceptions.BadResponseException)2 NotSupportedForBotsException (de.btobastian.javacord.exceptions.NotSupportedForBotsException)2 PermissionsException (de.btobastian.javacord.exceptions.PermissionsException)2 RateLimitedException (de.btobastian.javacord.exceptions.RateLimitedException)2 TypedException (io.javalin.util.TypedException)2