diff --git a/src/FilterLists.Agent/ListArchiver/CaptureLists.cs b/src/FilterLists.Agent/Application/Archiver/CaptureLists.cs similarity index 68% rename from src/FilterLists.Agent/ListArchiver/CaptureLists.cs rename to src/FilterLists.Agent/Application/Archiver/CaptureLists.cs index 5b4ee348b..c5a9e3c70 100644 --- a/src/FilterLists.Agent/ListArchiver/CaptureLists.cs +++ b/src/FilterLists.Agent/Application/Archiver/CaptureLists.cs @@ -1,9 +1,9 @@ using System.Threading; using System.Threading.Tasks; -using FilterLists.Agent.Infrastructure; +using FilterLists.Agent.Core.Interfaces; using MediatR; -namespace FilterLists.Agent.ListArchiver +namespace FilterLists.Agent.Application.Archiver { public static class CaptureLists { @@ -13,18 +13,18 @@ public class Command : IRequest public class Handler : AsyncRequestHandler { - private readonly IFilterListsApiClient _apiClient; private readonly IMediator _mediator; + private readonly IListInfoRepository _repo; - public Handler(IFilterListsApiClient apiClient, IMediator mediator) + public Handler(IMediator mediator, IListInfoRepository listInfoRepository) { - _apiClient = apiClient; + _repo = listInfoRepository; _mediator = mediator; } protected override async Task Handle(Command request, CancellationToken cancellationToken) { - var lists = await _apiClient.GetListInfo(); + var lists = await _repo.GetAll(); await _mediator.Send(new DownloadLists.Command(lists), cancellationToken); await _mediator.Send(new CommitDownloadedLists.Command(), cancellationToken); } diff --git a/src/FilterLists.Agent/ListArchiver/CommitDownloadedLists.cs b/src/FilterLists.Agent/Application/Archiver/CommitDownloadedLists.cs similarity index 95% rename from src/FilterLists.Agent/ListArchiver/CommitDownloadedLists.cs rename to src/FilterLists.Agent/Application/Archiver/CommitDownloadedLists.cs index e35ede2cb..9eaa30740 100644 --- a/src/FilterLists.Agent/ListArchiver/CommitDownloadedLists.cs +++ b/src/FilterLists.Agent/Application/Archiver/CommitDownloadedLists.cs @@ -2,7 +2,7 @@ using LibGit2Sharp; using MediatR; -namespace FilterLists.Agent.ListArchiver +namespace FilterLists.Agent.Application.Archiver { public static class CommitDownloadedLists { diff --git a/src/FilterLists.Agent/ListArchiver/DownloadList.cs b/src/FilterLists.Agent/Application/Archiver/DownloadList.cs similarity index 96% rename from src/FilterLists.Agent/ListArchiver/DownloadList.cs rename to src/FilterLists.Agent/Application/Archiver/DownloadList.cs index 4b49e81e6..85254d3a7 100644 --- a/src/FilterLists.Agent/ListArchiver/DownloadList.cs +++ b/src/FilterLists.Agent/Application/Archiver/DownloadList.cs @@ -4,13 +4,13 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; -using FilterLists.Agent.Entities; -using FilterLists.Agent.Infrastructure; +using FilterLists.Agent.Core.Entities; +using FilterLists.Agent.Infrastructure.Clients; using MediatR; //TODO: upsert into MariaDB Rules table https://stackoverflow.com/questions/15271202/mysql-load-data-infile-with-on-duplicate-key-update -namespace FilterLists.Agent.ListArchiver +namespace FilterLists.Agent.Application.Archiver { public static class DownloadList { diff --git a/src/FilterLists.Agent/ListArchiver/DownloadLists.cs b/src/FilterLists.Agent/Application/Archiver/DownloadLists.cs similarity index 95% rename from src/FilterLists.Agent/ListArchiver/DownloadLists.cs rename to src/FilterLists.Agent/Application/Archiver/DownloadLists.cs index de474b336..6a2f79c20 100644 --- a/src/FilterLists.Agent/ListArchiver/DownloadLists.cs +++ b/src/FilterLists.Agent/Application/Archiver/DownloadLists.cs @@ -3,10 +3,10 @@ using System.Threading; using System.Threading.Tasks; using System.Threading.Tasks.Dataflow; -using FilterLists.Agent.Entities; +using FilterLists.Agent.Core.Entities; using MediatR; -namespace FilterLists.Agent.ListArchiver +namespace FilterLists.Agent.Application.Archiver { public static class DownloadLists { diff --git a/src/FilterLists.Agent/ListArchiver/FileType.cs b/src/FilterLists.Agent/Application/Archiver/FileType.cs similarity index 69% rename from src/FilterLists.Agent/ListArchiver/FileType.cs rename to src/FilterLists.Agent/Application/Archiver/FileType.cs index 8872596d9..e6eda328a 100644 --- a/src/FilterLists.Agent/ListArchiver/FileType.cs +++ b/src/FilterLists.Agent/Application/Archiver/FileType.cs @@ -1,4 +1,4 @@ -namespace FilterLists.Agent.ListArchiver +namespace FilterLists.Agent.Application.Archiver { public enum FileType { diff --git a/src/FilterLists.Agent/ListArchiver/StreamExtensions.cs b/src/FilterLists.Agent/Application/Archiver/StreamExtensions.cs similarity index 97% rename from src/FilterLists.Agent/ListArchiver/StreamExtensions.cs rename to src/FilterLists.Agent/Application/Archiver/StreamExtensions.cs index 68ca53856..3be344680 100644 --- a/src/FilterLists.Agent/ListArchiver/StreamExtensions.cs +++ b/src/FilterLists.Agent/Application/Archiver/StreamExtensions.cs @@ -5,7 +5,7 @@ using SharpCompress.Archives.SevenZip; using SharpCompress.Readers; -namespace FilterLists.Agent.ListArchiver +namespace FilterLists.Agent.Application.Archiver { public static class StreamExtensions { diff --git a/src/FilterLists.Agent/Entities/ListInfo.cs b/src/FilterLists.Agent/Core/Entities/ListInfo.cs similarity index 62% rename from src/FilterLists.Agent/Entities/ListInfo.cs rename to src/FilterLists.Agent/Core/Entities/ListInfo.cs index f0c093d62..44dbea2aa 100644 --- a/src/FilterLists.Agent/Entities/ListInfo.cs +++ b/src/FilterLists.Agent/Core/Entities/ListInfo.cs @@ -1,7 +1,9 @@ using System; +using JetBrains.Annotations; -namespace FilterLists.Agent.Entities +namespace FilterLists.Agent.Core.Entities { + [UsedImplicitly] public class ListInfo { public int Id { get; private set; } diff --git a/src/FilterLists.Agent/Core/Interfaces/IListInfoRepository.cs b/src/FilterLists.Agent/Core/Interfaces/IListInfoRepository.cs new file mode 100644 index 000000000..91bbcb18e --- /dev/null +++ b/src/FilterLists.Agent/Core/Interfaces/IListInfoRepository.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using FilterLists.Agent.Core.Entities; + +namespace FilterLists.Agent.Core.Interfaces +{ + public interface IListInfoRepository + { + Task> GetAll(); + } +} \ No newline at end of file diff --git a/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs b/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs new file mode 100644 index 000000000..a24dbaa9f --- /dev/null +++ b/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs @@ -0,0 +1,31 @@ +using FilterLists.Agent.Infrastructure.Clients; +using MediatR; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace FilterLists.Agent.Extensions +{ + public static class ServiceCollectionExtensions + { + public static void RegisterAgentServices(this IServiceCollection serviceCollection) + { + var configuration = GetConfiguration(); + serviceCollection.AddLogging(b => + { + b.AddConsole(); + b.AddApplicationInsights(configuration["ApplicationInsights:InstrumentationKey"] ?? ""); + }); + serviceCollection.AddMediatR(typeof(Program).Assembly); + serviceCollection.AddHttpClient(); + serviceCollection.AddSingleton(); + } + + private static IConfigurationRoot GetConfiguration() + { + return new ConfigurationBuilder() + .AddEnvironmentVariables() + .Build(); + } + } +} \ No newline at end of file diff --git a/src/FilterLists.Agent/FilterLists.Agent.csproj b/src/FilterLists.Agent/FilterLists.Agent.csproj index a0df16f14..71da73361 100644 --- a/src/FilterLists.Agent/FilterLists.Agent.csproj +++ b/src/FilterLists.Agent/FilterLists.Agent.csproj @@ -27,8 +27,7 @@ - - + diff --git a/src/FilterLists.Agent/Infrastructure/AgentHttpClient.cs b/src/FilterLists.Agent/Infrastructure/Clients/AgentHttpClient.cs similarity index 83% rename from src/FilterLists.Agent/Infrastructure/AgentHttpClient.cs rename to src/FilterLists.Agent/Infrastructure/Clients/AgentHttpClient.cs index e3ce1e37d..12b21a0c3 100644 --- a/src/FilterLists.Agent/Infrastructure/AgentHttpClient.cs +++ b/src/FilterLists.Agent/Infrastructure/Clients/AgentHttpClient.cs @@ -1,9 +1,11 @@ using System; using System.Net.Http; using System.Net.Http.Headers; +using JetBrains.Annotations; -namespace FilterLists.Agent.Infrastructure +namespace FilterLists.Agent.Infrastructure.Clients { + [UsedImplicitly] public class AgentHttpClient { public AgentHttpClient(HttpClient client) diff --git a/src/FilterLists.Agent/Infrastructure/FilterListsApiClient.cs b/src/FilterLists.Agent/Infrastructure/Clients/FilterListsApiClient.cs similarity index 65% rename from src/FilterLists.Agent/Infrastructure/FilterListsApiClient.cs rename to src/FilterLists.Agent/Infrastructure/Clients/FilterListsApiClient.cs index 7e3f1c495..c639f9003 100644 --- a/src/FilterLists.Agent/Infrastructure/FilterListsApiClient.cs +++ b/src/FilterLists.Agent/Infrastructure/Clients/FilterListsApiClient.cs @@ -1,14 +1,12 @@ using System; -using System.Collections.Generic; using System.Threading.Tasks; -using FilterLists.Agent.Entities; using RestSharp; -namespace FilterLists.Agent.Infrastructure +namespace FilterLists.Agent.Infrastructure.Clients { public interface IFilterListsApiClient { - Task> GetListInfo(); + Task ExecuteAsync(IRestRequest request); } public class FilterListsApiClient : IFilterListsApiClient @@ -22,13 +20,7 @@ public FilterListsApiClient() _restClient = new RestClient(FilterListsApiBaseUrl) {UserAgent = "FilterLists.Agent"}; } - public async Task> GetListInfo() - { - var listsRequest = new RestRequest("lists"); - return await ExecuteAsync>(listsRequest); - } - - private async Task ExecuteAsync(IRestRequest request) + public async Task ExecuteAsync(IRestRequest request) { var response = await _restClient.ExecuteTaskAsync(request); if (response.ErrorException == null) diff --git a/src/FilterLists.Agent/Infrastructure/Repositories/ListInfoRepository.cs b/src/FilterLists.Agent/Infrastructure/Repositories/ListInfoRepository.cs new file mode 100644 index 000000000..900c84ea1 --- /dev/null +++ b/src/FilterLists.Agent/Infrastructure/Repositories/ListInfoRepository.cs @@ -0,0 +1,25 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using FilterLists.Agent.Core.Entities; +using FilterLists.Agent.Core.Interfaces; +using FilterLists.Agent.Infrastructure.Clients; +using RestSharp; + +namespace FilterLists.Agent.Infrastructure.Repositories +{ + public class ListInfoRepository : IListInfoRepository + { + private readonly IFilterListsApiClient _apiClient; + + public ListInfoRepository(IFilterListsApiClient apiClient) + { + _apiClient = apiClient; + } + + public async Task> GetAll() + { + var listsRequest = new RestRequest("lists"); + return await _apiClient.ExecuteAsync>(listsRequest); + } + } +} \ No newline at end of file diff --git a/src/FilterLists.Agent/Program.cs b/src/FilterLists.Agent/Program.cs index 85e6f95ec..067489a6c 100644 --- a/src/FilterLists.Agent/Program.cs +++ b/src/FilterLists.Agent/Program.cs @@ -1,13 +1,9 @@ using System; using System.Threading.Tasks; -using Autofac; -using Autofac.Extensions.DependencyInjection; -using FilterLists.Agent.Infrastructure; -using FilterLists.Agent.ListArchiver; +using FilterLists.Agent.Application.Archiver; +using FilterLists.Agent.Extensions; using MediatR; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; namespace FilterLists.Agent { @@ -17,40 +13,17 @@ public static class Program public static async Task Main() { - RegisterServices(); + BuildServiceProvider(); var mediator = _serviceProvider.GetService(); await mediator.Send(new CaptureLists.Command()); - - ((IDisposable) _serviceProvider).Dispose(); } - private static void RegisterServices() + private static void BuildServiceProvider() { - var configuration = GetConfiguration(); var serviceCollection = new ServiceCollection(); - - // register Agent services - serviceCollection.AddLogging(b => - { - b.AddConsole(); - b.AddApplicationInsights(configuration["ApplicationInsights:InstrumentationKey"] ?? ""); - }); - serviceCollection.AddMediatR(typeof(Program).Assembly); - serviceCollection.AddHttpClient(); - serviceCollection.AddSingleton(); - - var containerBuilder = new ContainerBuilder(); - containerBuilder.Populate(serviceCollection); - var container = containerBuilder.Build(); - _serviceProvider = new AutofacServiceProvider(container); - } - - private static IConfigurationRoot GetConfiguration() - { - return new ConfigurationBuilder() - .AddEnvironmentVariables() - .Build(); + serviceCollection.RegisterAgentServices(); + _serviceProvider = serviceCollection.BuildServiceProvider(); } } } \ No newline at end of file