use of io.discloader.discloader.common.exceptions.UnauthorizedException in project DiscLoader by R3alCl0ud.
the class RESTQueue method handle.
public void handle() {
try {
if (waiting || queue.size() == 0 || globalLimit) {
return;
}
waiting = true;
final APIRequest apiRequest = queue.get(0);
BaseRequest request = apiRequest.createRequest();
request = addHeaders(request, apiRequest.auth, apiRequest.multi);
request.asStringAsync(new Callback<String>() {
@Override
public void cancelled() {
apiRequest.future.completeExceptionally(new Throwable());
}
@Override
public void completed(HttpResponse<String> response) {
Map<String, List<String>> headers = response.getHeaders();
headers.forEach((name, value) -> {
switch(name) {
case "X-RateLimit-Limit":
rateLimit = Integer.parseInt(value.get(0), 10);
break;
case "X-RateLimit-Remaining":
remaining = Integer.parseInt(value.get(0), 10);
break;
case "x-ratelimit-reset":
case "X-RateLimit-Reset":
resetTime = (Long.parseLong(value.get(0), 10) * 1000L);
break;
case "X-RateLimit-Global":
globalLimit = Boolean.parseBoolean(value.get(0));
break;
}
});
RawEvent event = new RawEvent(loader, response);
DateFormat df = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
try {
timeDifference = Date.from(Instant.now()).getTime() - df.parse(headers.get("Date").get(0)).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
int code = response.getStatus();
if (code == 429) {
Thread wait = new Thread("Ratelimit resetting - " + apiRequest.url) {
@Override
public void run() {
try {
Thread.sleep(Integer.parseInt(headers.get("Retry-After").get(0), 10) + 500);
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
waiting = false;
globalLimit = false;
handle();
}
};
wait.setPriority((Thread.NORM_PRIORITY + Thread.MAX_PRIORITY) / 2);
wait.setDaemon(true);
wait.start();
return;
} else if (code != 200 && code != 201 && code != 204 && code != 304) {
queue.remove(apiRequest);
loader.emit(event);
loader.emit("RawPacket", event);
ExceptionJSON data = gson.fromJson(response.getBody(), ExceptionJSON.class);
switch(code) {
case 401:
apiRequest.future.completeExceptionally(new UnauthorizedException(response.getBody()));
break;
case 403:
switch(data.code) {
case 20002:
apiRequest.future.completeExceptionally(new AccountTypeException(data));
break;
case 50013:
apiRequest.future.completeExceptionally(new PermissionsException(data));
break;
default:
apiRequest.future.completeExceptionally(new UnauthorizedException(response.getBody()));
break;
}
break;
default:
apiRequest.future.completeExceptionally(new DiscordException(data));
break;
}
} else {
queue.remove(apiRequest);
loader.emit(event);
loader.emit("RawPacket", event);
apiRequest.future.complete(response.getBody());
}
globalLimit = false;
long waitTime = ((resetTime - System.currentTimeMillis()) + timeDifference + 500);
if (remaining == 0 && waitTime > 0) {
Thread wait = new Thread("REST Waiting - " + apiRequest.url) {
@Override
public void run() {
try {
Thread.sleep(waitTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
waiting = false;
handle();
}
};
wait.setPriority((Thread.NORM_PRIORITY + Thread.MAX_PRIORITY) / 2);
wait.setDaemon(true);
wait.start();
} else {
waiting = false;
handle();
}
}
@Override
public void failed(UnirestException e) {
apiRequest.future.completeExceptionally(e);
handle();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
use of io.discloader.discloader.common.exceptions.UnauthorizedException in project DiscLoader by R3alCl0ud.
the class Guild method delete.
public CompletableFuture<IGuild> delete() {
if (!isOwner())
throw new UnauthorizedException("Only the guild's owner can delete a guild");
CompletableFuture<IGuild> future = new CompletableFuture<>();
loader.rest.makeRequest(Endpoints.guild(getID()), DLUtil.Methods.DELETE, true).thenAcceptAsync(data -> {
future.complete(this);
});
return future;
}
Aggregations