Java SDK base

This commit is contained in:
Braydon 2024-04-23 03:29:09 -04:00
parent 88705f363b
commit 8e43044877
13 changed files with 658 additions and 0 deletions

27
Java-SDK/.gitignore vendored Normal file

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

6
Java-SDK/README.md Normal file

@ -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`

79
Java-SDK/pom.xml Normal file

@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--Project Details-->
<groupId>cc.restfulmc</groupId>
<artifactId>Java-SDK</artifactId>
<version>1.0.0</version>
<!-- 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>
</properties>
<build>
<plugins>
<!-- Used for compiling the source code with the proper Java version -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<!-- Enable incremental builds, this is reversed due to -->
<!-- a bug as seen in https://issues.apache.org/jira/browse/MCOMPILER-209 -->
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>
<!-- Handles shading of dependencies in the final output jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.2</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!-- Dependencies -->
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.32</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>5.0.0-alpha.14</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

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

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

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

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

@ -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<Player> getPlayer(@NonNull String query) {
return CompletableFuture.supplyAsync(() -> sendGetPlayerRequest(query));
}
}

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

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

@ -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> T execute(@NonNull Class<T> 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
}
}
}

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

@ -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?
* <p>
* A "Legacy" player is a player that
* has not yet migrated to a Mojang account.
* </p>
*/
private final boolean legacy;
}