mirror of
https://github.com/tribufu/tribufu-java
synced 2026-06-01 09:42:37 +00:00
Add some types and test make resquests
This commit is contained in:
parent
8a45692208
commit
3e6f8251a7
12 changed files with 310 additions and 2 deletions
20
examples/build.gradle
Normal file
20
examples/build.gradle
Normal file
|
|
@ -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'
|
||||||
|
}
|
||||||
23
examples/src/main/java/com/tribufu/example/ExampleApp.java
Normal file
23
examples/src/main/java/com/tribufu/example/ExampleApp.java
Normal file
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,8 +3,15 @@
|
||||||
package com.tribufu;
|
package com.tribufu;
|
||||||
|
|
||||||
import com.tribufu.http.TribufuHttp;
|
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.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tribufu API
|
* Tribufu API
|
||||||
|
|
@ -19,7 +26,7 @@ import java.util.Map;
|
||||||
*/
|
*/
|
||||||
public class TribufuApi {
|
public class TribufuApi {
|
||||||
private static final String VERSION = "0.0.0";
|
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 String baseUrl;
|
||||||
protected final TribufuApiOptions options;
|
protected final TribufuApiOptions options;
|
||||||
|
|
@ -160,6 +167,95 @@ public class TribufuApi {
|
||||||
return headers;
|
return headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get games from the Tribufu API.
|
||||||
|
*
|
||||||
|
* @param page
|
||||||
|
*/
|
||||||
|
public List<Game> getGames() {
|
||||||
|
Map<String, String> headers = this.getHeaders();
|
||||||
|
return this.http.getArray("/v1/packages", headers, Game[].class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a game from the Tribufu API.
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
*/
|
||||||
|
public CompletableFuture<Game> getGameById(String id) {
|
||||||
|
Map<String, String> headers = this.getHeaders();
|
||||||
|
return this.http.get("/v1/packages/" + id, headers, Game.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a game from the Tribufu API.
|
||||||
|
*
|
||||||
|
* @param page
|
||||||
|
*/
|
||||||
|
public List<Server> getServers() {
|
||||||
|
Map<String, String> 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<Server> getServerById(String id) {
|
||||||
|
Map<String, String> 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<Server> getServerByAddress(String address) {
|
||||||
|
Map<String, String> 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<Profile> getUsersById(String id) {
|
||||||
|
Map<String, String> 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<Profile> getUsersByUuid(String uuid) {
|
||||||
|
return this.getUserByKey("uuid", uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a user by email from the Tribufu API.
|
||||||
|
*
|
||||||
|
* @param email
|
||||||
|
*/
|
||||||
|
public CompletableFuture<Profile> getUsersByName(String email) {
|
||||||
|
return this.getUserByKey("email", email);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a user by custom key from the Tribufu API.
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
* @param value
|
||||||
|
*/
|
||||||
|
private CompletableFuture<Profile> getUserByKey(String key, String value) {
|
||||||
|
Map<String, String> headers = this.getHeaders();
|
||||||
|
return this.http.get("/v1/users?" + key + "=" + value, headers, Profile.class);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* private OAuth2TokenResponse getOAuthToken(OAuth2GrantType grantType, String
|
* private OAuth2TokenResponse getOAuthToken(OAuth2GrantType grantType, String
|
||||||
* grantValue, String clientId,
|
* grantValue, String clientId,
|
||||||
|
|
|
||||||
|
|
@ -4,12 +4,16 @@ package com.tribufu.http;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
|
||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
import java.net.http.HttpRequest;
|
import java.net.http.HttpRequest;
|
||||||
import java.net.http.HttpResponse;
|
import java.net.http.HttpResponse;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -85,6 +89,32 @@ public class TribufuHttp {
|
||||||
return sendRequest(request, returnType);
|
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 <T> List<T> getArray(String path, Map<String, String> headers, Class<T[]> 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.
|
* Make a POST request to the Tribufu API and deserialize the response JSON.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
24
lib/src/main/java/com/tribufu/types/games/Game.java
Normal file
24
lib/src/main/java/com/tribufu/types/games/Game.java
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
43
lib/src/main/java/com/tribufu/types/servers/Server.java
Normal file
43
lib/src/main/java/com/tribufu/types/servers/Server.java
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
// Copyright (c) Tribufu. All Rights Reserved.
|
||||||
|
|
||||||
|
package com.tribufu.types.servers;
|
||||||
|
|
||||||
|
public enum ServerStatus {
|
||||||
|
Unknown,
|
||||||
|
Offline,
|
||||||
|
Online
|
||||||
|
}
|
||||||
13
lib/src/main/java/com/tribufu/types/users/MiniProfile.java
Normal file
13
lib/src/main/java/com/tribufu/types/users/MiniProfile.java
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
28
lib/src/main/java/com/tribufu/types/users/Profile.java
Normal file
28
lib/src/main/java/com/tribufu/types/users/Profile.java
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
9
lib/src/main/java/com/tribufu/types/users/UserType.java
Normal file
9
lib/src/main/java/com/tribufu/types/users/UserType.java
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
// Copyright (c) Tribufu. All Rights Reserved.
|
||||||
|
|
||||||
|
package com.tribufu.types.users;
|
||||||
|
|
||||||
|
public enum UserType {
|
||||||
|
User,
|
||||||
|
Bot,
|
||||||
|
Org,
|
||||||
|
}
|
||||||
|
|
@ -2,5 +2,6 @@ plugins {
|
||||||
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.4.0'
|
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.4.0'
|
||||||
}
|
}
|
||||||
|
|
||||||
rootProject.name = 'SDK-JVM'
|
rootProject.name = 'TribufuJvm'
|
||||||
include('lib')
|
include('lib')
|
||||||
|
include('examples')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue