Search in sources :

Example 1 with MultipartBody

use of com.mashape.unirest.request.body.MultipartBody 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 2 with MultipartBody

use of com.mashape.unirest.request.body.MultipartBody 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 3 with MultipartBody

use of com.mashape.unirest.request.body.MultipartBody in project Javacord by BtoBastian.

the class ImplUser 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 to user {} (name: {}, comment: {})", ImplUser.this, file.getName(), 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", file);
            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 a file to user {} (name: {}, comment: {})", ImplUser.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) JSONException(org.json.JSONException) MalformedURLException(java.net.MalformedURLException)

Example 4 with MultipartBody

use of com.mashape.unirest.request.body.MultipartBody 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)

Example 5 with MultipartBody

use of com.mashape.unirest.request.body.MultipartBody in project DiscLoader by R3alCl0ud.

the class APIRequest method createRequest.

public BaseRequest createRequest() throws IOException {
    BaseRequest request = null;
    switch(this.method) {
        case 0:
            request = Unirest.get(this.route);
            break;
        case 1:
            request = Unirest.post(this.route);
            if (multi) {
                // try {
                SendableMessage sdata = (SendableMessage) this.data;
                File file = sdata.file;
                Resource resource = sdata.resource;
                byte[] bytes = new byte[0];
                if (file != null)
                    bytes = DLUtil.readAllBytes(file);
                if (resource != null)
                    bytes = DLUtil.readAllBytes(resource);
                MultipartBody body = ((HttpRequestWithBody) request).fields(null);
                String loc = "";
                if (file != null)
                    loc = file.getName();
                if (resource != null)
                    loc = resource.getFileName();
                body.field("Content-type", "multipart/form-data").field("file", bytes, loc).field("payload_json", gson.toJson(sdata));
            // } catch (IOException ex) {
            // ex.printStackTrace();
            // }
            } else {
                ((HttpRequestWithBody) request).body(gson.toJson(data));
            }
            break;
        case 3:
            request = Unirest.patch(this.route);
            ((HttpRequestWithBody) request).body(gson.toJson(data));
            break;
        case 2:
            request = Unirest.delete(this.route);
            break;
        case 4:
            request = Unirest.put(this.route);
            ((HttpRequestWithBody) request).body(gson.toJson(this.data));
            break;
        default:
            request = Unirest.get(this.route);
            break;
    }
    return request;
}
Also used : SendableMessage(io.discloader.discloader.entity.sendable.SendableMessage) MultipartBody(com.mashape.unirest.request.body.MultipartBody) HttpRequestWithBody(com.mashape.unirest.request.HttpRequestWithBody) BaseRequest(com.mashape.unirest.request.BaseRequest) Resource(io.discloader.discloader.client.render.util.Resource) File(java.io.File)

Aggregations

MultipartBody (com.mashape.unirest.request.body.MultipartBody)6 HttpResponse (com.mashape.unirest.http.HttpResponse)4 Message (de.btobastian.javacord.entities.message.Message)4 MessageReceiver (de.btobastian.javacord.entities.message.MessageReceiver)4 ImplMessage (de.btobastian.javacord.entities.message.impl.ImplMessage)4 JSONException (org.json.JSONException)4 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)3 BaseRequest (com.mashape.unirest.request.BaseRequest)2 HttpRequestWithBody (com.mashape.unirest.request.HttpRequestWithBody)2 Resource (io.discloader.discloader.client.render.util.Resource)2 SendableMessage (io.discloader.discloader.entity.sendable.SendableMessage)2 File (java.io.File)2 MalformedURLException (java.net.MalformedURLException)2 HttpRequest (com.mashape.unirest.request.HttpRequest)1 DiscordException (io.discloader.discloader.common.exceptions.DiscordException)1 JSONObject (org.json.JSONObject)1