From 8e43044877097aebddd1049444f1db8fdce93e93 Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Tue, 23 Apr 2024 03:29:09 -0400 Subject: [PATCH] Java SDK base --- Java-SDK/.gitignore | 27 +++++++ Java-SDK/README.md | 6 ++ Java-SDK/pom.xml | 79 +++++++++++++++++++ .../src/main/java/cc/restfulmc/sdk/Testy.java | 41 ++++++++++ .../cc/restfulmc/sdk/client/ClientConfig.java | 58 ++++++++++++++ .../restfulmc/sdk/client/RESTfulMCClient.java | 64 +++++++++++++++ .../restfulmc/sdk/command/ClientCommands.java | 56 +++++++++++++ .../sdk/command/impl/AsyncClientCommands.java | 54 +++++++++++++ .../sdk/command/impl/SyncClientCommands.java | 52 ++++++++++++ .../sdk/exception/RestfulMCAPIException.java | 62 +++++++++++++++ .../restfulmc/sdk/request/APIWebRequest.java | 62 +++++++++++++++ .../sdk/response/CacheableResponse.java | 40 ++++++++++ .../cc/restfulmc/sdk/response/Player.java | 57 +++++++++++++ 13 files changed, 658 insertions(+) create mode 100644 Java-SDK/.gitignore create mode 100644 Java-SDK/README.md create mode 100644 Java-SDK/pom.xml create mode 100644 Java-SDK/src/main/java/cc/restfulmc/sdk/Testy.java create mode 100644 Java-SDK/src/main/java/cc/restfulmc/sdk/client/ClientConfig.java create mode 100644 Java-SDK/src/main/java/cc/restfulmc/sdk/client/RESTfulMCClient.java create mode 100644 Java-SDK/src/main/java/cc/restfulmc/sdk/command/ClientCommands.java create mode 100644 Java-SDK/src/main/java/cc/restfulmc/sdk/command/impl/AsyncClientCommands.java create mode 100644 Java-SDK/src/main/java/cc/restfulmc/sdk/command/impl/SyncClientCommands.java create mode 100644 Java-SDK/src/main/java/cc/restfulmc/sdk/exception/RestfulMCAPIException.java create mode 100644 Java-SDK/src/main/java/cc/restfulmc/sdk/request/APIWebRequest.java create mode 100644 Java-SDK/src/main/java/cc/restfulmc/sdk/response/CacheableResponse.java create mode 100644 Java-SDK/src/main/java/cc/restfulmc/sdk/response/Player.java diff --git a/Java-SDK/.gitignore b/Java-SDK/.gitignore new file mode 100644 index 0000000..cab2341 --- /dev/null +++ b/Java-SDK/.gitignore @@ -0,0 +1,27 @@ +*.class +*.log +*.ctxt +.mtj.tmp/ +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar +hs_err_pid* +replay_pid* +.idea +cmake-build-*/ +.idea/**/mongoSettings.xml +*.iws +out/ +build/ +target/ +.idea_modules/ +atlassian-ide-plugin.xml +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties +pom.xml.versionsBackup \ No newline at end of file diff --git a/Java-SDK/README.md b/Java-SDK/README.md new file mode 100644 index 0000000..457e770 --- /dev/null +++ b/Java-SDK/README.md @@ -0,0 +1,6 @@ +[![Publish Workflow](https://git.rainnny.club/Rainnny/RESTfulMC/actions/workflows/publish-java-sdk.yml/badge.svg)](./actions?workflow=publish-java-sdk.yml) + +# RESTfulMC Java Library +The Java SDK for the RESTfulMC API. + +`Not done` \ No newline at end of file diff --git a/Java-SDK/pom.xml b/Java-SDK/pom.xml new file mode 100644 index 0000000..43f2179 --- /dev/null +++ b/Java-SDK/pom.xml @@ -0,0 +1,79 @@ + + + 4.0.0 + + + cc.restfulmc + Java-SDK + 1.0.0 + + + + 8 + ${java.version} + ${java.version} + UTF-8 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + ${java.version} + ${java.version} + + + + false + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.5.2 + + false + + + + package + + shade + + + + + + + + + + + org.projectlombok + lombok + 1.18.32 + provided + + + + com.squareup.okhttp3 + okhttp + 5.0.0-alpha.14 + compile + + + com.google.code.gson + gson + 2.10.1 + compile + + + \ No newline at end of file diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/Testy.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/Testy.java new file mode 100644 index 0000000..56f2dfa --- /dev/null +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/Testy.java @@ -0,0 +1,41 @@ +/* + * 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 cc.restfulmc.sdk; + +import cc.restfulmc.sdk.client.ClientConfig; +import cc.restfulmc.sdk.client.RESTfulMCClient; +import cc.restfulmc.sdk.response.Player; +import lombok.SneakyThrows; + +/** + * @author Braydon + */ +public final class Testy { + @SneakyThrows + public static void main(String[] args) { + RESTfulMCClient client = new RESTfulMCClient(ClientConfig.defaultConfig()); // Create the client + Player player = client.sync().getPlayer("Rainnny"); + System.out.println("player = " + player); + } +} \ No newline at end of file diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/client/ClientConfig.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/client/ClientConfig.java new file mode 100644 index 0000000..b574772 --- /dev/null +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/client/ClientConfig.java @@ -0,0 +1,58 @@ +/* + * 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 cc.restfulmc.sdk.client; + +import lombok.Builder; +import lombok.Getter; +import lombok.NonNull; + +/** + * Configuration for a {@link RESTfulMCClient}. + * + * @author Braydon + */ +@Builder @Getter +public final class ClientConfig { + /** + * The API endpoint to make requests to. + */ + @NonNull private final String apiEndpoint; + + /** + * Should debugging be enabled? + */ + private final boolean debugging; + + /** + * Get the default client config. + * + * @return the default config + */ + @NonNull + public static ClientConfig defaultConfig() { + return ClientConfig.builder() + .apiEndpoint("https://api.restfulmc.cc") + .build(); + } +} \ No newline at end of file diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/client/RESTfulMCClient.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/client/RESTfulMCClient.java new file mode 100644 index 0000000..d192b2a --- /dev/null +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/client/RESTfulMCClient.java @@ -0,0 +1,64 @@ +/* + * 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 cc.restfulmc.sdk.client; + +import cc.restfulmc.sdk.command.impl.AsyncClientCommands; +import cc.restfulmc.sdk.command.impl.SyncClientCommands; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NonNull; +import lombok.experimental.Accessors; + +/** + * @author Braydon + */ +@Getter @Accessors(fluent = true) +public final class RESTfulMCClient { + public static final Gson GSON = new GsonBuilder() + .serializeNulls() + .create(); + + /** + * The config for this client. + */ + @NonNull @Getter(AccessLevel.NONE) private final ClientConfig config; + + /** + * Synchronized commands for this client. + */ + @NonNull private final SyncClientCommands sync; + + /** + * Asynchronous commands for this client. + */ + @NonNull private final AsyncClientCommands async; + + public RESTfulMCClient(@NonNull ClientConfig config) { + this.config = config; + sync = new SyncClientCommands(config); + async = new AsyncClientCommands(config); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..772bd54 --- /dev/null +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/command/ClientCommands.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 cc.restfulmc.sdk.command; + +import cc.restfulmc.sdk.client.ClientConfig; +import cc.restfulmc.sdk.request.APIWebRequest; +import cc.restfulmc.sdk.response.Player; +import lombok.*; + +/** + * An executor to make + * requests to the API with. + * + * @author Braydon + */ +@AllArgsConstructor(access = AccessLevel.PROTECTED) @Getter(AccessLevel.PROTECTED) +public abstract class ClientCommands { + /** + * The config to make requests with. + */ + @NonNull private final ClientConfig config; + + /** + * Get a player by their username or UUID. + * + * @param query the player uuid or username + * @return the found player + */ + @NonNull @SneakyThrows + protected final Player sendGetPlayerRequest(@NonNull String query) { + return APIWebRequest.builder() + .endpoint(config.getApiEndpoint() + "/player/" + query) + .build().execute(Player.class); + } +} \ 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 new file mode 100644 index 0000000..eedf2a8 --- /dev/null +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/command/impl/AsyncClientCommands.java @@ -0,0 +1,54 @@ +/* + * 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 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.response.Player; +import lombok.NonNull; + +import java.util.concurrent.CompletableFuture; + +/** + * Asynchronous commands for the {@link RESTfulMCClient}. + * + * @author Braydon + */ +public final class AsyncClientCommands extends ClientCommands { + public AsyncClientCommands(@NonNull ClientConfig config) { + super(config); + } + + /** + * Get a player by their username or UUID. + * + * @param query the player uuid or username + * @return the found player + */ + @NonNull + public CompletableFuture getPlayer(@NonNull String query) { + return CompletableFuture.supplyAsync(() -> sendGetPlayerRequest(query)); + } +} \ 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 new file mode 100644 index 0000000..4486382 --- /dev/null +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/command/impl/SyncClientCommands.java @@ -0,0 +1,52 @@ +/* + * 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 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.response.Player; +import lombok.NonNull; + +/** + * Synchronized commands for the {@link RESTfulMCClient}. + * + * @author Braydon + */ +public final class SyncClientCommands extends ClientCommands { + public SyncClientCommands(@NonNull ClientConfig config) { + super(config); + } + + /** + * Get a player by their username or UUID. + * + * @param query the player uuid or username + * @return the found player + */ + @NonNull + public Player getPlayer(@NonNull String query) { + return sendGetPlayerRequest(query); + } +} \ 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 new file mode 100644 index 0000000..be1f08d --- /dev/null +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/exception/RestfulMCAPIException.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 cc.restfulmc.sdk.exception; + +import cc.restfulmc.sdk.client.RESTfulMCClient; +import com.google.gson.JsonObject; +import lombok.Getter; +import lombok.NonNull; +import lombok.ToString; + +/** + * @author Braydon + */ +@Getter @ToString +public final class RestfulMCAPIException extends Exception { + /** + * The status code of this error. + */ + @NonNull private final String status; + + /** + * The HTTP code of this error. + */ + private final int code; + + /** + * The timestamp this error occurred. + */ + @NonNull private final String timestamp; + + public RestfulMCAPIException(@NonNull String json) { + this(RESTfulMCClient.GSON.fromJson(json, JsonObject.class)); + } + + private RestfulMCAPIException(@NonNull JsonObject jsonObject) { + super(jsonObject.get("message").getAsString()); + status = jsonObject.get("status").getAsString(); + code = jsonObject.get("code").getAsInt(); + timestamp = jsonObject.get("timestamp").getAsString(); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..5427eac --- /dev/null +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/request/APIWebRequest.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 cc.restfulmc.sdk.request; + +import cc.restfulmc.sdk.client.RESTfulMCClient; +import cc.restfulmc.sdk.exception.RestfulMCAPIException; +import lombok.Builder; +import lombok.Getter; +import lombok.NonNull; +import lombok.SneakyThrows; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +/** + * @author Braydon + */ +@Builder @Getter +public final class APIWebRequest { + private static final OkHttpClient HTTP_CLIENT = new OkHttpClient(); + + /** + * The endpoint to make the request to. + */ + @NonNull private final String endpoint; + + @SneakyThrows @NonNull + public T execute(@NonNull Class responseType) { + Request request = new Request.Builder() + .url(endpoint) + .build(); // Build the request + try (Response response = HTTP_CLIENT.newCall(request).execute()) { + 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); + } + return RESTfulMCClient.GSON.fromJson(json, responseType); // Return the response + } + } +} \ No newline at end of file diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/response/CacheableResponse.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/response/CacheableResponse.java new file mode 100644 index 0000000..08870fe --- /dev/null +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/response/CacheableResponse.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 cc.restfulmc.sdk.response; + +import lombok.Getter; +import lombok.ToString; + +/** + * A cached response. + * + * @author Braydon + */ +@Getter @ToString +public abstract class CacheableResponse { + /** + * The unix timestamp of when the response was cached. + */ + private long cached; +} \ No newline at end of file diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/response/Player.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/response/Player.java new file mode 100644 index 0000000..0eb2730 --- /dev/null +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/response/Player.java @@ -0,0 +1,57 @@ +/* + * 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 cc.restfulmc.sdk.response; + +import lombok.*; + +import java.util.UUID; + +/** + * A representation of a player. + * + * @author Braydon + */ +@AllArgsConstructor @Getter +@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = false) +@ToString(callSuper = true) +public final class Player extends CacheableResponse { + /** + * The unique id of this player. + */ + @NonNull private final UUID uniqueId; + + /** + * The username of this player. + */ + @NonNull private final String username; + + /** + * Is this player legacy? + *

+ * A "Legacy" player is a player that + * has not yet migrated to a Mojang account. + *

+ */ + private final boolean legacy; +} \ No newline at end of file