feat: internal API class for Guild Configs
Some checks failed
Java CI with Gradle / build (push) Has been cancelled
Some checks failed
Java CI with Gradle / build (push) Has been cancelled
This commit is contained in:
parent
a915138828
commit
4d8d68af70
@ -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<Void> create(GuildConfig cfg) {
|
||||
RequestBody body = ApiConnection.marshall(cfg);
|
||||
return CompletableFuture.allOf(conn.post(body));
|
||||
}
|
||||
|
||||
public CompletableFuture<GuildConfig> get(String guildId) {
|
||||
HashMap<String, String> 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<Void> update(GuildConfig cfg) {
|
||||
HashMap<String, String> params = new HashMap<>();
|
||||
params.put("guildid", cfg.getGuildId());
|
||||
|
||||
RequestBody body = ApiConnection.marshall(cfg);
|
||||
|
||||
return CompletableFuture.allOf(conn.put(params, body));
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> delete(String guildId) {
|
||||
HashMap<String, String> params = new HashMap<>();
|
||||
params.put("guildid", guildId);
|
||||
|
||||
return CompletableFuture.allOf(conn.delete(params));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user