<commit_before>using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using VkNet.Abstractions;
using VkNet.Abstractions.Authorization;
using VkNet.Abstractions.Core;
using VkNet.Abstractions.Utils;
using VkNet.Exception;
using VkNet.Infrastructure;
using VkNet.Utils.AntiCaptcha;

namespace VkNet.Utils
{
	/// <summary>
	/// Методы расширения для типов
	/// </summary>
	public static class TypeHelper
	{
		/// <summary>
		/// DI регистрация зависимостей по умолчанию
		/// </summary>
		/// <param name="container"> DI контейнер </param>
		public static void RegisterDefaultDependencies(this IServiceCollection container)
		{
			if (container.All(x => x.ServiceType != typeof(IBrowser)))
			{
				container.TryAddSingleton<IBrowser, Browser>();
			}

			if (container.All(x => x.ServiceType != typeof(IAuthorizationFlow)))
			{
				container.TryAddSingleton<IAuthorizationFlow, Browser>();
			}

			if (container.All(x => x.ServiceType != typeof(INeedValidationHandler)))
			{
				container.TryAddSingleton<INeedValidationHandler, Browser>();
			}

			if (container.All(x => x.ServiceType != typeof(ILogger<>)))
			{
				container.TryAddSingleton(typeof(ILogger<>), typeof(NullLogger<>));
			}

			if (container.All(x => x.ServiceType != typeof(IRestClient)))
			{
				container.TryAddSingleton<IRestClient, RestClient>();
			}

			if (container.All(x => x.ServiceType != typeof(IWebProxy)))
			{
				container.TryAddSingleton<IWebProxy>(t => null);
			}

			if (container.All(x => x.ServiceType != typeof(IVkApiVersionManager)))
			{
				container.TryAddSingleton<IVkApiVersionManager, VkApiVersionManager>();
			}

			if (container.All(x => x.ServiceType != typeof(ICaptchaHandler)))
			{
				container.TryAddSingleton<ICaptchaHandler, CaptchaHandler>();
			}

			if (container.All(x => x.ServiceType != typeof(ILanguageService)))
			{
				container.TryAddSingleton<ILanguageService, LanguageService>();
			}

			if (container.All(x => x.ServiceType != typeof(ICaptchaSolver)))
			{
				container.TryAddSingleton<ICaptchaSolver>(sp => null);
			}
		}

		/// <summary>
		/// Попытаться асинхронно выполнить метод.
		/// </summary>
		/// <param name="func"> Синхронный метод. </param>
		/// <typeparam name="T"> Тип ответа </typeparam>
		/// <returns> Результат выполнения функции. </returns>
		public static Task<T> TryInvokeMethodAsync<T>(Func<T> func)
		{
			var tcs = new TaskCompletionSource<T>();

			Task.Factory.StartNew(() =>
				{
					try
					{
						var result = func.Invoke();
						tcs.SetResult(result);
					}
					catch (VkApiException ex)
					{
						tcs.SetException(ex);
					}
				})
				.ConfigureAwait(false);

			return tcs.Task;
		}

		/// <summary>
		/// Попытаться асинхронно выполнить метод.<commit_msg>Зарегистрировать HttpClient как Синглтон #657<commit_after>using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using VkNet.Abstractions;
using VkNet.Abstractions.Authorization;
using VkNet.Abstractions.Core;
using VkNet.Abstractions.Utils;
using VkNet.Exception;
using VkNet.Infrastructure;
using VkNet.Utils.AntiCaptcha;

namespace VkNet.Utils
{
	/// <summary>
	/// Методы расширения для типов
	/// </summary>
	public static class TypeHelper
	{
		/// <summary>
		/// DI регистрация зависимостей по умолчанию
		/// </summary>
		/// <param name="container"> DI контейнер </param>
		public static void RegisterDefaultDependencies(this IServiceCollection container)
		{
			if (container.All(x => x.ServiceType != typeof(IBrowser)))
			{
				container.TryAddSingleton<IBrowser, Browser>();
			}

			if (container.All(x => x.ServiceType != typeof(IAuthorizationFlow)))
			{
				container.TryAddSingleton<IAuthorizationFlow, Browser>();
			}

			if (container.All(x => x.ServiceType != typeof(INeedValidationHandler)))
			{
				container.TryAddSingleton<INeedValidationHandler, Browser>();
			}

			if (container.All(x => x.ServiceType != typeof(ILogger<>)))
			{
				container.TryAddSingleton(typeof(ILogger<>), typeof(NullLogger<>));
			}

			if (container.All(x => x.ServiceType != typeof(IRestClient)))
			{
				container.TryAddSingleton<IRestClient, RestClient>();
			}

			if (container.All(x => x.ServiceType != typeof(IWebProxy)))
			{
				container.TryAddSingleton<IWebProxy>(t => null);
			}

			if (container.All(x => x.ServiceType != typeof(IVkApiVersionManager)))
			{
				container.TryAddSingleton<IVkApiVersionManager, VkApiVersionManager>();
			}

			if (container.All(x => x.ServiceType != typeof(ICaptchaHandler)))
			{
				container.TryAddSingleton<ICaptchaHandler, CaptchaHandler>();
			}

			if (container.All(x => x.ServiceType != typeof(ILanguageService)))
			{
				container.TryAddSingleton<ILanguageService, LanguageService>();
			}

			if (container.All(x => x.ServiceType != typeof(ICaptchaSolver)))
			{
				container.TryAddSingleton<ICaptchaSolver>(sp => null);
			}

			if (container.All(x => x.ServiceType != typeof(HttpClient)))
			{
				container.TryAddSingleton<HttpClient>();
			}
		}

		/// <summary>
		/// Попытаться асинхронно выполнить метод.
		/// </summary>
		/// <param name="func"> Синхронный метод. </param>
		/// <typeparam name="T"> Тип ответа </typeparam>
		/// <returns> Результат выполнения функции. </returns>
		public static Task<T> TryInvokeMethodAsync<T>(Func<T> func)
		{
			var tcs = new TaskCompletionSource<T>();

			Task.Factory.StartNew(() =>
				{
					try
					{
						var result = func.Invoke();
						tcs.SetResult(result);
					}
					catch (VkApiException ex)
					{
						tcs.SetException(ex);
					}
				})
				.ConfigureAwait(false);

			return tcs.Task;
		}

		/// <summary>
		/// Попытаться асинхронно выполнить метод.
{'fraction_non_alphanumeric': 0.1408589764686523, 'fraction_numerical': 0.0004936646371564917, 'mean_word_length': 3.8276409849086575, 'pattern_counts': {'":': 0, '<': 66, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6056952', 'n_tokens_mistral': 2348, 'n_tokens_neox': 2048, 'n_words': 346}