<commit_before>﻿using System;
using System.Net;
using System.Net.Http;
using CommandLine;
using FilterLists.Agent.AppSettings;
using FilterLists.Agent.Core.Interfaces.Repositories;
using FilterLists.Agent.Infrastructure.Repositories;
using LibGit2Sharp;
using MediatR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Octokit;
using Polly;
using RestSharp;
using Credentials = Octokit.Credentials;
using Repository = LibGit2Sharp.Repository;

namespace FilterLists.Agent.Extensions
{
    public static class ServiceCollectionExtensions
    {
        public static void RegisterAgentServices(this IServiceCollection services)
        {
            services.AddConfiguration();
            services.AddLoggingCustom();
            services.AddLocalization();
            services.AddTransient<Parser>();
            services.AddMediatR(AppDomain.CurrentDomain.GetAssemblies());
            services.AddApiClient();
            services.AddGitHubClient();
            services.AddUrlRepository();
            services.AddListRepository();
            services.AddArchiveRepository();
            services.AddTransient<IListInfoRepository, ListInfoRepository>();
            services.AddTransient<IGitHubIssuesRepository, GitHubIssuesRepository>();
        }

        private static void AddConfiguration(this IServiceCollection services)
        {
            var config = new ConfigurationBuilder()
                .AddEnvironmentVariables()
                .AddJsonFile("appsettings.json", true, true)
#if DEBUG
                .AddJsonFile("appsettings.Development.json", true, true)
#endif
                .Build();
            services.ConfigureCustom<ApplicationInsightsSettings>(config);
            services.ConfigureCustom<ArchiveSettings>(config);
            services.ConfigureCustom<ConnectionStringSettings>(config);
            services.ConfigureCustom<FilterListsApiSettings>(config);
            services.ConfigureCustom<GitHubSettings>(config);
        }

        private static void ConfigureCustom<TSettings>(this IServiceCollection services, IConfiguration configuration)
            where TSettings : class
        {
            services.Configure<TSettings>(
                configuration.GetSection(typeof(TSettings).Name.Replace("Settings", "", StringComparison.Ordinal)));
        }

        private static void AddLoggingCustom(this IServiceCollection services)
        {
            services.AddLogging(b =>
            {
                b.AddConsole();
                var applicationInsightsSettings = b.Services.BuildServiceProvider()
                    .GetService<IOptions<ApplicationInsightsSettings>>().Value;
                b.AddApplicationInsights(applicationInsightsSettings.InstrumentationKey);
            });
        }

        private static void AddApiClient(this IServiceCollection services)
        {
            services.AddSingleton<IRestClient>(b =>
            {
                var filterListsApiSettings = b.GetService<IOptions<FilterListsApiSettings>>().Value;
                return new RestClient(filterListsApiSettings.BaseUrl) {UserAgent = "FilterLists.Agent"};
            });
        }

        private static void AddGitHubClient(this IServiceCollection services)
        {
            services.AddSingleton<IGitHubClient, GitHubClient>(s =>
            {
                var gitHubSettings = s.GetService<IOptions<GitHubSettings>>().Value;
                return new GitHubClient(new ProductHeaderValue(gitHubSettings.ProductHeaderValue))
                {
                    Credentials = new Credentials(gitHubSettings.PersonalAccessToken)
                };
            });
        }

        private static void AddUrlRepository(this IServiceCollection services)
        {
            services.AddHttpClient<IUrlRepository, UrlRepository>()
                .ConfigureHttpMessageHandlerBuilder(b =>
                {
                    b.PrimaryHandler = new HttpClientHandler {AllowAutoRedirect = false};
                    b.Build();
                })
                .AddTransientHttpErrorPolicy(b =>
                    b.OrResult(r => r.StatusCode == HttpStatusCode.TooManyRequests)
                        .WaitAndRetryAsync(5, i => i * TimeSpan.FromSeconds(3)));
        }

        private static void AddListRepository(this IServiceCollection services)
        {
            services.AddHttpClient<IListRepository, ListRepository>()
                .ConfigureHttpMessageHandlerBuilder(b =>
                {
                    b.PrimaryHandler = new HttpClientHandler {AllowAutoRedirect = false};
                    b.Build();
                })
                .AddTransientHttpErrorPolicy(b =>
                    b.OrResult(r => r.StatusCode == HttpStatusCode.TooManyRequests)
                        .WaitAndRetryAsync(5, i => i * TimeSpan.FromSeconds(3)));
        }

        private static void AddArchiveRepository(this IServiceCollection services)
        {
            services.AddTransient<IRepository, Repository>(s =>
            {
                var archiveSettings = s.GetService<IOptions<ArchiveSettings>>().Value;
                if (!Repository.IsValid(archiveSettings.RepositoryDirectory))
                    Repository.Init(archiveSettings.RepositoryDirectory);
                return new Repository(archiveSettings.RepositoryDirectory);
            });
        }
    }
}<commit_msg>minor DI registration cleanup<commit_after>﻿using System;
using System.Net;
using System.Net.Http;
using CommandLine;
using FilterLists.Agent.AppSettings;
using FilterLists.Agent.Core.Interfaces.Repositories;
using FilterLists.Agent.Infrastructure.Repositories;
using LibGit2Sharp;
using MediatR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Octokit;
using Polly;
using RestSharp;
using Credentials = Octokit.Credentials;
using Repository = LibGit2Sharp.Repository;

namespace FilterLists.Agent.Extensions
{
    public static class ServiceCollectionExtensions
    {
        public static void RegisterAgentServices(this IServiceCollection services)
        {
            services.AddConfiguration();
            services.AddLoggingCustom();
            services.AddLocalization();
            services.AddTransient<Parser>();
            services.AddMediatR(AppDomain.CurrentDomain.GetAssemblies());
            services.AddApiClient();
            services.AddGitHubClient();
            services.AddUrlRepository();
            services.AddListRepository();
            services.AddArchiveRepository();
            services.AddTransient<IListInfoRepository, ListInfoRepository>();
            services.AddTransient<IGitHubIssuesRepository, GitHubIssuesRepository>();
        }

        private static void AddConfiguration(this IServiceCollection services)
        {
            var config = new ConfigurationBuilder()
                .AddEnvironmentVariables()
                .AddJsonFile("appsettings.json", true, true)
#if DEBUG
                .AddJsonFile("appsettings.Development.json", true, true)
#endif
                .Build();
            services.ConfigureCustom<ApplicationInsightsSettings>(config);
            services.ConfigureCustom<ArchiveSettings>(config);
            services.ConfigureCustom<ConnectionStringSettings>(config);
            services.ConfigureCustom<FilterListsApiSettings>(config);
            services.ConfigureCustom<GitHubSettings>(config);
        }

        private static void ConfigureCustom<TSettings>(this IServiceCollection services, IConfiguration configuration)
            where TSettings : class
        {
            services.Configure<TSettings>(
                configuration.GetSection(typeof(TSettings).Name.Replace("Settings", "", StringComparison.Ordinal)));
        }

        private static void AddLoggingCustom(this IServiceCollection services)
        {
            services.AddLogging(b =>
            {
                b.AddConsole();
                var applicationInsightsSettings = b.Services.BuildServiceProvider()
                    .GetService<IOptions<ApplicationInsightsSettings>>().Value;
                b.AddApplicationInsights(applicationInsightsSettings.InstrumentationKey);
            });
        }

        private static void AddApiClient(this IServiceCollection services)
        {
            services.AddSingleton<IRestClient, RestClient>(b =>
            {
                var filterListsApiSettings = b.GetService<IOptions<FilterListsApiSettings>>().Value;
                return new RestClient(filterListsApiSettings.BaseUrl) {UserAgent = "FilterLists.Agent"};
            });
        }

        private static void AddGitHubClient(this IServiceCollection services)
        {
            services.AddSingleton<IGitHubClient, GitHubClient>(s =>
            {
                var gitHubSettings = s.GetService<IOptions<GitHubSettings>>().Value;
                return new GitHubClient(new ProductHeaderValue(gitHubSettings.ProductHeaderValue))
                {
                    Credentials = new Credentials(gitHubSettings.PersonalAccessToken)
                };
            });
        }

        private static void AddUrlRepository(this IServiceCollection services)
        {
            services.AddHttpClient<IUrlRepository, UrlRepository>()
                .ConfigureHttpMessageHandlerBuilder(b =>
                {
                    b.PrimaryHandler = new HttpClientHandler {AllowAutoRedirect = false};
                    b.Build();
                })
                .AddTransientHttpErrorPolicy(b =>
                    b.OrResult(r => r.StatusCode == HttpStatusCode.TooManyRequests)
                        .WaitAndRetryAsync(5, i => i * TimeSpan.FromSeconds(3)));
        }

        private static void AddListRepository(this IServiceCollection services)
        {
            services.AddHttpClient<IListRepository, ListRepository>().AddTransientHttpErrorPolicy(b =>
                b.OrResult(r => r.StatusCode == HttpStatusCode.TooManyRequests)
                    .WaitAndRetryAsync(5, i => i * TimeSpan.FromSeconds(3)));
        }

        private static void AddArchiveRepository(this IServiceCollection services)
        {
            services.AddTransient<IRepository, Repository>(s =>
            {
                var archiveSettings = s.GetService<IOptions<ArchiveSettings>>().Value;
                if (!Repository.IsValid(archiveSettings.RepositoryDirectory))
                    Repository.Init(archiveSettings.RepositoryDirectory);
                return new Repository(archiveSettings.RepositoryDirectory);
            });
        }
    }
}
{'fraction_non_alphanumeric': 0.08382789317507418, 'fraction_numerical': 0.001112759643916914, 'mean_word_length': 2.3883129123468425, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '137518', 'n_tokens_mistral': 2570, 'n_tokens_neox': 2435, 'n_words': 422}