#pragma warning disable using System.Net.Http.Headers; using System.Text; namespace The_Untamed_Music_Player.OnlineAPIs.CloudMusicAPI.System.Extensions; internal static class HttpClientExtensions { public static Task SendAsync(this HttpClient client, HttpMethod method, string url) => client.SendAsync(method, url, null, null); public static Task SendAsync(this HttpClient client, HttpMethod method, string url, IEnumerable> queries, IEnumerable> headers) => client.SendAsync(method, url, queries, headers, (byte[])null, "application/x-www-form-urlencoded"); public static Task SendAsync(this HttpClient client, HttpMethod method, string url, IEnumerable> queries, IEnumerable> headers, string content, string contentType) => client.SendAsync(method, url, queries, headers, content is null ? null : Encoding.UTF8.GetBytes(content), contentType); public static Task SendAsync(this HttpClient client, HttpMethod method, string url, IEnumerable> queries, IEnumerable> headers, byte[] content, string contentType) { ArgumentNullException.ThrowIfNull(client); ArgumentNullException.ThrowIfNull(method); if (string.IsNullOrEmpty(url)) { throw new ArgumentNullException(nameof(url)); } if (string.IsNullOrEmpty(contentType)) { throw new ArgumentNullException(nameof(contentType)); } UriBuilder uriBuilder; HttpRequestMessage request; uriBuilder = new UriBuilder(url); if (queries is not null) { string query; query = queries.ToQueryString(); if (!string.IsNullOrEmpty(query)) { if (string.IsNullOrEmpty(uriBuilder.Query)) { uriBuilder.Query = query; } else { uriBuilder.Query += "&" + query; } } } request = new HttpRequestMessage(method, uriBuilder.Uri); if (content is not null) { request.Content = new ByteArrayContent(content); } else if (queries is not null && method != HttpMethod.Get) { request.Content = new FormUrlEncodedContent(queries); } if (request.Content is not null) { request.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType); } if (headers is not null) { foreach (var header in headers) { request.Headers.TryAddWithoutValidation(header.Key, header.Value); } } return client.SendAsync(request); } }