Return both HttpResponseMessage and the JSON data.

This commit is contained in:
Filip Strajnar 2024-06-16 10:11:53 +02:00
parent 2ab1d8d34d
commit 28b67a0263

View file

@ -13,21 +13,22 @@ public class DiscordUserClient
{
_httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Host = "discord.com";
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", authorizationToken);
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation(
"Authorization",
authorizationToken
);
}
public async Task<ChannelMessagesResponse[]?> GetChannelMessages(
string channelId,
int offset = 0,
string? authorId = null,
int limit = 50
)
public async Task<(
HttpResponseMessage Response,
ChannelMessagesResponse[]? JsonData
)> GetChannelMessages(string channelId, int offset = 0, string? authorId = null, int limit = 50)
{
string authorQuery = authorId is not null ? $"&author_id={authorId}" : "";
string url =
$"https://discord.com/api/v9/channels/{channelId}/messages?limit={limit}&offset={offset}{authorQuery}";
HttpResponseMessage response = await _httpClient.GetAsync(url);
return await response.Content.ReadFromJsonAsync<ChannelMessagesResponse[]>();
return (response, await response.Content.ReadFromJsonAsync<ChannelMessagesResponse[]>());
}
sealed class MessageContent
@ -65,16 +66,15 @@ public class DiscordUserClient
/// recent messages from this particular author.
/// </param>
/// <returns></returns>
public async Task<GuildMessageSearchResponse?> SearchGuildMessages(
string guildId,
int offset,
string? authorId = null
)
public async Task<(
HttpResponseMessage Response,
GuildMessageSearchResponse? JsonData
)> SearchGuildMessages(string guildId, int offset, string? authorId = null)
{
string authorQuery = authorId is not null ? $"&author_id={authorId}" : "";
string url =
$"https://discord.com/api/v9/guilds/{guildId}/messages/search?offset={offset}{authorQuery}";
HttpResponseMessage response = await _httpClient.GetAsync(url);
return await response.Content.ReadFromJsonAsync<GuildMessageSearchResponse>();
return (response, await response.Content.ReadFromJsonAsync<GuildMessageSearchResponse>());
}
}