Skip to content

Commit

Permalink
Конфигурация Flurl (#836)
Browse files Browse the repository at this point in the history
* Разрешить Flurl ответы не только с 2ХХ статус кодом

* Убрать глобальную настройку Flurl
  • Loading branch information
itsokto authored and inyutin-maxim committed Apr 25, 2019
1 parent 18df8c0 commit a464299
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Threading.Tasks;
using Flurl;
using Flurl.Http;
using Flurl.Http.Configuration;
using VkNet.Enums;
using VkNet.Exception;

Expand All @@ -14,17 +13,10 @@ public abstract class AbstractAuthorizationForm : IAuthorizationForm
{
private readonly IAuthorizationFormHtmlParser _htmlParser;

private readonly DefaultHttpClientFactory _httpClientFactory;

private readonly IFlurlClientFactory _clientFactory;

/// <inheritdoc />
protected AbstractAuthorizationForm(IAuthorizationFormHtmlParser htmlParser, DefaultHttpClientFactory httpClientFactory,
IFlurlClientFactory clientFactory)
protected AbstractAuthorizationForm(IAuthorizationFormHtmlParser htmlParser)
{
_htmlParser = htmlParser;
_httpClientFactory = httpClientFactory;
_clientFactory = clientFactory;
}

/// <inheritdoc />
Expand All @@ -37,7 +29,7 @@ public async Task<AuthorizationFormResult> 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)))
Expand Down Expand Up @@ -67,7 +59,7 @@ public async Task<AuthorizationFormResult> ExecuteAsync(Url url)
/// <summary>
/// Заполнение полей формы
/// </summary>
/// <param name="form">Форма</param>
/// <param name="form"> Форма </param>
protected abstract void FillFormFields(VkHtmlFormResult form);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Flurl.Http.Configuration;
using JetBrains.Annotations;
using VkNet.Enums;

Expand All @@ -9,8 +8,8 @@ namespace VkNet.Infrastructure.Authorization.ImplicitFlow
public sealed class ConsentForm : AbstractAuthorizationForm
{
/// <inheritdoc />
public ConsentForm(IAuthorizationFormHtmlParser htmlParser, DefaultHttpClientFactory httpClientFactory, IFlurlClientFactory _factory)
: base(htmlParser, httpClientFactory, _factory)
public ConsentForm(IAuthorizationFormHtmlParser htmlParser)
: base(htmlParser)
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Flurl.Http.Configuration;
using JetBrains.Annotations;
using VkNet.Enums;
using VkNet.Exception;
Expand All @@ -16,9 +15,9 @@ public sealed class ImplicitFlowCaptchaLoginForm : AbstractAuthorizationForm
private readonly ICaptchaSolver _captchaSolver;

/// <inheritdoc />
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;
Expand All @@ -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))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Flurl.Http.Configuration;
using JetBrains.Annotations;
using VkNet.Enums;
using VkNet.Model;
Expand All @@ -12,9 +11,9 @@ public sealed class ImplicitFlowLoginForm : AbstractAuthorizationForm
private readonly IApiAuthParams _authorizationParameters;

/// <inheritdoc />
public ImplicitFlowLoginForm(DefaultHttpClientFactory httpClientFactory, IAuthorizationFormHtmlParser htmlParser,
IApiAuthParams authorizationParameters, IFlurlClientFactory factory)
: base(htmlParser, httpClientFactory, factory)
public ImplicitFlowLoginForm(IAuthorizationFormHtmlParser htmlParser,
IApiAuthParams authorizationParameters)
: base(htmlParser)
{
_authorizationParameters = authorizationParameters;
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Flurl.Http.Configuration;
using JetBrains.Annotations;
using VkNet.Enums;
using VkNet.Exception;
Expand All @@ -13,9 +12,8 @@ public sealed class TwoFactorForm : AbstractAuthorizationForm
private readonly IApiAuthParams _authorizationParameters;

/// <inheritdoc />
public TwoFactorForm(IApiAuthParams authorizationParameters, DefaultHttpClientFactory httpClientFactory,
IAuthorizationFormHtmlParser htmlParser, IFlurlClientFactory _factory)
: base(htmlParser, httpClientFactory, _factory)
public TwoFactorForm(IApiAuthParams authorizationParameters, IAuthorizationFormHtmlParser htmlParser)
: base(htmlParser)
{
_authorizationParameters = authorizationParameters;
}
Expand Down
5 changes: 2 additions & 3 deletions VkNet/Utils/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,7 +45,7 @@ public Task<HttpResponse<string>> GetAsync(Uri uri, IEnumerable<KeyValuePair<str
_logger.LogDebug($"GET request: {uriBuilder.Uri}");
}

return CallAsync(() => uri.ToString().SetQueryParams(parameters).GetAsync());
return CallAsync(() => uri.ToString().AllowAnyHttpStatus().SetQueryParams(parameters).GetAsync());
}

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

var content = new FormUrlEncodedContent(parameters);

return CallAsync(() => uri.ToString().PostAsync(content));
return CallAsync(() => uri.ToString().AllowAnyHttpStatus().PostAsync(content));
}

/// <inheritdoc />
Expand Down
7 changes: 0 additions & 7 deletions VkNet/Utils/TypeHelper.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -114,12 +113,6 @@ private static void RegisterAuthorization(this IServiceCollection services)
services.AddSingleton<IAuthorizationForm, ImplicitFlowLoginForm>();
services.AddSingleton<IAuthorizationForm, TwoFactorForm>();
services.AddSingleton<IAuthorizationForm, ConsentForm>();
services.TryAddSingleton<IFlurlClientFactory, PerBaseUrlFlurlClientFactory>();
services.TryAddSingleton<DefaultHttpClientFactory, ProxyHttpClientFactory>();
services.TryAddSingleton<DefaultHttpClientFactory, NoRedirectHttpClientFactory>();
services.TryAddSingleton<ProxyHttpClientFactory>();
services.TryAddSingleton<NoRedirectHttpClientFactory>();
services.TryAddSingleton<CookieContainer>();
}
}
}
6 changes: 0 additions & 6 deletions VkNet/VkApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -832,11 +831,6 @@ private void BaseAuthorize(IApiAuthParams authParams)

private void Initialization(IServiceProvider serviceProvider)
{
FlurlHttp.Configure(settings =>
{
settings.HttpClientFactory = serviceProvider.GetService<ProxyHttpClientFactory>();
});

_logger = serviceProvider.GetService<ILogger<VkApi>>();
_captchaHandler = serviceProvider.GetRequiredService<ICaptchaHandler>();
_language = serviceProvider.GetRequiredService<ILanguageService>();
Expand Down

0 comments on commit a464299

Please sign in to comment.