Add Minecraft server lookups to the Java SDK
This commit is contained in:
parent
cf5ee56b64
commit
10669f1b4a
@ -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 <T> the server type
|
||||
* @throws RESTfulMCAPIException if an api error occurs
|
||||
*/
|
||||
@NonNull @SuppressWarnings("unchecked")
|
||||
protected final <T extends MinecraftServer> 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());
|
||||
}
|
||||
}
|
@ -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<Player> 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 <T> the server type
|
||||
* @throws RESTfulMCAPIException in the future if an api error occurs
|
||||
*/
|
||||
@NonNull
|
||||
public <T extends MinecraftServer> CompletableFuture<T> getMinecraftServer(@NonNull MinecraftServer.Platform platform, @NonNull String hostname) {
|
||||
return CompletableFuture.supplyAsync(() -> sendGetMinecraftServerRequest(platform, hostname));
|
||||
}
|
||||
}
|
@ -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 <T> the server type
|
||||
* @throws RESTfulMCAPIException if an api error occurs
|
||||
*/
|
||||
@NonNull
|
||||
public <T extends MinecraftServer> T getMinecraftServer(@NonNull MinecraftServer.Platform platform, @NonNull String hostname) throws RESTfulMCAPIException {
|
||||
return sendGetMinecraftServerRequest(platform, hostname);
|
||||
}
|
||||
}
|
@ -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();
|
@ -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 <T> the response type
|
||||
* @throws RESTfulMCAPIException if an api error occurs
|
||||
*/
|
||||
@SneakyThrows @NonNull
|
||||
public <T> T execute(@NonNull Class<T> responseType) {
|
||||
public <T> T execute(@NonNull Class<T> 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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user