diff --git a/examples/build.gradle b/examples/build.gradle new file mode 100644 index 0000000..54a64af --- /dev/null +++ b/examples/build.gradle @@ -0,0 +1,20 @@ +plugins { + id 'application' +} + +version = "0.0.4" +archivesBaseName = "TribufuExample" + +repositories { + mavenCentral() +} + +dependencies { + implementation project(':lib') + implementation 'com.google.guava:guava:30.0-jre' + testImplementation 'junit:junit:4.13.1' +} + +application { + mainClass = 'com.tribufu.example.ExampleApp' +} diff --git a/examples/src/main/java/com/tribufu/example/ExampleApp.java b/examples/src/main/java/com/tribufu/example/ExampleApp.java new file mode 100644 index 0000000..3f7fff5 --- /dev/null +++ b/examples/src/main/java/com/tribufu/example/ExampleApp.java @@ -0,0 +1,23 @@ +// Copyright (c) Tribufu. All Rights Reserved. + +package com.tribufu.example; + +import com.tribufu.TribufuApi; + +public class ExampleApp { + public static void main(String[] args) { + var api = new TribufuApi(); + + var games = api.getGames(); + System.out.println("\n---- GAMES ----"); + for (var game : games) { + System.out.println(game.name); + } + + var servers = api.getServers(); + System.out.println("\n---- SERVERS ----"); + for (var server : servers) { + System.out.println(server.name); + } + } +} diff --git a/lib/src/main/java/com/tribufu/TribufuApi.java b/lib/src/main/java/com/tribufu/TribufuApi.java index ddb4093..063660f 100644 --- a/lib/src/main/java/com/tribufu/TribufuApi.java +++ b/lib/src/main/java/com/tribufu/TribufuApi.java @@ -3,8 +3,15 @@ package com.tribufu; import com.tribufu.http.TribufuHttp; +import com.tribufu.types.games.Game; +import com.tribufu.types.servers.Server; +import com.tribufu.types.users.Profile; + +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.concurrent.CompletableFuture; /** * Tribufu API @@ -19,7 +26,7 @@ import java.util.Map; */ public class TribufuApi { private static final String VERSION = "0.0.0"; - private static final String API_URL = "https://api.tribufu.com"; + private static final String API_URL = "http://localhost:5000"; protected final String baseUrl; protected final TribufuApiOptions options; @@ -160,6 +167,95 @@ public class TribufuApi { return headers; } + /** + * Get games from the Tribufu API. + * + * @param page + */ + public List getGames() { + Map headers = this.getHeaders(); + return this.http.getArray("/v1/packages", headers, Game[].class); + } + + /** + * Get a game from the Tribufu API. + * + * @param id + */ + public CompletableFuture getGameById(String id) { + Map headers = this.getHeaders(); + return this.http.get("/v1/packages/" + id, headers, Game.class); + } + + /** + * Get a game from the Tribufu API. + * + * @param page + */ + public List getServers() { + Map headers = this.getHeaders(); + return this.http.getArray("/v1/servers", headers, Server[].class); + } + + /** + * Get a server by id from the Tribufu API. + * + * @param id + */ + public CompletableFuture getServerById(String id) { + Map headers = this.getHeaders(); + return this.http.get("/v1/servers/" + id, headers, Server.class); + } + + /** + * Get a server by address from the Tribufu API. + * + * @param id + */ + public CompletableFuture getServerByAddress(String address) { + Map headers = this.getHeaders(); + return this.http.get("/v1/servers/address/" + address, headers, Server.class); + } + + /** + * Get a user by id from the Tribufu API. + * + * @param id + */ + public CompletableFuture getUsersById(String id) { + Map headers = this.getHeaders(); + return this.http.get("/v1/users/" + id, headers, Profile.class); + } + + /** + * Get a user by name from the Tribufu API. + * + * @param uuid + */ + public CompletableFuture getUsersByUuid(String uuid) { + return this.getUserByKey("uuid", uuid); + } + + /** + * Get a user by email from the Tribufu API. + * + * @param email + */ + public CompletableFuture getUsersByName(String email) { + return this.getUserByKey("email", email); + } + + /** + * Get a user by custom key from the Tribufu API. + * + * @param key + * @param value + */ + private CompletableFuture getUserByKey(String key, String value) { + Map headers = this.getHeaders(); + return this.http.get("/v1/users?" + key + "=" + value, headers, Profile.class); + } + /* * private OAuth2TokenResponse getOAuthToken(OAuth2GrantType grantType, String * grantValue, String clientId, diff --git a/lib/src/main/java/com/tribufu/http/TribufuHttp.java b/lib/src/main/java/com/tribufu/http/TribufuHttp.java index 38829ca..19497cf 100644 --- a/lib/src/main/java/com/tribufu/http/TribufuHttp.java +++ b/lib/src/main/java/com/tribufu/http/TribufuHttp.java @@ -4,12 +4,16 @@ package com.tribufu.http; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.URI; import java.util.concurrent.CompletableFuture; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import java.util.Map; /** @@ -85,6 +89,32 @@ public class TribufuHttp { return sendRequest(request, returnType); } + /** + * Make a GET request to the Tribufu API and deserialize the response JSON. + * + * @param path The path of the resource. + * @param headers Optional headers. + * @param returnType The type to deserialize the response into. + * @return A CompletableFuture containing the deserialized response object. + */ + public List getArray(String path, Map headers, Class returnType) { + var requestBuilder = HttpRequest.newBuilder(); + + for (var entry : headers.entrySet()) { + requestBuilder.header(entry.getKey(), entry.getValue()); + } + + var request = requestBuilder + .uri(URI.create(this.options.baseUrl + path)) + .GET() + .build(); + + var response = sendRequest(request); + T[] array = new Gson().fromJson(response.join(), returnType); + + return Arrays.asList(array); + } + /** * Make a POST request to the Tribufu API and deserialize the response JSON. * diff --git a/lib/src/main/java/com/tribufu/types/games/Game.java b/lib/src/main/java/com/tribufu/types/games/Game.java new file mode 100644 index 0000000..7c2b38b --- /dev/null +++ b/lib/src/main/java/com/tribufu/types/games/Game.java @@ -0,0 +1,24 @@ +// Copyright (c) Tribufu. All Rights Reserved. + +package com.tribufu.types.games; + +public class Game { + public String id; + public String name; + public String description; + public String iconUrl; + public String bannerUrl; + public String capsuleImageUrl; + public String libraryImageUrl; + public String slug; + public Integer gamePort; + public Integer queryPort; + public Integer rconPort; + public String steamAppId; + public String steamServerAppId; + public String rustGamedigId; + public String nodeGamedigId; + public String serverConnectUrl; + public String created; + public String updated; +} diff --git a/lib/src/main/java/com/tribufu/types/servers/Server.java b/lib/src/main/java/com/tribufu/types/servers/Server.java new file mode 100644 index 0000000..2f34907 --- /dev/null +++ b/lib/src/main/java/com/tribufu/types/servers/Server.java @@ -0,0 +1,43 @@ +// Copyright (c) Tribufu. All Rights Reserved. + +package com.tribufu.types.servers; + +import com.tribufu.types.users.MiniProfile; + +public class Server { + public long id; + public String name; + public String description; + public String address; + public Short gamePort; + public short queryPort; + public Short rconPort; + public String rconPassword; + public long packageId; + public String packageIconUrl; + public ServerPackage packageObj; + public String version; + public Integer clusterId; + public String websiteUrl; + public String bannerUrl; + public Long ownerId; + public MiniProfile owner; + public double uptime; + public String lastOnline; + public ServerStatus status; + public Integer ping; + public String map; + public Integer usedSlots; + public Integer maxSlots; + public String motd; + public Object players; + public String country; + public boolean steam; + public String discordServerId; + public String youtubeVideoUrl; + public Object tags; + public int commentCount; + public String secret; + public String created; + public String updated; +} diff --git a/lib/src/main/java/com/tribufu/types/servers/ServerPackage.java b/lib/src/main/java/com/tribufu/types/servers/ServerPackage.java new file mode 100644 index 0000000..223e12a --- /dev/null +++ b/lib/src/main/java/com/tribufu/types/servers/ServerPackage.java @@ -0,0 +1,12 @@ +// Copyright (c) Tribufu. All Rights Reserved. + +package com.tribufu.types.servers; + +public class ServerPackage { + public long id; + public String name; + public String slug; + public String rustGamedigId; + public String nodeGamedigId; + public String serverConnectUrl; +} diff --git a/lib/src/main/java/com/tribufu/types/servers/ServerStatus.java b/lib/src/main/java/com/tribufu/types/servers/ServerStatus.java new file mode 100644 index 0000000..62fa8cc --- /dev/null +++ b/lib/src/main/java/com/tribufu/types/servers/ServerStatus.java @@ -0,0 +1,9 @@ +// Copyright (c) Tribufu. All Rights Reserved. + +package com.tribufu.types.servers; + +public enum ServerStatus { + Unknown, + Offline, + Online +} diff --git a/lib/src/main/java/com/tribufu/types/users/MiniProfile.java b/lib/src/main/java/com/tribufu/types/users/MiniProfile.java new file mode 100644 index 0000000..4751755 --- /dev/null +++ b/lib/src/main/java/com/tribufu/types/users/MiniProfile.java @@ -0,0 +1,13 @@ +// Copyright (c) Tribufu. All Rights Reserved. + +package com.tribufu.types.users; + +public class MiniProfile { + public long id; + public String uuid; + public String name; + public String displayName; + public UserType kind; + public boolean verified; + public String photoUrl; +} diff --git a/lib/src/main/java/com/tribufu/types/users/Profile.java b/lib/src/main/java/com/tribufu/types/users/Profile.java new file mode 100644 index 0000000..6db252b --- /dev/null +++ b/lib/src/main/java/com/tribufu/types/users/Profile.java @@ -0,0 +1,28 @@ +// Copyright (c) Tribufu. All Rights Reserved. + +package com.tribufu.types.users; + +import java.time.LocalDate; + +public class Profile { + public long id; + public String uuid; + public String name; + public String displayName; + public UserType kind; + public long publicFlags; + public boolean verified; + public int level; + public double experience; + public boolean publicBirthday; + public LocalDate birthday; + public double points; + public String location; + public String photoUrl; + public String bannerUrl; + public String lastOnline; + public String biography; + public int viewCount; + public String created; + public String updated; +} diff --git a/lib/src/main/java/com/tribufu/types/users/UserType.java b/lib/src/main/java/com/tribufu/types/users/UserType.java new file mode 100644 index 0000000..af28909 --- /dev/null +++ b/lib/src/main/java/com/tribufu/types/users/UserType.java @@ -0,0 +1,9 @@ +// Copyright (c) Tribufu. All Rights Reserved. + +package com.tribufu.types.users; + +public enum UserType { + User, + Bot, + Org, +} diff --git a/settings.gradle b/settings.gradle index 165c9c5..901c9f6 100644 --- a/settings.gradle +++ b/settings.gradle @@ -2,5 +2,6 @@ plugins { id 'org.gradle.toolchains.foojay-resolver-convention' version '0.4.0' } -rootProject.name = 'SDK-JVM' +rootProject.name = 'TribufuJvm' include('lib') +include('examples')