fire ready for capture events, other cleanup

This commit is contained in:
Collin M. Barrett 2019-06-26 16:16:17 -05:00
parent 09e8a58d15
commit 095c37e7b1
7 changed files with 65 additions and 49 deletions

View file

@ -1,28 +0,0 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using RestSharp;
namespace FilterLists.Agent
{
public class CaptureLists
{
public class Command : IRequest
{
}
public class Handler : AsyncRequestHandler<Command>
{
private readonly IFilterListsApiClient apiClient;
public Handler(IFilterListsApiClient apiClient) => this.apiClient = apiClient;
protected override async Task Handle(Command request, CancellationToken cancellationToken)
{
var listsRequest = new RestRequest("lists");
var restResponse = await apiClient.ExecuteAsync<IEnumerable<FilterListDto>>(listsRequest);
}
}
}
}

View file

@ -1,6 +1,6 @@
namespace FilterLists.Agent
{
public class FilterListDto
public class FilterList
{
public int Id { get; private set; }
public string ViewUrl { get; private set; }

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Microsoft Managed Recommended Rules" Description="These rules focus on the most critical problems in your code, including potential security holes, application crashes, and other important logic and design errors. It is recommended to include this rule set in any custom rule set you create for your projects." ToolsVersion="10.0">
<RuleSet Name="Microsoft Managed Recommended Rules" Description="These rules focus on the most critical problems in your code, including potential security holes, application crashes, and other important logic and design errors. It is recommended to include this rule set in any custom rule set you create for your projects." ToolsVersion="16.0">
<Localization ResourceAssembly="Microsoft.VisualStudio.CodeAnalysis.RuleSets.Strings.dll" ResourceBaseName="Microsoft.VisualStudio.CodeAnalysis.RuleSets.Strings.Localized">
<Name Resource="MinimumRecommendedRules_Name" />
<Description Resource="MinimumRecommendedRules_Description" />
@ -9,6 +9,7 @@
<Rule Id="CA1009" Action="Warning" />
<Rule Id="CA1016" Action="Warning" />
<Rule Id="CA1033" Action="Warning" />
<Rule Id="CA1034" Action="None" />
<Rule Id="CA1049" Action="Warning" />
<Rule Id="CA1060" Action="Warning" />
<Rule Id="CA1061" Action="Warning" />

View file

@ -2,7 +2,7 @@
using System.Threading.Tasks;
using RestSharp;
namespace FilterLists.Agent
namespace FilterLists.Agent.Infrastructure
{
public interface IFilterListsApiClient
{
@ -13,13 +13,16 @@ public class FilterListsApiClient : IFilterListsApiClient
{
private const string FilterListsApiBaseUrl = "https://filterlists.com/api/v1";
private const string ExceptionMessage = "Error retrieving response from FilterLists API.";
private readonly IRestClient restClient;
private readonly IRestClient _restClient;
public FilterListsApiClient() => restClient = new RestClient(FilterListsApiBaseUrl);
public FilterListsApiClient()
{
_restClient = new RestClient(FilterListsApiBaseUrl);
}
public async Task<TResponse> ExecuteAsync<TResponse>(RestRequest request)
{
var response = await restClient.ExecuteTaskAsync<TResponse>(request);
var response = await _restClient.ExecuteTaskAsync<TResponse>(request);
if (response.ErrorException == null)
return response.Data;
throw new ApplicationException(ExceptionMessage, response.ErrorException);

View file

@ -0,0 +1,37 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using FilterLists.Agent.Infrastructure;
using FilterLists.Agent.ListArchiver.Events;
using MediatR;
using RestSharp;
namespace FilterLists.Agent.ListArchiver
{
public class CaptureAllLists
{
public class Command : IRequest
{
}
public class Handler : AsyncRequestHandler<Command>
{
private readonly IFilterListsApiClient _apiClient;
private readonly IMediator _mediator;
public Handler(IFilterListsApiClient apiClient, IMediator mediator)
{
_apiClient = apiClient;
_mediator = mediator;
}
protected override async Task Handle(Command request, CancellationToken cancellationToken)
{
var listsRequest = new RestRequest("lists");
var filterLists = await _apiClient.ExecuteAsync<IEnumerable<FilterList>>(listsRequest);
foreach (var filterList in filterLists)
await _mediator.Publish(new ListReadyForCapture(filterList), cancellationToken);
}
}
}
}

View file

@ -0,0 +1,14 @@
using MediatR;
namespace FilterLists.Agent.ListArchiver.Events
{
public class ListReadyForCapture : INotification
{
public ListReadyForCapture(FilterList filterList)
{
FilterList = filterList;
}
public FilterList FilterList { get; }
}
}

View file

@ -2,10 +2,11 @@
using System.Threading.Tasks;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using FilterLists.Agent.Infrastructure;
using FilterLists.Agent.ListArchiver;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
//TODO: get raw list urls and IDs
//TODO: foreach list, download and persist raw list to disk with standardized name and overwriting the previous version
//TODO: git add .
//TODO: git commit
@ -23,9 +24,9 @@ public static async Task Main()
var mediator = _serviceProvider.GetService<IMediator>();
await mediator.Send(new CaptureLists.Command());
await mediator.Send(new CaptureAllLists.Command());
DisposeServices();
((IDisposable) _serviceProvider).Dispose();
}
private static void RegisterServices()
@ -41,17 +42,5 @@ private static void RegisterServices()
var container = containerBuilder.Build();
_serviceProvider = new AutofacServiceProvider(container);
}
private static void DisposeServices()
{
switch (_serviceProvider)
{
case null:
return;
case IDisposable disposableServiceProvider:
disposableServiceProvider.Dispose();
break;
}
}
}
}