Add some types and test make resquests

This commit is contained in:
Guilherme Werner 2024-01-05 09:59:24 -03:00
parent 8a45692208
commit 3e6f8251a7
12 changed files with 310 additions and 2 deletions

View file

@ -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<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
* grantValue, String clientId,

View file

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

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

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

View file

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

View file

@ -0,0 +1,9 @@
// Copyright (c) Tribufu. All Rights Reserved.
package com.tribufu.types.servers;
public enum ServerStatus {
Unknown,
Offline,
Online
}

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

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

View file

@ -0,0 +1,9 @@
// Copyright (c) Tribufu. All Rights Reserved.
package com.tribufu.types.users;
public enum UserType {
User,
Bot,
Org,
}