From 93fde2621dbbb981acbfd8b37546046fbaefcf31 Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Sun, 28 Apr 2024 01:55:50 -0400 Subject: [PATCH] API base --- pom.xml | 26 ++++- .../braydon/pelican/action/PanelAction.java | 80 ++++++++++++++ .../braydon/pelican/action/PanelActions.java | 45 ++++++++ .../action/pelican/PelicanPanelActions.java | 40 +++++++ .../action/pterodactyl/PteroPanelActions.java | 61 +++++++++++ .../application/ApplicationNodeActions.java | 56 ++++++++++ .../braydon/pelican/client/ClientConfig.java | 62 +++++++++++ .../me/braydon/pelican/client/Pelican4J.java | 97 +++++++++++++++++ .../pelican/exception/PanelAPIException.java | 53 ++++++++++ .../java/me/braydon/pelican/model/Node.java | 70 ++++++++++++ .../me/braydon/pelican/model/PanelModel.java | 45 ++++++++ .../HttpMethod.java} | 8 +- .../pelican/request/JsonWebRequest.java | 100 ++++++++++++++++++ .../pelican/request/WebRequestHandler.java | 55 ++++++++++ .../pelican/test/PelicanActionTests.java | 8 +- 15 files changed, 800 insertions(+), 6 deletions(-) create mode 100644 src/main/java/me/braydon/pelican/action/PanelAction.java create mode 100644 src/main/java/me/braydon/pelican/action/PanelActions.java create mode 100644 src/main/java/me/braydon/pelican/action/pelican/PelicanPanelActions.java create mode 100644 src/main/java/me/braydon/pelican/action/pterodactyl/PteroPanelActions.java create mode 100644 src/main/java/me/braydon/pelican/action/pterodactyl/application/ApplicationNodeActions.java create mode 100644 src/main/java/me/braydon/pelican/client/ClientConfig.java create mode 100644 src/main/java/me/braydon/pelican/client/Pelican4J.java create mode 100644 src/main/java/me/braydon/pelican/exception/PanelAPIException.java create mode 100644 src/main/java/me/braydon/pelican/model/Node.java create mode 100644 src/main/java/me/braydon/pelican/model/PanelModel.java rename src/main/java/me/braydon/pelican/{Pelican4J.java => request/HttpMethod.java} (89%) create mode 100644 src/main/java/me/braydon/pelican/request/JsonWebRequest.java create mode 100644 src/main/java/me/braydon/pelican/request/WebRequestHandler.java diff --git a/pom.xml b/pom.xml index cbe0ef1..a093dbc 100644 --- a/pom.xml +++ b/pom.xml @@ -12,9 +12,8 @@ 8 - - UTF-8 + 2.1.0-alpha1 @@ -171,6 +170,7 @@ provided + com.squareup.okhttp3 okhttp @@ -183,5 +183,27 @@ 2.10.1 compile + + + + org.junit.jupiter + junit-jupiter-engine + 5.9.2 + test + + + + + org.slf4j + slf4j-api + ${slf4j.version} + compile + + + org.slf4j + slf4j-simple + ${slf4j.version} + compile + \ No newline at end of file diff --git a/src/main/java/me/braydon/pelican/action/PanelAction.java b/src/main/java/me/braydon/pelican/action/PanelAction.java new file mode 100644 index 0000000..f8b62a4 --- /dev/null +++ b/src/main/java/me/braydon/pelican/action/PanelAction.java @@ -0,0 +1,80 @@ +/* + * 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.action; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.NonNull; +import me.braydon.pelican.model.PanelModel; +import me.braydon.pelican.request.JsonWebRequest; +import me.braydon.pelican.request.WebRequestHandler; + +/** + * An action that can be executed on a panel. + * + * @param the type of response expected when this action is executed + * @author Braydon + */ +@AllArgsConstructor(access = AccessLevel.PRIVATE) +public class PanelAction> { + /** + * The web handler to execute actions with. + */ + @NonNull private final WebRequestHandler requestHandler; + + /** + * The web request this action will execute. + */ + @NonNull private final JsonWebRequest webRequest; + + /** + * The type of response expected when + * this action is executed, null if none. + */ + private final Class responseType; + + /** + * Execute this action instantly. + * + * @return the response, null if none + */ + public T execute() { + return requestHandler.handle(webRequest, responseType); + } + + /** + * Create a new panel action. + * + * @param requestHandler the request handler to use + * @param webRequest the web request to send for this action + * @param responseType the expected response type, null if none + * @param the response type + * @return the panel action + */ + @NonNull + public static > PanelAction create(@NonNull WebRequestHandler requestHandler, + @NonNull JsonWebRequest webRequest, Class responseType) { + return new PanelAction<>(requestHandler, webRequest, responseType); + } +} \ No newline at end of file diff --git a/src/main/java/me/braydon/pelican/action/PanelActions.java b/src/main/java/me/braydon/pelican/action/PanelActions.java new file mode 100644 index 0000000..55748cd --- /dev/null +++ b/src/main/java/me/braydon/pelican/action/PanelActions.java @@ -0,0 +1,45 @@ +/* + * 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.action; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NonNull; +import lombok.experimental.Accessors; +import me.braydon.pelican.request.WebRequestHandler; + +/** + * A set of actions that can + * be performed on a panel. + * + * @author Braydon + */ +@AllArgsConstructor @Getter(AccessLevel.PROTECTED) @Accessors(fluent = true) +public abstract class PanelActions { + /** + * The request handler to use for action execution. + */ + @NonNull private final WebRequestHandler requestHandler; +} \ No newline at end of file diff --git a/src/main/java/me/braydon/pelican/action/pelican/PelicanPanelActions.java b/src/main/java/me/braydon/pelican/action/pelican/PelicanPanelActions.java new file mode 100644 index 0000000..2d9e918 --- /dev/null +++ b/src/main/java/me/braydon/pelican/action/pelican/PelicanPanelActions.java @@ -0,0 +1,40 @@ +/* + * 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.action.pelican; + +import lombok.NonNull; +import me.braydon.pelican.action.pterodactyl.PteroPanelActions; +import me.braydon.pelican.request.WebRequestHandler; + +/** + * Implemented actions for the Pelican panel. + * + * @author Braydon + * @see Pelican Website + */ +public class PelicanPanelActions extends PteroPanelActions { + public PelicanPanelActions(@NonNull WebRequestHandler requestHandler) { + super(requestHandler); + } +} \ No newline at end of file diff --git a/src/main/java/me/braydon/pelican/action/pterodactyl/PteroPanelActions.java b/src/main/java/me/braydon/pelican/action/pterodactyl/PteroPanelActions.java new file mode 100644 index 0000000..6e08816 --- /dev/null +++ b/src/main/java/me/braydon/pelican/action/pterodactyl/PteroPanelActions.java @@ -0,0 +1,61 @@ +/* + * 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.action.pterodactyl; + +import lombok.Getter; +import lombok.NonNull; +import lombok.experimental.Accessors; +import me.braydon.pelican.action.PanelActions; +import me.braydon.pelican.action.pterodactyl.application.ApplicationNodeActions; +import me.braydon.pelican.request.WebRequestHandler; + +/** + * Implemented actions for the Pterodactyl panel. + * + * @author Braydon + * @see Pterodactyl Website + */ +@Getter @Accessors(fluent = true) +public class PteroPanelActions extends PanelActions { + /** + * The application actions. + */ + @NonNull private final Application application; + + public PteroPanelActions(@NonNull WebRequestHandler requestHandler) { + super(requestHandler); + this.application = new Application(); + } + + /** + * Actions that can be performed for the application. + */ + @Getter @Accessors(fluent = true) + public class Application { + /** + * Node actions for the application. + */ + @NonNull private final ApplicationNodeActions nodes = new ApplicationNodeActions(requestHandler()); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..ab4f189 --- /dev/null +++ b/src/main/java/me/braydon/pelican/action/pterodactyl/application/ApplicationNodeActions.java @@ -0,0 +1,56 @@ +/* + * 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.action.pterodactyl.application; + +import lombok.NonNull; +import me.braydon.pelican.action.PanelAction; +import me.braydon.pelican.action.PanelActions; +import me.braydon.pelican.model.Node; +import me.braydon.pelican.request.JsonWebRequest; +import me.braydon.pelican.request.WebRequestHandler; + +/** + * Application node actions + * for the Pterodactyl panel. + * + * @author Braydon + */ +public final class ApplicationNodeActions extends PanelActions { + public ApplicationNodeActions(@NonNull WebRequestHandler requestHandler) { + super(requestHandler); + } + + /** + * Get the details of the + * node with the given id. + * + * @param id the node id + * @return the action + */ + public PanelAction details(int id) { + return PanelAction.create(requestHandler(), JsonWebRequest.builder() + .endpoint("/application/nodes/" + id) + .build(), Node.class); + } +} \ No newline at end of file diff --git a/src/main/java/me/braydon/pelican/client/ClientConfig.java b/src/main/java/me/braydon/pelican/client/ClientConfig.java new file mode 100644 index 0000000..da9ca56 --- /dev/null +++ b/src/main/java/me/braydon/pelican/client/ClientConfig.java @@ -0,0 +1,62 @@ +/* + * 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.client; + +import lombok.Builder; +import lombok.Getter; +import lombok.NonNull; +import lombok.ToString; +import lombok.experimental.Accessors; + +/** + * A configuration for a {@link Pelican4J} client. + * + * @author Braydon + */ +@Builder @Getter @Accessors(fluent = true) @ToString +public final class ClientConfig { + /** + * The URL to the panel. + */ + @NonNull private final String panelUrl; + + /** + * The API key to use for authentication. + */ + @ToString.Exclude @NonNull private final String apiKey; + + /** + * Whether debugging should be enabled. + */ + private final boolean debugging; + + public static class ClientConfigBuilder { + @NonNull + public ClientConfigBuilder debugging(boolean state) { + debugging = state; + System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "debug"); // Enable debugging in the logger + return this; + } + } +} \ No newline at end of file diff --git a/src/main/java/me/braydon/pelican/client/Pelican4J.java b/src/main/java/me/braydon/pelican/client/Pelican4J.java new file mode 100644 index 0000000..109071c --- /dev/null +++ b/src/main/java/me/braydon/pelican/client/Pelican4J.java @@ -0,0 +1,97 @@ +/* + * 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.client; + +import lombok.Getter; +import lombok.NonNull; +import lombok.SneakyThrows; +import lombok.experimental.Accessors; +import lombok.extern.slf4j.Slf4j; +import me.braydon.pelican.action.PanelActions; +import me.braydon.pelican.action.pelican.PelicanPanelActions; +import me.braydon.pelican.action.pterodactyl.PteroPanelActions; +import me.braydon.pelican.request.WebRequestHandler; + +import java.io.Closeable; + +/** + * A client for the API. + * + * @param the actions for this client + * @author Braydon + */ +@Getter @Accessors(fluent = true) +@Slf4j(topic = "Pelican4J Client") +public final class Pelican4J implements Closeable { + /** + * The config for this client. + */ + @NonNull private final ClientConfig config; + + /** + * The actions for this client. + */ + @NonNull private final A actions; + + @SneakyThrows + private Pelican4J(@NonNull ClientConfig config, @NonNull Class actionsClass) { + this.config = config; + actions = actionsClass.getConstructor(WebRequestHandler.class).newInstance(new WebRequestHandler(config)); + if (config.debugging()) { + log.debug("Created a new {} client: {}", actionsClass == PelicanPanelActions.class ? "Pelican" : "Ptero", config); + } + } + + /** + * Cleanup this client and shutdown. + */ + @Override + public void close() { + // TODO: close any resources, e.g: websockets + } + + /** + * Create a new API client + * for the Pelican panel. + * + * @param config the client config + * @return the created client + */ + @NonNull + public static Pelican4J forPelican(@NonNull ClientConfig config) { + return new Pelican4J<>(config, PelicanPanelActions.class); + } + + /** + * Create a new API client + * for the Pterodactyl panel. + * + * @param config the client config + * @return the created client + */ + @NonNull + public static Pelican4J forPtero(@NonNull ClientConfig config) { + return new Pelican4J<>(config, PteroPanelActions.class); + } +} \ No newline at end of file diff --git a/src/main/java/me/braydon/pelican/exception/PanelAPIException.java b/src/main/java/me/braydon/pelican/exception/PanelAPIException.java new file mode 100644 index 0000000..8d5283b --- /dev/null +++ b/src/main/java/me/braydon/pelican/exception/PanelAPIException.java @@ -0,0 +1,53 @@ +/* + * 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.exception; + +import lombok.Getter; +import lombok.NonNull; +import lombok.ToString; + +/** + * This exception is raised when there is an + * error when interacting with the panel API. + * + * @author Braydon + */ +@Getter @ToString +public final class PanelAPIException extends RuntimeException { + /** + * The HTTP code of the exception. + */ + private final int code; + + /** + * The status of the exception. + */ + @NonNull private final String status; + + public PanelAPIException(int code, @NonNull String status, @NonNull String details) { + super(details); + this.code = code; + this.status = status; + } +} \ No newline at end of file diff --git a/src/main/java/me/braydon/pelican/model/Node.java b/src/main/java/me/braydon/pelican/model/Node.java new file mode 100644 index 0000000..44d7880 --- /dev/null +++ b/src/main/java/me/braydon/pelican/model/Node.java @@ -0,0 +1,70 @@ +/* + * 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.model; + +import com.google.gson.annotations.SerializedName; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NonNull; +import lombok.ToString; + +import java.util.UUID; + +/** + * A server node model. + * + * @author Braydon + */ +@AllArgsConstructor @Getter @ToString +public final class Node extends PanelModel { + /** + * The numericId of this node. + */ + private final int id; + + /** + * The UUID of this node. + */ + @NonNull private final UUID uuid; + + /** + * The name of this node. + */ + @NonNull private final String name; + + /** + * The description of this node, null if none. + */ + private final String description; + + /** + * The ID of the location of this node. + */ + @SerializedName("location_id") private final int locationId; + + /** + * Whether this node is public. + */ + @SerializedName("public") private final boolean publiclyVisible; +} \ No newline at end of file diff --git a/src/main/java/me/braydon/pelican/model/PanelModel.java b/src/main/java/me/braydon/pelican/model/PanelModel.java new file mode 100644 index 0000000..f452ae5 --- /dev/null +++ b/src/main/java/me/braydon/pelican/model/PanelModel.java @@ -0,0 +1,45 @@ +/* + * 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.model; + +import com.google.gson.annotations.SerializedName; +import lombok.*; + +/** + * A model for the panel. + * + * @author Braydon + */ +@NoArgsConstructor @Setter @Getter @ToString +public class PanelModel { + /** + * The type of this model. + */ + @NonNull private String object; + + /** + * The value of this model. + */ + @SerializedName("attributes") @NonNull private T value; +} \ No newline at end of file diff --git a/src/main/java/me/braydon/pelican/Pelican4J.java b/src/main/java/me/braydon/pelican/request/HttpMethod.java similarity index 89% rename from src/main/java/me/braydon/pelican/Pelican4J.java rename to src/main/java/me/braydon/pelican/request/HttpMethod.java index 54b3742..581df3d 100644 --- a/src/main/java/me/braydon/pelican/Pelican4J.java +++ b/src/main/java/me/braydon/pelican/request/HttpMethod.java @@ -21,9 +21,13 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package me.braydon.pelican; +package me.braydon.pelican.request; /** + * Methods for making HTTP requests. + * * @author Braydon */ -public final class Pelican4J { } \ No newline at end of file +public enum HttpMethod { + GET, POST, PATCH, DELETE +} \ 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 new file mode 100644 index 0000000..2b0c3ad --- /dev/null +++ b/src/main/java/me/braydon/pelican/request/JsonWebRequest.java @@ -0,0 +1,100 @@ +/* + * 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; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import lombok.Builder; +import lombok.NonNull; +import lombok.SneakyThrows; +import me.braydon.pelican.client.ClientConfig; +import me.braydon.pelican.exception.PanelAPIException; +import me.braydon.pelican.model.PanelModel; +import okhttp3.*; + +/** + * A json web request. + * + * @author Braydon + */ +@Builder +public class JsonWebRequest { + private static final OkHttpClient HTTP_CLIENT = new OkHttpClient(); + private static final MediaType JSON_MEDIA = MediaType.get("application/json"); + private static final Gson GSON = new GsonBuilder() + .serializeNulls() + .create(); + + /** + * The method of this request. + */ + @Builder.Default @NonNull private final HttpMethod method = HttpMethod.GET; + + /** + * The endpoint to make the request to. + */ + @NonNull private final String endpoint; + + /** + * The body to send in this request, null if none. + */ + private final String body; + + /** + * Execute this request. + * + * @param clientConfig the client config to use + * @param responseType the expected response type, null for none + * @return the response, null if none + * @param the response type + */ + @SneakyThrows + protected > T execute(@NonNull ClientConfig clientConfig, Class responseType) { + Request request = new Request.Builder() + .method(method.name(), body == null ? null : RequestBody.create(body, JSON_MEDIA)) + .url(clientConfig.panelUrl() + "/api" + endpoint) + .addHeader("Content-Type", "application/json") + .addHeader("Accept", "Application/vnd.pterodactyl.v1+json") + .addHeader("Authorization", "Bearer " + clientConfig.apiKey()) + .build(); // Build the request to send + + // Execute the request and receive the response + try (Response response = HTTP_CLIENT.newCall(request).execute()) { + int status = response.code(); // The HTTP response code + String json = response.body().string(); // The json response + + // If the status is not 200 (OK), handle the error + if (status != 200) { + 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()); + } + + // If there is a response expected, parse + // the received json and return it + return responseType == null ? null : GSON.fromJson(json, responseType).getValue(); + } + } +} \ No newline at end of file diff --git a/src/main/java/me/braydon/pelican/request/WebRequestHandler.java b/src/main/java/me/braydon/pelican/request/WebRequestHandler.java new file mode 100644 index 0000000..22322bc --- /dev/null +++ b/src/main/java/me/braydon/pelican/request/WebRequestHandler.java @@ -0,0 +1,55 @@ +/* + * 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; + +import lombok.AllArgsConstructor; +import lombok.NonNull; +import me.braydon.pelican.client.ClientConfig; +import me.braydon.pelican.model.PanelModel; + +/** + * The handler for processing web requests. + * + * @author Braydon + */ +@AllArgsConstructor +public final class WebRequestHandler { + /** + * The client config used to make requests. + */ + @NonNull private final ClientConfig clientConfig; + + /** + * Handle the given web request. + * + * @param request the request to handle + * @param responseType the expected response type, null if none + * @return the response, null if none + * @param the response type + */ + public > T handle(@NonNull JsonWebRequest request, Class responseType) { + // TODO: handle rate limit handling for async reqs + return request.execute(clientConfig, responseType); + } +} \ No newline at end of file diff --git a/src/test/java/me/braydon/pelican/test/PelicanActionTests.java b/src/test/java/me/braydon/pelican/test/PelicanActionTests.java index 0f81fd8..84cc95b 100644 --- a/src/test/java/me/braydon/pelican/test/PelicanActionTests.java +++ b/src/test/java/me/braydon/pelican/test/PelicanActionTests.java @@ -26,6 +26,8 @@ package me.braydon.pelican.test; import me.braydon.pelican.action.pelican.PelicanPanelActions; import me.braydon.pelican.client.ClientConfig; import me.braydon.pelican.client.Pelican4J; +import me.braydon.pelican.exception.PanelAPIException; +import me.braydon.pelican.model.Node; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -50,7 +52,8 @@ public final class PelicanActionTests { client = Pelican4J.forPelican(ClientConfig.builder() .panelUrl(System.getenv("TEST_PANEL_URL")) .apiKey(System.getenv("TEST_APPS_API_KEY")) - .build()); + .debugging(true) + .build()); // Create a new Pelican client } /** @@ -64,7 +67,8 @@ public final class PelicanActionTests { */ @Test void testGetNodes() { - client.getActions().application().nodes(); + Node node = client.actions().application().nodes().details(-1).execute(); + System.out.println("node = " + node); // TODO: ... } }