From 2075146506870d51a9c7d61955fb237357ce627a Mon Sep 17 00:00:00 2001 From: emorell96 Date: Tue, 23 Mar 2021 22:33:30 -0700 Subject: [PATCH] implemented the SendAsync that takes IEnumerable> in order to create a FormUrlEncodedContent and send the request with UrlEncoded content. --- src/Discord.Net.Rest/Net/DefaultRestClient.cs | 11 ++++++++ .../Net/HttpClientFactoryRestClient.cs | 26 +++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/Discord.Net.Rest/Net/DefaultRestClient.cs b/src/Discord.Net.Rest/Net/DefaultRestClient.cs index b5036d94e..2bbe068bf 100644 --- a/src/Discord.Net.Rest/Net/DefaultRestClient.cs +++ b/src/Discord.Net.Rest/Net/DefaultRestClient.cs @@ -84,6 +84,17 @@ namespace Discord.Net.Rest return await SendInternalAsync(restRequest, cancelToken, headerOnly).ConfigureAwait(false); } } + public async Task SendAsync(string method, string endpoint, IEnumerable> formDataContent, CancellationToken cancelToken, bool headerOnly, string reason = null) + { + string uri = Path.Combine(_baseUrl, endpoint); + using (var restRequest = new HttpRequestMessage(GetMethod(method), uri)) + { + if (reason != null) + restRequest.Headers.Add("X-Audit-Log-Reason", Uri.EscapeDataString(reason)); + restRequest.Content = new FormUrlEncodedContent(formDataContent); // json, Encoding.UTF8, "application/json"); + return await SendInternalAsync(restRequest, cancelToken, headerOnly).ConfigureAwait(false); + } + } /// Unsupported param type. public async Task SendAsync(string method, string endpoint, IReadOnlyDictionary multipartParams, CancellationToken cancelToken, bool headerOnly, string reason = null) diff --git a/src/Discord.Net.Rest/Net/HttpClientFactoryRestClient.cs b/src/Discord.Net.Rest/Net/HttpClientFactoryRestClient.cs index 7a1154d1c..1aa29b273 100644 --- a/src/Discord.Net.Rest/Net/HttpClientFactoryRestClient.cs +++ b/src/Discord.Net.Rest/Net/HttpClientFactoryRestClient.cs @@ -91,9 +91,30 @@ namespace Discord.Rest.Net return await SendInternalAsync(restRequest, cancelToken, headerOnly).ConfigureAwait(false); } } + /// + /// This is the new SendAsync method required for sending UrlEncoded data. This is needed for Oauth2 stuff -> refresh token, and get a new token from a code. + /// + /// + /// + /// + /// + /// + /// + /// + public async Task SendAsync(string method, string endpoint, IEnumerable> formDataContent, CancellationToken cancelToken, bool headerOnly, string reason = null) + { + string uri = Path.Combine(_baseUrl, endpoint); + using (var restRequest = new HttpRequestMessage(GetMethod(method), uri)) + { + if (reason != null) + restRequest.Headers.Add("X-Audit-Log-Reason", Uri.EscapeDataString(reason)); + restRequest.Content = new FormUrlEncodedContent(formDataContent); // json, Encoding.UTF8, "application/json"); + return await SendInternalAsync(restRequest, cancelToken, headerOnly).ConfigureAwait(false); + } + } - /// Unsupported param type. - public async Task SendAsync(string method, string endpoint, IReadOnlyDictionary multipartParams, CancellationToken cancelToken, bool headerOnly, string reason = null) + /// Unsupported param type. + public async Task SendAsync(string method, string endpoint, IReadOnlyDictionary multipartParams, CancellationToken cancelToken, bool headerOnly, string reason = null) { string uri = Path.Combine(_baseUrl, endpoint); using (var restRequest = new HttpRequestMessage(GetMethod(method), uri)) @@ -149,6 +170,7 @@ namespace Discord.Rest.Net { cancelToken = cancelTokenSource.Token; HttpResponseMessage response = await _client.SendAsync(request, cancelToken).ConfigureAwait(false); + var str = await response.Content.ReadAsStringAsync(); var headers = response.Headers.ToDictionary(x => x.Key, x => x.Value.FirstOrDefault(), StringComparer.OrdinalIgnoreCase);