This commit is contained in:
Braydon 2024-04-28 01:55:50 -04:00
parent 5e601168f9
commit 93fde2621d
15 changed files with 800 additions and 6 deletions

26
pom.xml

@ -12,9 +12,8 @@
<!-- Properties -->
<properties>
<java.version>8</java.version>
<!-- <maven.compiler.source>${java.version}</maven.compiler.source>-->
<!-- <maven.compiler.target>${java.version}</maven.compiler.target>-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4j.version>2.1.0-alpha1</slf4j.version>
</properties>
<!-- Build Config -->
@ -171,6 +170,7 @@
<scope>provided</scope>
</dependency>
<!-- Libraries -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
@ -183,5 +183,27 @@
<version>2.10.1</version>
<scope>compile</scope>
</dependency>
<!-- Tests -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

@ -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 <T> the type of response expected when this action is executed
* @author Braydon
*/
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class PanelAction<T extends PanelModel<T>> {
/**
* 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<T> 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 <T> the response type
* @return the panel action
*/
@NonNull
public static <T extends PanelModel<T>> PanelAction<T> create(@NonNull WebRequestHandler requestHandler,
@NonNull JsonWebRequest webRequest, Class<T> responseType) {
return new PanelAction<>(requestHandler, webRequest, responseType);
}
}

@ -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;
}

@ -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 <a href="https://pelican.dev">Pelican Website</a>
*/
public class PelicanPanelActions extends PteroPanelActions {
public PelicanPanelActions(@NonNull WebRequestHandler requestHandler) {
super(requestHandler);
}
}

@ -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 <a href="https://pterodactyl.io">Pterodactyl Website</a>
*/
@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());
}
}

@ -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<Node> details(int id) {
return PanelAction.create(requestHandler(), JsonWebRequest.builder()
.endpoint("/application/nodes/" + id)
.build(), Node.class);
}
}

@ -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;
}
}
}

@ -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 <A> the actions for this client
* @author Braydon
*/
@Getter @Accessors(fluent = true)
@Slf4j(topic = "Pelican4J Client")
public final class Pelican4J<A extends PanelActions> 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<A> 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<PelicanPanelActions> 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<PteroPanelActions> forPtero(@NonNull ClientConfig config) {
return new Pelican4J<>(config, PteroPanelActions.class);
}
}

@ -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;
}
}

@ -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<Node> {
/**
* 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;
}

@ -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<T> {
/**
* The type of this model.
*/
@NonNull private String object;
/**
* The value of this model.
*/
@SerializedName("attributes") @NonNull private T value;
}

@ -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 { }
public enum HttpMethod {
GET, POST, PATCH, DELETE
}

@ -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 <T> the response type
*/
@SneakyThrows
protected <T extends PanelModel<T>> T execute(@NonNull ClientConfig clientConfig, Class<T> 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();
}
}
}

@ -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 <T> the response type
*/
public <T extends PanelModel<T>> T handle(@NonNull JsonWebRequest request, Class<T> responseType) {
// TODO: handle rate limit handling for async reqs
return request.execute(clientConfig, responseType);
}
}

@ -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: ...
}
}