summary refs log tree commit diff
path: root/extra/admin-api/Utilities
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2026-06-05 22:42:33 +0200
committerRory& <root@rory.gay>2026-06-05 22:42:33 +0200
commitd109d871d75d7ee707bb33342d5fed6865124166 (patch)
tree8685f6caa2dde878528eaa67ede8da803f734eb1 /extra/admin-api/Utilities
parentReturn url on webhook create (diff)
downloadserver-ts-d109d871d75d7ee707bb33342d5fed6865124166.tar.xz
Unit tests for webhooks
Diffstat (limited to 'extra/admin-api/Utilities')
-rw-r--r--extra/admin-api/Utilities/Spacebar.Sdk/Core/SpacebarClient.cs36
1 files changed, 36 insertions, 0 deletions
diff --git a/extra/admin-api/Utilities/Spacebar.Sdk/Core/SpacebarClient.cs b/extra/admin-api/Utilities/Spacebar.Sdk/Core/SpacebarClient.cs

index 5843b2710..2b4a4ba47 100644 --- a/extra/admin-api/Utilities/Spacebar.Sdk/Core/SpacebarClient.cs +++ b/extra/admin-api/Utilities/Spacebar.Sdk/Core/SpacebarClient.cs
@@ -73,6 +73,13 @@ public class AuthenticatedSpacebarClient { if (!resp.IsSuccessStatusCode) throw SpacebarApiException.FromJson((await resp.Content.ReadFromJsonAsync<JsonObject>())!); return (await resp.Content.ReadFromJsonAsync<Guild>())!; } + + public async Task<List<Guild>> GetJoinedGuilds() { + var resp = await ApiHttpClient.GetAsync("users/@me/guilds"); + // TODO: abstract out + if (!resp.IsSuccessStatusCode) throw SpacebarApiException.FromJson((await resp.Content.ReadFromJsonAsync<JsonObject>())!); + return (await resp.Content.ReadFromJsonAsync<List<Guild>>())!; + } } public class SpacebarClientChannel(AuthenticatedSpacebarClient client, long channelId) { @@ -91,6 +98,22 @@ public class SpacebarClientChannel(AuthenticatedSpacebarClient client, long chan Console.WriteLine(data.ToJson(indent: false, ignoreNull: true)); return data.Select(x => x.Deserialize<Message>()).ToList(); } + + public async Task<Webhook> CreateWebhookAsync(CreateWebhookRequest req) { + var resp = await client.ApiHttpClient.PostAsJsonAsync($"channels/{channelId}/webhooks", req, new JsonSerializerOptions() { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull + }); + // TODO: abstract out + if (!resp.IsSuccessStatusCode) throw SpacebarApiException.FromJson((await resp.Content.ReadFromJsonAsync<JsonObject>())!); + return (await resp.Content.ReadFromJsonAsync<Webhook>())!; + } + + public async Task<List<Webhook>> GetWebhooksAsync() { + var resp = await client.ApiHttpClient.GetAsync($"channels/{channelId}/webhooks"); + // TODO: abstract out + if (!resp.IsSuccessStatusCode) throw SpacebarApiException.FromJson((await resp.Content.ReadFromJsonAsync<JsonObject>())!); + return (await resp.Content.ReadFromJsonAsync<List<Webhook>>())!; + } } public class SpacebarClientGuild(AuthenticatedSpacebarClient client, long guildId) { @@ -115,6 +138,19 @@ public class SpacebarClientGuild(AuthenticatedSpacebarClient client, long guildI if (!resp.IsSuccessStatusCode) throw SpacebarApiException.FromJson((await resp.Content.ReadFromJsonAsync<JsonObject>())!); return (await resp.Content.ReadFromJsonAsync<Channel>())!; } + + public async Task LeaveAsync(bool lurking = false) { + var req = new HttpRequestMessage(HttpMethod.Delete, $"users/@me/guilds/{guildId}") { + Content = new StringContent(new JsonObject() { + { "lurking", lurking } + }.ToJsonString(new JsonSerializerOptions() { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull + })) + }; + var resp = await client.ApiHttpClient.SendAsync(req); + // TODO: abstract out + if (!resp.IsSuccessStatusCode) throw SpacebarApiException.FromJson((await resp.Content.ReadFromJsonAsync<JsonObject>())!); + } } public class AuthenticatedSpacebarGatewayClient(ILogger<AuthenticatedSpacebarGatewayClient> logger, SpacebarClientWellKnown wellKnown, string token) {