diff --git a/src/main/java/dev/salmonllama/fsbot/endpoints/fashionscape/guildconfig/GuildConfigApi.java b/src/main/java/dev/salmonllama/fsbot/endpoints/fashionscape/guildconfig/GuildConfigApi.java new file mode 100644 index 0000000..c1c7f07 --- /dev/null +++ b/src/main/java/dev/salmonllama/fsbot/endpoints/fashionscape/guildconfig/GuildConfigApi.java @@ -0,0 +1,59 @@ +package dev.salmonllama.fsbot.endpoints.fashionscape.guildconfig; + +import dev.salmonllama.fsbot.config.EnvManager; +import dev.salmonllama.fsbot.endpoints.ApiConnection; +import okhttp3.RequestBody; + +import java.io.IOException; +import java.util.HashMap; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; + +public class GuildConfigApi { + + private static final String GUILD_CONFIG_URI = EnvManager.API_URI + "/guildconfig"; + + private final ApiConnection conn; + + public GuildConfigApi() { + conn = new ApiConnection(GUILD_CONFIG_URI); + } + + public CompletableFuture create(GuildConfig cfg) { + RequestBody body = ApiConnection.marshall(cfg); + return CompletableFuture.allOf(conn.post(body)); + } + + public CompletableFuture get(String guildId) { + HashMap params = new HashMap<>(); + params.put("guildid", guildId); + + return conn.get(params).thenApplyAsync(response -> { + if (response.body() == null) { + throw new CompletionException(new RuntimeException("No response")); + } + + try { + return ApiConnection.unmarshall(response.body(), GuildConfig.class); + } catch (IOException e) { + throw new CompletionException(e); + } + }); + } + + public CompletableFuture update(GuildConfig cfg) { + HashMap params = new HashMap<>(); + params.put("guildid", cfg.getGuildId()); + + RequestBody body = ApiConnection.marshall(cfg); + + return CompletableFuture.allOf(conn.put(params, body)); + } + + public CompletableFuture delete(String guildId) { + HashMap params = new HashMap<>(); + params.put("guildid", guildId); + + return CompletableFuture.allOf(conn.delete(params)); + } +}