Search in sources :

Example 6 with HttpResponse

use of com.mashape.unirest.http.HttpResponse in project Javacord by BtoBastian.

the class ImplChannel method sendFile.

@Override
public Future<Message> sendFile(final File file, final String comment, 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 a file in channel {} (name: {}, comment: {})", ImplChannel.this, file.getName(), comment);
            api.checkRateLimit(null, RateLimitType.SERVER_MESSAGE, null, ImplChannel.this);
            MultipartBody body = Unirest.post("https://discordapp.com/api/v6/channels/" + id + "/messages").header("authorization", api.getToken()).field("file", file);
            if (comment != null) {
                body.field("content", comment);
            }
            HttpResponse<JsonNode> response = body.asJson();
            api.checkResponse(response);
            api.checkRateLimit(response, RateLimitType.SERVER_MESSAGE, null, ImplChannel.this);
            logger.debug("Sent a file in channel {} (name: {}, comment: {})", ImplChannel.this, file.getName(), comment);
            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) MessageReceiver(de.btobastian.javacord.entities.message.MessageReceiver) MultipartBody(com.mashape.unirest.request.body.MultipartBody) 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 7 with HttpResponse

use of com.mashape.unirest.http.HttpResponse in project Javacord by BtoBastian.

the class ImplChannel method sendFile.

@Override
public Future<Message> sendFile(final InputStream inputStream, final String filename, final String comment, 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 an input stream in channel {} (comment: {})", ImplChannel.this, comment);
            api.checkRateLimit(null, RateLimitType.SERVER_MESSAGE, null, ImplChannel.this);
            MultipartBody body = Unirest.post("https://discordapp.com/api/v6/channels/" + id + "/messages").header("authorization", api.getToken()).field("file", inputStream, filename);
            if (comment != null) {
                body.field("content", comment);
            }
            HttpResponse<JsonNode> response = body.asJson();
            api.checkResponse(response);
            api.checkRateLimit(response, RateLimitType.SERVER_MESSAGE, null, ImplChannel.this);
            logger.debug("Sent an input stream in channel {} (comment: {})", ImplChannel.this, comment);
            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) MessageReceiver(de.btobastian.javacord.entities.message.MessageReceiver) MultipartBody(com.mashape.unirest.request.body.MultipartBody) 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 8 with HttpResponse

use of com.mashape.unirest.http.HttpResponse in project dataverse by IQSS.

the class DataCaptureModuleUtilTest method testGetScriptFromRequestNotFound.

@Test
public void testGetScriptFromRequestNotFound() throws UnsupportedEncodingException {
    System.out.println("getScriptFromRequestNotFound");
    HttpResponseFactory factory = new DefaultHttpResponseFactory();
    org.apache.http.HttpResponse response = factory.newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_NOT_FOUND, null), null);
    JsonObjectBuilder jab = Json.createObjectBuilder();
    jab.add("userId", 42);
    jab.add("datasetIdentifier", "123");
    jab.add("script", "#!/bin/sh");
    response.setEntity(new StringEntity(jab.build().toString()));
    HttpResponse<JsonNode> httpResponse = new HttpResponse<>(response, JsonNode.class);
    ScriptRequestResponse result = DataCaptureModuleUtil.getScriptFromRequest(httpResponse);
    assertEquals(404, result.getHttpStatusCode());
    assertEquals(-1, result.getDatasetId());
    assertEquals(-1, result.getUserId());
    assertEquals(null, result.getScript());
}
Also used : StringEntity(org.apache.http.entity.StringEntity) HttpResponse(com.mashape.unirest.http.HttpResponse) DefaultHttpResponseFactory(org.apache.http.impl.DefaultHttpResponseFactory) JsonNode(com.mashape.unirest.http.JsonNode) JsonObjectBuilder(javax.json.JsonObjectBuilder) HttpResponseFactory(org.apache.http.HttpResponseFactory) DefaultHttpResponseFactory(org.apache.http.impl.DefaultHttpResponseFactory) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 9 with HttpResponse

use of com.mashape.unirest.http.HttpResponse in project dataverse by IQSS.

the class DataCaptureModuleUtilTest method testGetScriptFromRequestOk.

@Test
public void testGetScriptFromRequestOk() throws UnsupportedEncodingException {
    System.out.println("getScriptFromRequestOk");
    HttpResponseFactory factory = new DefaultHttpResponseFactory();
    org.apache.http.HttpResponse response = factory.newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null), null);
    JsonObjectBuilder jab = Json.createObjectBuilder();
    jab.add("userId", 42);
    jab.add("datasetIdentifier", "123");
    jab.add("script", "#!/bin/sh");
    response.setEntity(new StringEntity(jab.build().toString()));
    HttpResponse<JsonNode> httpResponse = new HttpResponse<>(response, JsonNode.class);
    ScriptRequestResponse result = DataCaptureModuleUtil.getScriptFromRequest(httpResponse);
    assertEquals(200, result.getHttpStatusCode());
    assertEquals("123", result.getDatasetIdentifier());
    assertEquals(42, result.getUserId());
    assertEquals("#!/bin/sh", result.getScript());
}
Also used : StringEntity(org.apache.http.entity.StringEntity) HttpResponse(com.mashape.unirest.http.HttpResponse) DefaultHttpResponseFactory(org.apache.http.impl.DefaultHttpResponseFactory) JsonNode(com.mashape.unirest.http.JsonNode) JsonObjectBuilder(javax.json.JsonObjectBuilder) HttpResponseFactory(org.apache.http.HttpResponseFactory) DefaultHttpResponseFactory(org.apache.http.impl.DefaultHttpResponseFactory) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 10 with HttpResponse

use of com.mashape.unirest.http.HttpResponse in project Javacord by BtoBastian.

the class ImplUser method sendFile.

@Override
public Future<Message> sendFile(final InputStream inputStream, final String filename, final String comment, 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 an input stream to user {} (comment: {})", ImplUser.this, comment);
            api.checkRateLimit(null, RateLimitType.PRIVATE_MESSAGE, null, null);
            MultipartBody body = Unirest.post("https://discordapp.com/api/v6/channels/" + getUserChannelIdBlocking() + "/messages").header("authorization", api.getToken()).field("file", inputStream, filename);
            if (comment != null) {
                body.field("content", comment);
            }
            HttpResponse<JsonNode> response = body.asJson();
            api.checkResponse(response);
            api.checkRateLimit(response, RateLimitType.PRIVATE_MESSAGE, null, null);
            logger.debug("Sent an input stream to user {} (comment: {})", ImplUser.this, comment);
            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) MessageReceiver(de.btobastian.javacord.entities.message.MessageReceiver) MultipartBody(com.mashape.unirest.request.body.MultipartBody) ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) HttpResponse(com.mashape.unirest.http.HttpResponse) JSONException(org.json.JSONException) MalformedURLException(java.net.MalformedURLException)

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