From 18df8c031832138537961abad3c60a3750ff1f9c Mon Sep 17 00:00:00 2001 From: itsokto <36706257+itsokto@users.noreply.github.com> Date: Thu, 25 Apr 2019 16:35:36 +0300 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=BC=D0=B5=D0=BD=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20HttpClient=20=D0=BD=D0=B0=20Flurl=20(#835)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VkNet/Utils/RestClient.cs | 69 +++++++++++---------------------------- VkNet/Utils/TypeHelper.cs | 2 -- 2 files changed, 19 insertions(+), 52 deletions(-) diff --git a/VkNet/Utils/RestClient.cs b/VkNet/Utils/RestClient.cs index 40fa88061..140aa3dfb 100644 --- a/VkNet/Utils/RestClient.cs +++ b/VkNet/Utils/RestClient.cs @@ -4,6 +4,8 @@ using System.Net; using System.Net.Http; using System.Threading.Tasks; +using Flurl; +using Flurl.Http; using JetBrains.Annotations; using Microsoft.Extensions.Logging; using Newtonsoft.Json; @@ -15,14 +17,11 @@ namespace VkNet.Utils [UsedImplicitly] public class RestClient : IRestClient { - private readonly HttpClient _httpClient; - private readonly ILogger _logger; /// - public RestClient(HttpClient httpClient, ILogger logger) + public RestClient(ILogger logger) { - _httpClient = httpClient; _logger = logger; } @@ -37,20 +36,17 @@ public RestClient(HttpClient httpClient, ILogger logger) /// public Task> GetAsync(Uri uri, IEnumerable> parameters) { - var queries = parameters - .Where(parameter => !string.IsNullOrWhiteSpace(parameter.Value)) - .Select(parameter => $"{parameter.Key.ToLowerInvariant()}={parameter.Value}"); - - var url = new UriBuilder(uri) + if (_logger != null) { - Query = string.Join("&", queries) - }; + var uriBuilder = new UriBuilder(uri) + { + Query = string.Join("&", parameters.Select(x => $"{x.Key}={x.Value}")) + }; - _logger?.LogDebug($"GET request: {url.Uri}"); - - var request = new HttpRequestMessage(HttpMethod.Get, url.Uri); + _logger.LogDebug($"GET request: {uriBuilder.Uri}"); + } - return CallAsync(httpClient => httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)); + return CallAsync(() => uri.ToString().SetQueryParams(parameters).GetAsync()); } /// @@ -64,14 +60,18 @@ public Task> PostAsync(Uri uri, IEnumerable uri.ToString().PostAsync(content)); + } - return CallAsync(httpClient => httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)); + /// + public void Dispose() + { + GC.SuppressFinalize(this); } - private async Task> CallAsync(Func> method) + private async Task> CallAsync(Func> method) { - var response = await method(_httpClient).ConfigureAwait(false); + var response = await method().ConfigureAwait(false); var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); @@ -82,36 +82,5 @@ private async Task> CallAsync(Func.Success(response.StatusCode, content, url) : HttpResponse.Fail(response.StatusCode, content, url); } - - /// - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - /// - /// Releases unmanaged and - optionally - managed resources. - /// - ~RestClient() - { - Dispose(false); - } - - /// - /// Releases unmanaged and - optionally - managed resources. - /// - /// - /// true to release both managed and unmanaged resources; false - /// to release only - /// unmanaged resources. - /// - protected virtual void Dispose(bool disposing) - { - if (disposing) - { - _httpClient?.Dispose(); - } - } } } \ No newline at end of file diff --git a/VkNet/Utils/TypeHelper.cs b/VkNet/Utils/TypeHelper.cs index f215661e4..46c96f2bd 100644 --- a/VkNet/Utils/TypeHelper.cs +++ b/VkNet/Utils/TypeHelper.cs @@ -1,6 +1,5 @@ using System; using System.Net; -using System.Net.Http; using System.Threading.Tasks; using Flurl.Http.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -39,7 +38,6 @@ public static void RegisterDefaultDependencies(this IServiceCollection container container.TryAddSingleton(); container.TryAddSingleton(); container.TryAddSingleton(sp => null); - container.TryAddSingleton(); container.TryAddSingleton(); container.TryAddSingleton(); container.RegisterAuthorization();