From a4642999e62c8ee64a07087c51a0fdc0f4f09daa Mon Sep 17 00:00:00 2001 From: itsokto <36706257+itsokto@users.noreply.github.com> Date: Thu, 25 Apr 2019 20:20:16 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BE=D0=BD=D1=84=D0=B8=D0=B3=D1=83?= =?UTF-8?q?=D1=80=D0=B0=D1=86=D0=B8=D1=8F=20Flurl=20(#836)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Разрешить Flurl ответы не только с 2ХХ статус кодом * Убрать глобальную настройку Flurl --- .../ImplicitFlow/AbstractAuthorizationForm.cs | 14 ++----- .../Authorization/ImplicitFlow/ConsentForm.cs | 5 +-- .../ImplicitFlowCaptchaLoginForm.cs | 10 ++--- .../ImplicitFlow/ImplicitFlowLoginForm.cs | 7 ++-- .../NoRedirectHttpClientFactory.cs | 31 -------------- .../ImplicitFlow/ProxyHttpClientFactory.cs | 41 ------------------- .../ImplicitFlow/TwoFactorForm.cs | 6 +-- VkNet/Utils/RestClient.cs | 5 +-- VkNet/Utils/TypeHelper.cs | 7 ---- VkNet/VkApi.cs | 6 --- 10 files changed, 17 insertions(+), 115 deletions(-) delete mode 100644 VkNet/Infrastructure/Authorization/ImplicitFlow/NoRedirectHttpClientFactory.cs delete mode 100644 VkNet/Infrastructure/Authorization/ImplicitFlow/ProxyHttpClientFactory.cs diff --git a/VkNet/Infrastructure/Authorization/ImplicitFlow/AbstractAuthorizationForm.cs b/VkNet/Infrastructure/Authorization/ImplicitFlow/AbstractAuthorizationForm.cs index 95206013f..e41ebc358 100644 --- a/VkNet/Infrastructure/Authorization/ImplicitFlow/AbstractAuthorizationForm.cs +++ b/VkNet/Infrastructure/Authorization/ImplicitFlow/AbstractAuthorizationForm.cs @@ -3,7 +3,6 @@ using System.Threading.Tasks; using Flurl; using Flurl.Http; -using Flurl.Http.Configuration; using VkNet.Enums; using VkNet.Exception; @@ -14,17 +13,10 @@ public abstract class AbstractAuthorizationForm : IAuthorizationForm { private readonly IAuthorizationFormHtmlParser _htmlParser; - private readonly DefaultHttpClientFactory _httpClientFactory; - - private readonly IFlurlClientFactory _clientFactory; - /// - protected AbstractAuthorizationForm(IAuthorizationFormHtmlParser htmlParser, DefaultHttpClientFactory httpClientFactory, - IFlurlClientFactory clientFactory) + protected AbstractAuthorizationForm(IAuthorizationFormHtmlParser htmlParser) { _htmlParser = htmlParser; - _httpClientFactory = httpClientFactory; - _clientFactory = clientFactory; } /// @@ -37,7 +29,7 @@ public async Task ExecuteAsync(Url url) FillFormFields(form); - using (var cli = _clientFactory.Get(form.Action)) + using (var cli = new FlurlClient(form.Action).EnableCookies()) { var responseMessage = await cli.Request() .PostMultipartAsync(mp => mp.Add(new FormUrlEncodedContent(form.Fields))) @@ -67,7 +59,7 @@ public async Task ExecuteAsync(Url url) /// /// Заполнение полей формы /// - /// Форма + /// Форма protected abstract void FillFormFields(VkHtmlFormResult form); } } \ No newline at end of file diff --git a/VkNet/Infrastructure/Authorization/ImplicitFlow/ConsentForm.cs b/VkNet/Infrastructure/Authorization/ImplicitFlow/ConsentForm.cs index 4df3db3ac..3f7ce9500 100644 --- a/VkNet/Infrastructure/Authorization/ImplicitFlow/ConsentForm.cs +++ b/VkNet/Infrastructure/Authorization/ImplicitFlow/ConsentForm.cs @@ -1,4 +1,3 @@ -using Flurl.Http.Configuration; using JetBrains.Annotations; using VkNet.Enums; @@ -9,8 +8,8 @@ namespace VkNet.Infrastructure.Authorization.ImplicitFlow public sealed class ConsentForm : AbstractAuthorizationForm { /// - public ConsentForm(IAuthorizationFormHtmlParser htmlParser, DefaultHttpClientFactory httpClientFactory, IFlurlClientFactory _factory) - : base(htmlParser, httpClientFactory, _factory) + public ConsentForm(IAuthorizationFormHtmlParser htmlParser) + : base(htmlParser) { } diff --git a/VkNet/Infrastructure/Authorization/ImplicitFlow/ImplicitFlowCaptchaLoginForm.cs b/VkNet/Infrastructure/Authorization/ImplicitFlow/ImplicitFlowCaptchaLoginForm.cs index b7b3fb187..4f3bca892 100644 --- a/VkNet/Infrastructure/Authorization/ImplicitFlow/ImplicitFlowCaptchaLoginForm.cs +++ b/VkNet/Infrastructure/Authorization/ImplicitFlow/ImplicitFlowCaptchaLoginForm.cs @@ -1,4 +1,3 @@ -using Flurl.Http.Configuration; using JetBrains.Annotations; using VkNet.Enums; using VkNet.Exception; @@ -16,9 +15,9 @@ public sealed class ImplicitFlowCaptchaLoginForm : AbstractAuthorizationForm private readonly ICaptchaSolver _captchaSolver; /// - public ImplicitFlowCaptchaLoginForm(IAuthorizationFormHtmlParser htmlParser, DefaultHttpClientFactory httpClientFactory, - IApiAuthParams authorizationParameters, ICaptchaSolver captchaSolver, IFlurlClientFactory _factory) - : base(htmlParser, httpClientFactory, _factory) + public ImplicitFlowCaptchaLoginForm(IAuthorizationFormHtmlParser htmlParser, + IApiAuthParams authorizationParameters, ICaptchaSolver captchaSolver) + : base(htmlParser) { _authorizationParameters = authorizationParameters; _captchaSolver = captchaSolver; @@ -45,7 +44,8 @@ protected override void FillFormFields(VkHtmlFormResult form) form.Fields[AuthorizationFormFields.Password] = _authorizationParameters.Password; } - var captchaKey = _captchaSolver.Solve($"https://api.vk.com//captcha.php?sid={form.Fields[AuthorizationFormFields.CaptchaSid]}&s=1"); + var captchaKey = + _captchaSolver.Solve($"https://api.vk.com//captcha.php?sid={form.Fields[AuthorizationFormFields.CaptchaSid]}&s=1"); if (form.Fields.ContainsKey(AuthorizationFormFields.CaptchaKey)) { diff --git a/VkNet/Infrastructure/Authorization/ImplicitFlow/ImplicitFlowLoginForm.cs b/VkNet/Infrastructure/Authorization/ImplicitFlow/ImplicitFlowLoginForm.cs index 83219f20b..1bb8f51e1 100644 --- a/VkNet/Infrastructure/Authorization/ImplicitFlow/ImplicitFlowLoginForm.cs +++ b/VkNet/Infrastructure/Authorization/ImplicitFlow/ImplicitFlowLoginForm.cs @@ -1,4 +1,3 @@ -using Flurl.Http.Configuration; using JetBrains.Annotations; using VkNet.Enums; using VkNet.Model; @@ -12,9 +11,9 @@ public sealed class ImplicitFlowLoginForm : AbstractAuthorizationForm private readonly IApiAuthParams _authorizationParameters; /// - public ImplicitFlowLoginForm(DefaultHttpClientFactory httpClientFactory, IAuthorizationFormHtmlParser htmlParser, - IApiAuthParams authorizationParameters, IFlurlClientFactory factory) - : base(htmlParser, httpClientFactory, factory) + public ImplicitFlowLoginForm(IAuthorizationFormHtmlParser htmlParser, + IApiAuthParams authorizationParameters) + : base(htmlParser) { _authorizationParameters = authorizationParameters; } diff --git a/VkNet/Infrastructure/Authorization/ImplicitFlow/NoRedirectHttpClientFactory.cs b/VkNet/Infrastructure/Authorization/ImplicitFlow/NoRedirectHttpClientFactory.cs deleted file mode 100644 index e4bf932fc..000000000 --- a/VkNet/Infrastructure/Authorization/ImplicitFlow/NoRedirectHttpClientFactory.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System.Net; -using System.Net.Http; -using Flurl.Http.Configuration; - -namespace VkNet.Infrastructure.Authorization.ImplicitFlow -{ - public class NoRedirectHttpClientFactory : DefaultHttpClientFactory - { - private readonly IWebProxy _proxy; - - private readonly CookieContainer _cookieContainer; - - public NoRedirectHttpClientFactory(CookieContainer cookieContainer, IWebProxy proxy) - { - _cookieContainer = cookieContainer; - _proxy = proxy; - } - - public override HttpMessageHandler CreateMessageHandler() - { - return new HttpClientHandler - { - Proxy = _proxy, - UseProxy = true, - AllowAutoRedirect = false, - UseCookies = true, - CookieContainer = _cookieContainer - }; - } - } -} \ No newline at end of file diff --git a/VkNet/Infrastructure/Authorization/ImplicitFlow/ProxyHttpClientFactory.cs b/VkNet/Infrastructure/Authorization/ImplicitFlow/ProxyHttpClientFactory.cs deleted file mode 100644 index 761f0f0e0..000000000 --- a/VkNet/Infrastructure/Authorization/ImplicitFlow/ProxyHttpClientFactory.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System.Net; -using System.Net.Http; -using Flurl.Http.Configuration; -using JetBrains.Annotations; - -namespace VkNet.Infrastructure.Authorization.ImplicitFlow -{ - /// - [UsedImplicitly] - public sealed class ProxyHttpClientFactory : DefaultHttpClientFactory - { - private readonly IWebProxy _proxy; - - private readonly CookieContainer _cookieContainer; - - /// - public ProxyHttpClientFactory(IWebProxy proxy, CookieContainer cookieContainer) - { - _proxy = proxy; - _cookieContainer = cookieContainer; - } - - /// - public override HttpMessageHandler CreateMessageHandler() - { - if (_proxy == null) - { - return new HttpClientHandler(); - } - - return new HttpClientHandler - { - Proxy = _proxy, - UseProxy = true, - AllowAutoRedirect = true, - UseCookies = true, - CookieContainer = _cookieContainer - }; - } - } -} \ No newline at end of file diff --git a/VkNet/Infrastructure/Authorization/ImplicitFlow/TwoFactorForm.cs b/VkNet/Infrastructure/Authorization/ImplicitFlow/TwoFactorForm.cs index 971d1a7f1..05e593de4 100644 --- a/VkNet/Infrastructure/Authorization/ImplicitFlow/TwoFactorForm.cs +++ b/VkNet/Infrastructure/Authorization/ImplicitFlow/TwoFactorForm.cs @@ -1,4 +1,3 @@ -using Flurl.Http.Configuration; using JetBrains.Annotations; using VkNet.Enums; using VkNet.Exception; @@ -13,9 +12,8 @@ public sealed class TwoFactorForm : AbstractAuthorizationForm private readonly IApiAuthParams _authorizationParameters; /// - public TwoFactorForm(IApiAuthParams authorizationParameters, DefaultHttpClientFactory httpClientFactory, - IAuthorizationFormHtmlParser htmlParser, IFlurlClientFactory _factory) - : base(htmlParser, httpClientFactory, _factory) + public TwoFactorForm(IApiAuthParams authorizationParameters, IAuthorizationFormHtmlParser htmlParser) + : base(htmlParser) { _authorizationParameters = authorizationParameters; } diff --git a/VkNet/Utils/RestClient.cs b/VkNet/Utils/RestClient.cs index 140aa3dfb..6e71706b1 100644 --- a/VkNet/Utils/RestClient.cs +++ b/VkNet/Utils/RestClient.cs @@ -4,7 +4,6 @@ using System.Net; using System.Net.Http; using System.Threading.Tasks; -using Flurl; using Flurl.Http; using JetBrains.Annotations; using Microsoft.Extensions.Logging; @@ -46,7 +45,7 @@ public Task> GetAsync(Uri uri, IEnumerable uri.ToString().SetQueryParams(parameters).GetAsync()); + return CallAsync(() => uri.ToString().AllowAnyHttpStatus().SetQueryParams(parameters).GetAsync()); } /// @@ -60,7 +59,7 @@ public Task> PostAsync(Uri uri, IEnumerable uri.ToString().PostAsync(content)); + return CallAsync(() => uri.ToString().AllowAnyHttpStatus().PostAsync(content)); } /// diff --git a/VkNet/Utils/TypeHelper.cs b/VkNet/Utils/TypeHelper.cs index 46c96f2bd..2aaf3fb77 100644 --- a/VkNet/Utils/TypeHelper.cs +++ b/VkNet/Utils/TypeHelper.cs @@ -1,7 +1,6 @@ using System; using System.Net; using System.Threading.Tasks; -using Flurl.Http.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Logging; @@ -114,12 +113,6 @@ private static void RegisterAuthorization(this IServiceCollection services) services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - services.TryAddSingleton(); - services.TryAddSingleton(); - services.TryAddSingleton(); - services.TryAddSingleton(); - services.TryAddSingleton(); - services.TryAddSingleton(); } } } \ No newline at end of file diff --git a/VkNet/VkApi.cs b/VkNet/VkApi.cs index c27b8b12c..8cfb103dc 100644 --- a/VkNet/VkApi.cs +++ b/VkNet/VkApi.cs @@ -7,7 +7,6 @@ using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; -using Flurl.Http; using JetBrains.Annotations; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -832,11 +831,6 @@ private void BaseAuthorize(IApiAuthParams authParams) private void Initialization(IServiceProvider serviceProvider) { - FlurlHttp.Configure(settings => - { - settings.HttpClientFactory = serviceProvider.GetService(); - }); - _logger = serviceProvider.GetService>(); _captchaHandler = serviceProvider.GetRequiredService(); _language = serviceProvider.GetRequiredService();