/**
* POST request using UniRest library.
*
* @param url is endpoint to which request has to be done.
* @param params is data that has to be sent in body.
* @return JSONObject returns json response.
* @throws KiteException contains error message and error code inside.
* @throws JSONException occurs when there is error while parsing data.
*/
public JSONObject postRequest(String url, Map<String, Object> params) throws KiteException, JSONException {
try {
if(KiteConnect.httpHost != null){
Unirest.setProxy(KiteConnect.httpHost);
}
HttpResponse<JsonNode> response = Unirest.post(url)
.header("accept", "application/json")
.fields(params)
.asJson();
JsonNode body = response.getBody();
JSONObject jsonObject = body.getObject();
int code = response.getStatus();
if (code == 200)
return jsonObject;
else
throw dealWithKiteException(body, code);
} catch (UnirestException e) {
throw new KiteNoNetworkException("Connection error");
}
}
java类com.mashape.unirest.http.JsonNode的实例源码
KiteRequest.java 文件源码
项目:javakiteconnect
阅读 17
收藏 0
点赞 0
评论 0
CaptainClient.java 文件源码
项目:captain-java
阅读 19
收藏 0
点赞 0
评论 0
public void updateKv(String key, JSONObject js) {
Unirest.post(urlRoot() + "/api/kv/set").field("key", key).field("value", js.toString())
.asJsonAsync(new Callback<JsonNode>() {
@Override
public void completed(HttpResponse<JsonNode> response) {
}
@Override
public void failed(UnirestException e) {
e.printStackTrace();
}
@Override
public void cancelled() {
}
});
}
ImplDiscordAPI.java 文件源码
项目:Javacord
阅读 15
收藏 0
点赞 0
评论 0
@Override
public boolean checkTokenBlocking(String token) {
try {
// only the last 0-9 digits of the token should be visible.
// We don't want someone being able to login to an account by reading the logs.
logger.debug("Checking token {}", token.replaceAll(".{10}", "**********"));
HttpResponse<JsonNode> response = Unirest.get("https://discordapp.com/api/v6/users/@me/guilds")
.header("authorization", token)
.asJson();
if (response.getStatus() < 200 || response.getStatus() > 299) {
logger.debug("Checked token {} (valid: {})", token.replaceAll(".{10}", "**********"), false);
return false;
}
logger.debug("Checked token {} (valid: {})", token.replaceAll(".{10}", "**********"), true);
return true;
} catch (UnirestException e) {
return false;
}
}
ImplDiscordAPI.java 文件源码
项目:Javacord
阅读 16
收藏 0
点赞 0
评论 0
@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;
}
ImplDiscordAPI.java 文件源码
项目:Javacord
阅读 17
收藏 0
点赞 0
评论 0
@Override
public Future<Void> deleteInvite(final String inviteCode) {
return getThreadPool().getExecutorService().submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
logger.debug("Trying to delete invite {}", inviteCode);
HttpResponse<JsonNode> response = Unirest
.delete("https://discordapp.com/api/v6/invite/" + inviteCode)
.header("authorization", token)
.asJson();
checkResponse(response);
logger.info("Deleted invite {}", inviteCode);
return null;
}
});
}
ImplDiscordAPI.java 文件源码
项目:Javacord
阅读 17
收藏 0
点赞 0
评论 0
/**
* Requests the gateway.
*
* @return The requested gateway.
*/
public String requestGatewayBlocking() {
try {
logger.debug("Requesting gateway (token: {})", token.replaceAll(".{10}", "**********"));
HttpResponse<JsonNode> response = Unirest.get("https://discordapp.com/api/v6/gateway")
.header("authorization", token)
.asJson();
if (response.getStatus() == 401) {
throw new IllegalStateException("Cannot request gateway! Invalid token?");
}
if (response.getStatus() < 200 || response.getStatus() > 299) {
throw new IllegalStateException("Received http status code " + response.getStatus()
+ " with message " + response.getStatusText() + " and body " + response.getBody());
}
String gateway = response.getBody().getObject().getString("url");
logger.debug("Requested gateway {} (token: {})", gateway, token.replaceAll(".{10}", "**********"));
return gateway;
} catch (UnirestException e) {
e.printStackTrace();
return null;
}
}
ImplDiscordAPI.java 文件源码
项目:Javacord
阅读 15
收藏 0
点赞 0
评论 0
/**
* Checks the response.
*
* @param response The response to check.
* @throws Exception If the response has problems (status code not between 200 and 300).
*/
public void checkResponse(HttpResponse<JsonNode> response) throws Exception {
String message = "";
if (response.getBody() != null && !response.getBody().isArray() &&
response.getBody().getObject().has("message")) {
message = " " + response.getBody().getObject().getString("message");
}
if (response.getStatus() == 403) {
throw new PermissionsException("Missing permissions!" + message);
}
if (response.getStatus() == 429) {
// Handled in #checkRateLimit
return;
}
if (response.getStatus() < 200 || response.getStatus() > 299) {
throw new BadResponseException("Received http status code " + response.getStatus() + " with message "
+ response.getStatusText() + " and body " + response.getBody(), response.getStatus(),
response.getStatusText(), response);
}
}
ImplMessage.java 文件源码
项目:Javacord
阅读 16
收藏 0
点赞 0
评论 0
@Override
public Future<Void> removeAllReactions() {
return api.getThreadPool().getExecutorService().submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
logger.debug("Trying to remove all reactions from message {}", ImplMessage.this);
HttpResponse<JsonNode> response = Unirest
.delete("https://discordapp.com/api/v6/channels/" + channelId + "/messages/" + getId() + "/reactions")
.header("authorization", api.getToken())
.asJson();
api.checkResponse(response);
if (isPrivateMessage()) {
api.checkRateLimit(response, RateLimitType.UNKNOWN, null, null);
} else {
api.checkRateLimit(response, RateLimitType.UNKNOWN, null, getChannelReceiver());
}
logger.debug("Removed all reactions from message {}", ImplMessage.this);
return null;
}
});
}
ImplMessage.java 文件源码
项目:Javacord
阅读 16
收藏 0
点赞 0
评论 0
/**
* Adds an reaction to the message.
*
* @param reaction The reaction to add. Whether a unicode emoji or a custom emoji in the format <code>name:id</code>.
* @return A future which tells us if the creation was a success.
*/
private Future<Void> addReaction(final String reaction) {
return api.getThreadPool().getExecutorService().submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
logger.debug("Trying to add reaction to message with id {} (reaction: {})", getId(), reaction);
HttpResponse<JsonNode> response = Unirest
.put("https://discordapp.com/api/v6/channels/" + channelId + "/messages/" + getId() + "/reactions/" + reaction + "/@me")
.header("authorization", api.getToken())
.header("content-type", "application/json")
.body("{}")
.asJson();
api.checkResponse(response);
if (isPrivateMessage()) {
api.checkRateLimit(response, RateLimitType.UNKNOWN, null, null);
} else {
api.checkRateLimit(response, RateLimitType.UNKNOWN, null, getChannelReceiver());
}
logger.debug("Added reaction to message with id {} (reaction: {})", getId(), reaction);
return null;
}
});
}
ImplReaction.java 文件源码
项目:Javacord
阅读 17
收藏 0
点赞 0
评论 0
@Override
public Future<Void> removeUser(final User user) {
return api.getThreadPool().getExecutorService().submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
logger.debug("Trying to remove reactor {} from reaction {} of message {}", user, ImplReaction.this, message);
String reactionString = isCustomEmoji() ? getCustomEmoji().getName() + ":" + getCustomEmoji().getId() : getUnicodeEmoji();
HttpResponse<JsonNode> response = Unirest
.delete("https://discordapp.com/api/v6/channels/" + ((ImplMessage) message).getChannelId() + "/messages/" + message.getId() + "/reactions/" + reactionString + "/" + user.getId())
.header("authorization", api.getToken())
.asJson();
api.checkResponse(response);
api.checkRateLimit(response, RateLimitType.UNKNOWN, null, message.getChannelReceiver());
logger.debug("Removed reactor {} from reaction {} of message {}", user, ImplReaction.this, message);
return null;
}
});
}