use of com.mashape.unirest.http.HttpResponse in project Javacord by BtoBastian.
the class ImplDiscordAPI method updateProfile.
@Override
public Future<Void> updateProfile(final String newUsername, String newEmail, final String newPassword, final BufferedImage newAvatar) {
logger.debug("Trying to update profile (username: {}, email: {}, password: {}, change avatar: {}", newUsername, email, newPassword == null ? "null" : newPassword.replaceAll(".", "*"), newAvatar != null);
String avatarString = getYourself().getAvatarId();
if (newAvatar != null) {
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(newAvatar, "jpg", os);
avatarString = "data:image/jpg;base64," + BaseEncoding.base64().encode(os.toByteArray());
} catch (IOException ignored) {
}
}
final JSONObject params = new JSONObject().put("username", newUsername == null ? getYourself().getName() : newUsername).put("avatar", avatarString == null ? JSONObject.NULL : avatarString);
if (email != null && password != null) {
// do not exist in bot accounts
params.put("email", newEmail == null ? email : newEmail).put("password", password);
}
if (newPassword != null) {
params.put("new_password", newPassword);
}
return getThreadPool().getExecutorService().submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
HttpResponse<JsonNode> response = Unirest.patch("https://discordapp.com/api/v6/users/@me").header("authorization", token).header("Content-Type", "application/json").body(params.toString()).asJson();
checkResponse(response);
logger.info("Updated profile (username: {}, email: {}, password: {}, change avatar: {}", newUsername, email, newPassword == null ? "null" : newPassword.replaceAll(".", "*"), newAvatar != null);
((ImplUser) getYourself()).setAvatarId(response.getBody().getObject().getString("avatar"));
if (response.getBody().getObject().has("email") && !response.getBody().getObject().isNull("email")) {
setEmail(response.getBody().getObject().getString("email"));
}
setToken(response.getBody().getObject().getString("token"), token.startsWith("Bot "));
final String oldName = getYourself().getName();
((ImplUser) getYourself()).setName(response.getBody().getObject().getString("username"));
if (newPassword != null) {
password = newPassword;
}
if (!getYourself().getName().equals(oldName)) {
getThreadPool().getSingleThreadExecutorService("listeners").submit(new Runnable() {
@Override
public void run() {
List<UserChangeNameListener> listeners = getListeners(UserChangeNameListener.class);
synchronized (listeners) {
for (UserChangeNameListener listener : listeners) {
listener.onUserChangeName(ImplDiscordAPI.this, getYourself(), oldName);
}
}
}
});
}
return null;
}
});
}
use of com.mashape.unirest.http.HttpResponse in project Javacord by BtoBastian.
the class ImplDiscordAPI method parseInvite.
@Override
public Future<Invite> parseInvite(final String invite, FutureCallback<Invite> callback) {
final String inviteCode = invite.replace("https://discord.gg/", "").replace("http://discord.gg/", "");
ListenableFuture<Invite> future = getThreadPool().getListeningExecutorService().submit(new Callable<Invite>() {
@Override
public Invite call() throws Exception {
logger.debug("Trying to parse invite {} (parsed code: {})", invite, inviteCode);
HttpResponse<JsonNode> response = Unirest.get("https://discordapp.com/api/v6/invite/" + inviteCode).header("authorization", token).asJson();
checkResponse(response);
logger.debug("Parsed invite {} (parsed code: {})", invite, inviteCode);
return new ImplInvite(ImplDiscordAPI.this, response.getBody().getObject());
}
});
if (callback != null) {
Futures.addCallback(future, callback);
}
return future;
}
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;
}
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;
}
use of com.mashape.unirest.http.HttpResponse 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;
}
Aggregations