diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/command/ClientCommands.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/command/ClientCommands.java index 772bd54..45862b8 100644 --- a/Java-SDK/src/main/java/cc/restfulmc/sdk/command/ClientCommands.java +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/command/ClientCommands.java @@ -24,9 +24,14 @@ package cc.restfulmc.sdk.command; import cc.restfulmc.sdk.client.ClientConfig; +import cc.restfulmc.sdk.exception.RESTfulMCAPIException; import cc.restfulmc.sdk.request.APIWebRequest; import cc.restfulmc.sdk.response.Player; -import lombok.*; +import cc.restfulmc.sdk.response.server.MinecraftServer; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NonNull; /** * An executor to make @@ -46,11 +51,29 @@ public abstract class ClientCommands { * * @param query the player uuid or username * @return the found player + * @throws RESTfulMCAPIException if an api error occurs */ - @NonNull @SneakyThrows - protected final Player sendGetPlayerRequest(@NonNull String query) { + @NonNull + protected final Player sendGetPlayerRequest(@NonNull String query) throws RESTfulMCAPIException { return APIWebRequest.builder() .endpoint(config.getApiEndpoint() + "/player/" + query) .build().execute(Player.class); } + + /** + * Get a Minecraft server by its platform and hostname. + * + * @param platform the platform of the server + * @param hostname the hostname of the server + * @return the server + * @param the server type + * @throws RESTfulMCAPIException if an api error occurs + */ + @NonNull @SuppressWarnings("unchecked") + protected final T sendGetMinecraftServerRequest(@NonNull MinecraftServer.Platform platform, + @NonNull String hostname) throws RESTfulMCAPIException { + return (T) APIWebRequest.builder() + .endpoint(config.getApiEndpoint() + "/server/" + platform.name() + "/" + hostname) + .build().execute(platform.getServerClass()); + } } \ No newline at end of file diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/command/impl/AsyncClientCommands.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/command/impl/AsyncClientCommands.java index eedf2a8..f80e157 100644 --- a/Java-SDK/src/main/java/cc/restfulmc/sdk/command/impl/AsyncClientCommands.java +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/command/impl/AsyncClientCommands.java @@ -26,7 +26,9 @@ package cc.restfulmc.sdk.command.impl; import cc.restfulmc.sdk.client.ClientConfig; import cc.restfulmc.sdk.client.RESTfulMCClient; import cc.restfulmc.sdk.command.ClientCommands; +import cc.restfulmc.sdk.exception.RESTfulMCAPIException; import cc.restfulmc.sdk.response.Player; +import cc.restfulmc.sdk.response.server.MinecraftServer; import lombok.NonNull; import java.util.concurrent.CompletableFuture; @@ -46,9 +48,24 @@ public final class AsyncClientCommands extends ClientCommands { * * @param query the player uuid or username * @return the found player + * @throws RESTfulMCAPIException in the future if an api error occurs */ @NonNull public CompletableFuture getPlayer(@NonNull String query) { return CompletableFuture.supplyAsync(() -> sendGetPlayerRequest(query)); } + + /** + * Get a Minecraft server by its platform and hostname. + * + * @param platform the platform of the server + * @param hostname the hostname of the server + * @return the server + * @param the server type + * @throws RESTfulMCAPIException in the future if an api error occurs + */ + @NonNull + public CompletableFuture getMinecraftServer(@NonNull MinecraftServer.Platform platform, @NonNull String hostname) { + return CompletableFuture.supplyAsync(() -> sendGetMinecraftServerRequest(platform, hostname)); + } } \ No newline at end of file diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/command/impl/SyncClientCommands.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/command/impl/SyncClientCommands.java index 4486382..b444ddd 100644 --- a/Java-SDK/src/main/java/cc/restfulmc/sdk/command/impl/SyncClientCommands.java +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/command/impl/SyncClientCommands.java @@ -26,7 +26,9 @@ package cc.restfulmc.sdk.command.impl; import cc.restfulmc.sdk.client.ClientConfig; import cc.restfulmc.sdk.client.RESTfulMCClient; import cc.restfulmc.sdk.command.ClientCommands; +import cc.restfulmc.sdk.exception.RESTfulMCAPIException; import cc.restfulmc.sdk.response.Player; +import cc.restfulmc.sdk.response.server.MinecraftServer; import lombok.NonNull; /** @@ -44,9 +46,24 @@ public final class SyncClientCommands extends ClientCommands { * * @param query the player uuid or username * @return the found player + * @throws RESTfulMCAPIException if an api error occurs */ @NonNull - public Player getPlayer(@NonNull String query) { + public Player getPlayer(@NonNull String query) throws RESTfulMCAPIException { return sendGetPlayerRequest(query); } + + /** + * Get a Minecraft server by its platform and hostname. + * + * @param platform the platform of the server + * @param hostname the hostname of the server + * @return the server + * @param the server type + * @throws RESTfulMCAPIException if an api error occurs + */ + @NonNull + public T getMinecraftServer(@NonNull MinecraftServer.Platform platform, @NonNull String hostname) throws RESTfulMCAPIException { + return sendGetMinecraftServerRequest(platform, hostname); + } } \ No newline at end of file diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/exception/RestfulMCAPIException.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/exception/RESTfulMCAPIException.java similarity index 91% rename from Java-SDK/src/main/java/cc/restfulmc/sdk/exception/RestfulMCAPIException.java rename to Java-SDK/src/main/java/cc/restfulmc/sdk/exception/RESTfulMCAPIException.java index c926aa0..492a205 100644 --- a/Java-SDK/src/main/java/cc/restfulmc/sdk/exception/RestfulMCAPIException.java +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/exception/RESTfulMCAPIException.java @@ -37,7 +37,7 @@ import lombok.ToString; * @author Braydon */ @Getter @ToString -public final class RestfulMCAPIException extends Exception { +public final class RESTfulMCAPIException extends RuntimeException { /** * The status code of this error. */ @@ -53,11 +53,11 @@ public final class RestfulMCAPIException extends Exception { */ @NonNull private final String timestamp; - public RestfulMCAPIException(@NonNull String json) { + public RESTfulMCAPIException(@NonNull String json) { this(RESTfulMCClient.GSON.fromJson(json, JsonObject.class)); } - private RestfulMCAPIException(@NonNull JsonObject jsonObject) { + private RESTfulMCAPIException(@NonNull JsonObject jsonObject) { super(jsonObject.get("message").getAsString()); status = jsonObject.get("status").getAsString(); code = jsonObject.get("code").getAsInt(); diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/request/APIWebRequest.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/request/APIWebRequest.java index 5427eac..d5e0d17 100644 --- a/Java-SDK/src/main/java/cc/restfulmc/sdk/request/APIWebRequest.java +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/request/APIWebRequest.java @@ -24,7 +24,7 @@ package cc.restfulmc.sdk.request; import cc.restfulmc.sdk.client.RESTfulMCClient; -import cc.restfulmc.sdk.exception.RestfulMCAPIException; +import cc.restfulmc.sdk.exception.RESTfulMCAPIException; import lombok.Builder; import lombok.Getter; import lombok.NonNull; @@ -45,8 +45,16 @@ public final class APIWebRequest { */ @NonNull private final String endpoint; + /** + * Execute this request. + * + * @param responseType the response type class + * @return the response + * @param the response type + * @throws RESTfulMCAPIException if an api error occurs + */ @SneakyThrows @NonNull - public T execute(@NonNull Class responseType) { + public T execute(@NonNull Class responseType) throws RESTfulMCAPIException { Request request = new Request.Builder() .url(endpoint) .build(); // Build the request @@ -54,7 +62,7 @@ public final class APIWebRequest { int status = response.code(); // The response status code String json = response.body().string(); // The json response if (status != 200) { // Not 200 (OK), throw an exception - throw new RestfulMCAPIException(json); + throw new RESTfulMCAPIException(json); } return RESTfulMCClient.GSON.fromJson(json, responseType); // Return the response }