From c0fef300d011aa76cad6426de9d5f418dc8b0bbe Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Sun, 28 Apr 2024 17:33:04 -0400 Subject: [PATCH] Cleanup --- .../braydon/pelican/action/PanelAction.java | 5 +++ .../application/ApplicationNodeActions.java | 3 +- .../braydon/pelican/request/HttpStatus.java | 35 +++++++++++++++++++ .../pelican/request/JsonWebRequest.java | 4 +-- .../pelican/request/RateLimitHandler.java | 11 +++++- .../pelican/test/PelicanActionTests.java | 12 ++++++- 6 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 src/main/java/me/braydon/pelican/request/HttpStatus.java diff --git a/src/main/java/me/braydon/pelican/action/PanelAction.java b/src/main/java/me/braydon/pelican/action/PanelAction.java index 2821a69..9bf3f06 100644 --- a/src/main/java/me/braydon/pelican/action/PanelAction.java +++ b/src/main/java/me/braydon/pelican/action/PanelAction.java @@ -94,6 +94,11 @@ public class PanelAction> { /** * Execute this action instantly. + *

+ * Doing this will execute the web + * request and return the response + * immediately, in a blocking manner. + *

* * @return the response, null if none */ diff --git a/src/main/java/me/braydon/pelican/action/pterodactyl/application/ApplicationNodeActions.java b/src/main/java/me/braydon/pelican/action/pterodactyl/application/ApplicationNodeActions.java index 1469f10..436d179 100644 --- a/src/main/java/me/braydon/pelican/action/pterodactyl/application/ApplicationNodeActions.java +++ b/src/main/java/me/braydon/pelican/action/pterodactyl/application/ApplicationNodeActions.java @@ -49,7 +49,8 @@ public final class ApplicationNodeActions extends PanelActions { * @param id the node id * @return the action */ - public PanelAction details(int id) { + @NonNull + public PanelAction getDetails(int id) { return PanelAction.create(clientConfig(), rateLimitHandler(), JsonWebRequest.builder() .endpoint("/application/nodes/" + id) .build(), Node.class); diff --git a/src/main/java/me/braydon/pelican/request/HttpStatus.java b/src/main/java/me/braydon/pelican/request/HttpStatus.java new file mode 100644 index 0000000..3e079df --- /dev/null +++ b/src/main/java/me/braydon/pelican/request/HttpStatus.java @@ -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; +} \ No newline at end of file diff --git a/src/main/java/me/braydon/pelican/request/JsonWebRequest.java b/src/main/java/me/braydon/pelican/request/JsonWebRequest.java index dc76fa5..51adc28 100644 --- a/src/main/java/me/braydon/pelican/request/JsonWebRequest.java +++ b/src/main/java/me/braydon/pelican/request/JsonWebRequest.java @@ -99,8 +99,8 @@ public class JsonWebRequest { log.debug("Receive response: {}", status); } - // If the status is not 200 (OK), handle the error - if (status != 200) { + // If the status is not OK, handle the error + if (status != HttpStatus.OK) { JsonObject errorJsonObject = GSON.fromJson(json, JsonObject.class); errorJsonObject = errorJsonObject.get("errors").getAsJsonArray().get(0).getAsJsonObject(); throw new PanelAPIException(status, errorJsonObject.get("code").getAsString(), errorJsonObject.get("detail").getAsString()); diff --git a/src/main/java/me/braydon/pelican/request/RateLimitHandler.java b/src/main/java/me/braydon/pelican/request/RateLimitHandler.java index ae1a29a..c89c0f5 100644 --- a/src/main/java/me/braydon/pelican/request/RateLimitHandler.java +++ b/src/main/java/me/braydon/pelican/request/RateLimitHandler.java @@ -103,6 +103,15 @@ public final class RateLimitHandler { /** * Try and execute the given action. + *

+ * 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. + *

* * @param action the action to try * @param callback the callback to invoke @@ -124,7 +133,7 @@ public final class RateLimitHandler { // the task to be re-tried later if (ex instanceof PanelAPIException) { PanelAPIException apiException = (PanelAPIException) ex; - if (apiException.getCode() == 404 && !throwRateLimitErrors) { + if (apiException.getCode() == HttpStatus.TOO_MANY_REQUESTS && !throwRateLimitErrors) { if (clientConfig.debugging()) { log.debug("Panel API rate limit exceeded{}", retry ? ", queued action to be re-tried later..." : ""); } diff --git a/src/test/java/me/braydon/pelican/test/PelicanActionTests.java b/src/test/java/me/braydon/pelican/test/PelicanActionTests.java index 994c6c2..ec52379 100644 --- a/src/test/java/me/braydon/pelican/test/PelicanActionTests.java +++ b/src/test/java/me/braydon/pelican/test/PelicanActionTests.java @@ -25,8 +25,10 @@ package me.braydon.pelican.test; import lombok.SneakyThrows; 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.Pelican4J; +import me.braydon.pelican.model.Node; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -65,9 +67,17 @@ public final class PelicanActionTests { */ @Test @SneakyThrows 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); }); + + // Get the details for Node 1 in a blocking manner + Node node = nodeActions.getDetails(1).execute(); + System.out.println("node = " + node); + Thread.sleep(60000L); } }