Skip to content

Commit

Permalink
Заменить HttpClient на Flurl (#835)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsokto authored and inyutin-maxim committed Apr 25, 2019
1 parent a08cff2 commit 18df8c0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 52 deletions.
69 changes: 19 additions & 50 deletions VkNet/Utils/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,14 +17,11 @@ namespace VkNet.Utils
[UsedImplicitly]
public class RestClient : IRestClient
{
private readonly HttpClient _httpClient;

private readonly ILogger<RestClient> _logger;

/// <inheritdoc />
public RestClient(HttpClient httpClient, ILogger<RestClient> logger)
public RestClient(ILogger<RestClient> logger)
{
_httpClient = httpClient;
_logger = logger;
}

Expand All @@ -37,20 +36,17 @@ public RestClient(HttpClient httpClient, ILogger<RestClient> logger)
/// <inheritdoc />
public Task<HttpResponse<string>> GetAsync(Uri uri, IEnumerable<KeyValuePair<string, string>> 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());
}

/// <inheritdoc />
Expand All @@ -64,14 +60,18 @@ public Task<HttpResponse<string>> PostAsync(Uri uri, IEnumerable<KeyValuePair<st

var content = new FormUrlEncodedContent(parameters);

var request = new HttpRequestMessage(HttpMethod.Post, uri) { Content = content };
return CallAsync(() => uri.ToString().PostAsync(content));
}

return CallAsync(httpClient => httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead));
/// <inheritdoc />
public void Dispose()
{
GC.SuppressFinalize(this);
}

private async Task<HttpResponse<string>> CallAsync(Func<HttpClient, Task<HttpResponseMessage>> method)
private async Task<HttpResponse<string>> CallAsync(Func<Task<HttpResponseMessage>> method)
{
var response = await method(_httpClient).ConfigureAwait(false);
var response = await method().ConfigureAwait(false);

var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

Expand All @@ -82,36 +82,5 @@ private async Task<HttpResponse<string>> CallAsync(Func<HttpClient, Task<HttpRes
? HttpResponse<string>.Success(response.StatusCode, content, url)
: HttpResponse<string>.Fail(response.StatusCode, content, url);
}

/// <inheritdoc />
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
~RestClient()
{
Dispose(false);
}

/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing">
/// <c> true </c> to release both managed and unmanaged resources; <c> false </c>
/// to release only
/// unmanaged resources.
/// </param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_httpClient?.Dispose();
}
}
}
}
2 changes: 0 additions & 2 deletions VkNet/Utils/TypeHelper.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -39,7 +38,6 @@ public static void RegisterDefaultDependencies(this IServiceCollection container
container.TryAddSingleton<ICaptchaHandler, CaptchaHandler>();
container.TryAddSingleton<ILanguageService, LanguageService>();
container.TryAddSingleton<ICaptchaSolver>(sp => null);
container.TryAddSingleton<HttpClient>();
container.TryAddSingleton<IRateLimiter, RateLimiter>();
container.TryAddSingleton<IAwaitableConstraint, CountByIntervalAwaitableConstraint>();
container.RegisterAuthorization();
Expand Down

0 comments on commit 18df8c0

Please sign in to comment.