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