This commit is contained in:
parent
303bdb21dd
commit
c0fef300d0
@ -94,6 +94,11 @@ public class PanelAction<T extends PanelModel<T>> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute this action instantly.
|
* Execute this action instantly.
|
||||||
|
* <p>
|
||||||
|
* Doing this will execute the web
|
||||||
|
* request and return the response
|
||||||
|
* immediately, in a blocking manner.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* @return the response, null if none
|
* @return the response, null if none
|
||||||
*/
|
*/
|
||||||
|
@ -49,7 +49,8 @@ public final class ApplicationNodeActions extends PanelActions {
|
|||||||
* @param id the node id
|
* @param id the node id
|
||||||
* @return the action
|
* @return the action
|
||||||
*/
|
*/
|
||||||
public PanelAction<Node> details(int id) {
|
@NonNull
|
||||||
|
public PanelAction<Node> getDetails(int id) {
|
||||||
return PanelAction.create(clientConfig(), rateLimitHandler(), JsonWebRequest.builder()
|
return PanelAction.create(clientConfig(), rateLimitHandler(), JsonWebRequest.builder()
|
||||||
.endpoint("/application/nodes/" + id)
|
.endpoint("/application/nodes/" + id)
|
||||||
.build(), Node.class);
|
.build(), Node.class);
|
||||||
|
35
src/main/java/me/braydon/pelican/request/HttpStatus.java
Normal file
35
src/main/java/me/braydon/pelican/request/HttpStatus.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* MIT License
|
||||||
|
*
|
||||||
|
* Copyright (c) 2024 Braydon (Rainnny).
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
package me.braydon.pelican.request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Braydon
|
||||||
|
*/
|
||||||
|
public final class HttpStatus {
|
||||||
|
// 2xx Successful
|
||||||
|
public static final int OK = 200;
|
||||||
|
|
||||||
|
// 4xx Client Errors
|
||||||
|
public static final int TOO_MANY_REQUESTS = 429;
|
||||||
|
}
|
@ -99,8 +99,8 @@ public class JsonWebRequest {
|
|||||||
log.debug("Receive response: {}", status);
|
log.debug("Receive response: {}", status);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the status is not 200 (OK), handle the error
|
// If the status is not OK, handle the error
|
||||||
if (status != 200) {
|
if (status != HttpStatus.OK) {
|
||||||
JsonObject errorJsonObject = GSON.fromJson(json, JsonObject.class);
|
JsonObject errorJsonObject = GSON.fromJson(json, JsonObject.class);
|
||||||
errorJsonObject = errorJsonObject.get("errors").getAsJsonArray().get(0).getAsJsonObject();
|
errorJsonObject = errorJsonObject.get("errors").getAsJsonArray().get(0).getAsJsonObject();
|
||||||
throw new PanelAPIException(status, errorJsonObject.get("code").getAsString(), errorJsonObject.get("detail").getAsString());
|
throw new PanelAPIException(status, errorJsonObject.get("code").getAsString(), errorJsonObject.get("detail").getAsString());
|
||||||
|
@ -103,6 +103,15 @@ public final class RateLimitHandler {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Try and execute the given action.
|
* Try and execute the given action.
|
||||||
|
* <p>
|
||||||
|
* Doing this will try and execute the
|
||||||
|
* given action and immediately respond
|
||||||
|
* with the result. If the action fails
|
||||||
|
* to execute, and the error is due to
|
||||||
|
* a rate limit, the action will be queued
|
||||||
|
* to be re-tried later. All other errors
|
||||||
|
* will be passed to the given callback.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* @param action the action to try
|
* @param action the action to try
|
||||||
* @param callback the callback to invoke
|
* @param callback the callback to invoke
|
||||||
@ -124,7 +133,7 @@ public final class RateLimitHandler {
|
|||||||
// the task to be re-tried later
|
// the task to be re-tried later
|
||||||
if (ex instanceof PanelAPIException) {
|
if (ex instanceof PanelAPIException) {
|
||||||
PanelAPIException apiException = (PanelAPIException) ex;
|
PanelAPIException apiException = (PanelAPIException) ex;
|
||||||
if (apiException.getCode() == 404 && !throwRateLimitErrors) {
|
if (apiException.getCode() == HttpStatus.TOO_MANY_REQUESTS && !throwRateLimitErrors) {
|
||||||
if (clientConfig.debugging()) {
|
if (clientConfig.debugging()) {
|
||||||
log.debug("Panel API rate limit exceeded{}", retry ? ", queued action to be re-tried later..." : "");
|
log.debug("Panel API rate limit exceeded{}", retry ? ", queued action to be re-tried later..." : "");
|
||||||
}
|
}
|
||||||
|
@ -25,8 +25,10 @@ package me.braydon.pelican.test;
|
|||||||
|
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import me.braydon.pelican.action.pelican.PelicanPanelActions;
|
import me.braydon.pelican.action.pelican.PelicanPanelActions;
|
||||||
|
import me.braydon.pelican.action.pterodactyl.application.ApplicationNodeActions;
|
||||||
import me.braydon.pelican.client.ClientConfig;
|
import me.braydon.pelican.client.ClientConfig;
|
||||||
import me.braydon.pelican.client.Pelican4J;
|
import me.braydon.pelican.client.Pelican4J;
|
||||||
|
import me.braydon.pelican.model.Node;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Nested;
|
import org.junit.jupiter.api.Nested;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@ -65,9 +67,17 @@ public final class PelicanActionTests {
|
|||||||
*/
|
*/
|
||||||
@Test @SneakyThrows
|
@Test @SneakyThrows
|
||||||
void testGetNodes() {
|
void testGetNodes() {
|
||||||
client.actions().application().nodes().details(1).queue(node -> {
|
ApplicationNodeActions nodeActions = client.actions().application().nodes();
|
||||||
|
|
||||||
|
// Get the details for Node 1, await the response async
|
||||||
|
nodeActions.getDetails(1).queue(node -> {
|
||||||
System.out.println("node = " + node);
|
System.out.println("node = " + node);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Get the details for Node 1 in a blocking manner
|
||||||
|
Node node = nodeActions.getDetails(1).execute();
|
||||||
|
System.out.println("node = " + node);
|
||||||
|
|
||||||
Thread.sleep(60000L);
|
Thread.sleep(60000L);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user