You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

HttpClientFactoryRestClient.cs 8.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using Discord.Net.Converters;
  2. using Discord.Net.Rest;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Net.Http;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace Discord.Rest.Net
  15. {
  16. internal sealed class HttpClientFactoryRestClient : IRestClient, IDisposable
  17. {
  18. private const int HR_SECURECHANNELFAILED = -2146233079;
  19. private readonly HttpClient _client;
  20. private readonly string _baseUrl;
  21. private readonly JsonSerializer _errorDeserializer;
  22. private CancellationToken _cancelToken;
  23. private bool _isDisposed;
  24. public HttpClientFactoryRestClient(string baseUrl, HttpClient httpClient, bool useProxy = false)
  25. {
  26. _baseUrl = baseUrl;
  27. //this client would be given to use through DI. The advantage would be that it would be managed by DI, and no socket Exhaustation possible.
  28. _client = httpClient;
  29. //#pragma warning disable IDISP014
  30. // _client = new HttpClient(new HttpClientHandler
  31. // {
  32. // AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
  33. // UseCookies = false,
  34. // UseProxy = useProxy,
  35. // });
  36. //#pragma warning restore IDISP014
  37. SetHeader("accept-encoding", "gzip, deflate");
  38. _cancelToken = CancellationToken.None;
  39. _errorDeserializer = new JsonSerializer();
  40. }
  41. private void Dispose(bool disposing)
  42. {
  43. if (!_isDisposed)
  44. {
  45. if (disposing)
  46. _client.Dispose();
  47. _isDisposed = true;
  48. }
  49. }
  50. public void Dispose()
  51. {
  52. Dispose(true);
  53. }
  54. public void SetHeader(string key, string value)
  55. {
  56. _client.DefaultRequestHeaders.Remove(key);
  57. if (value != null)
  58. _client.DefaultRequestHeaders.Add(key, value);
  59. }
  60. public void SetCancelToken(CancellationToken cancelToken)
  61. {
  62. _cancelToken = cancelToken;
  63. }
  64. public async Task<RestResponse> SendAsync(string method, string endpoint, CancellationToken cancelToken, bool headerOnly, string reason = null)
  65. {
  66. string uri = Path.Combine(_baseUrl, endpoint);
  67. using (var restRequest = new HttpRequestMessage(GetMethod(method), uri))
  68. {
  69. if (reason != null)
  70. restRequest.Headers.Add("X-Audit-Log-Reason", Uri.EscapeDataString(reason));
  71. return await SendInternalAsync(restRequest, cancelToken, headerOnly).ConfigureAwait(false);
  72. }
  73. }
  74. public async Task<RestResponse> SendAsync(string method, string endpoint, string json, CancellationToken cancelToken, bool headerOnly, string reason = null)
  75. {
  76. string uri = Path.Combine(_baseUrl, endpoint);
  77. using (var restRequest = new HttpRequestMessage(GetMethod(method), uri))
  78. {
  79. if (reason != null)
  80. restRequest.Headers.Add("X-Audit-Log-Reason", Uri.EscapeDataString(reason));
  81. restRequest.Content = new StringContent(json, Encoding.UTF8, "application/json");
  82. return await SendInternalAsync(restRequest, cancelToken, headerOnly).ConfigureAwait(false);
  83. }
  84. }
  85. /// <exception cref="InvalidOperationException">Unsupported param type.</exception>
  86. public async Task<RestResponse> SendAsync(string method, string endpoint, IReadOnlyDictionary<string, object> multipartParams, CancellationToken cancelToken, bool headerOnly, string reason = null)
  87. {
  88. string uri = Path.Combine(_baseUrl, endpoint);
  89. using (var restRequest = new HttpRequestMessage(GetMethod(method), uri))
  90. {
  91. if (reason != null)
  92. restRequest.Headers.Add("X-Audit-Log-Reason", Uri.EscapeDataString(reason));
  93. var content = new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture));
  94. MemoryStream memoryStream = null;
  95. if (multipartParams != null)
  96. {
  97. foreach (var p in multipartParams)
  98. {
  99. switch (p.Value)
  100. {
  101. #pragma warning disable IDISP004
  102. case string stringValue:
  103. { content.Add(new StringContent(stringValue), p.Key); continue; }
  104. case byte[] byteArrayValue:
  105. { content.Add(new ByteArrayContent(byteArrayValue), p.Key); continue; }
  106. case Stream streamValue:
  107. { content.Add(new StreamContent(streamValue), p.Key); continue; }
  108. case MultipartFile fileValue:
  109. {
  110. var stream = fileValue.Stream;
  111. if (!stream.CanSeek)
  112. {
  113. memoryStream = new MemoryStream();
  114. await stream.CopyToAsync(memoryStream).ConfigureAwait(false);
  115. memoryStream.Position = 0;
  116. #pragma warning disable IDISP001
  117. stream = memoryStream;
  118. #pragma warning restore IDISP001
  119. }
  120. content.Add(new StreamContent(stream), p.Key, fileValue.Filename);
  121. #pragma warning restore IDISP004
  122. continue;
  123. }
  124. default:
  125. throw new InvalidOperationException($"Unsupported param type \"{p.Value.GetType().Name}\".");
  126. }
  127. }
  128. }
  129. restRequest.Content = content;
  130. var result = await SendInternalAsync(restRequest, cancelToken, headerOnly).ConfigureAwait(false);
  131. memoryStream?.Dispose();
  132. return result;
  133. }
  134. }
  135. private async Task<RestResponse> SendInternalAsync(HttpRequestMessage request, CancellationToken cancelToken, bool headerOnly)
  136. {
  137. using (var cancelTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_cancelToken, cancelToken))
  138. {
  139. cancelToken = cancelTokenSource.Token;
  140. HttpResponseMessage response = await _client.SendAsync(request, cancelToken).ConfigureAwait(false);
  141. var headers = response.Headers.ToDictionary(x => x.Key, x => x.Value.FirstOrDefault(), StringComparer.OrdinalIgnoreCase);
  142. var stream = !headerOnly ? await response.Content.ReadAsStreamAsync().ConfigureAwait(false) : null;
  143. return new RestResponse(response.StatusCode, headers, stream);
  144. }
  145. }
  146. private static readonly HttpMethod Patch = new HttpMethod("PATCH");
  147. private HttpMethod GetMethod(string method)
  148. {
  149. switch (method)
  150. {
  151. case "DELETE":
  152. return HttpMethod.Delete;
  153. case "GET":
  154. return HttpMethod.Get;
  155. case "PATCH":
  156. return Patch;
  157. case "POST":
  158. return HttpMethod.Post;
  159. case "PUT":
  160. return HttpMethod.Put;
  161. default:
  162. throw new ArgumentOutOfRangeException(nameof(method), $"Unknown HttpMethod: {method}");
  163. }
  164. }
  165. }
  166. }