mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
refactor towards SRP
This commit is contained in:
parent
ab92f0bcda
commit
50abb3ea05
3 changed files with 44 additions and 30 deletions
|
|
@ -1,12 +1,14 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using FilterLists.Agent.Entities;
|
||||
using RestSharp;
|
||||
|
||||
namespace FilterLists.Agent.Infrastructure
|
||||
{
|
||||
public interface IFilterListsApiClient
|
||||
{
|
||||
Task<TResponse> ExecuteAsync<TResponse>(IRestRequest request);
|
||||
Task<IEnumerable<ListInfo>> GetListInfo();
|
||||
}
|
||||
|
||||
public class FilterListsApiClient : IFilterListsApiClient
|
||||
|
|
@ -20,7 +22,13 @@ public FilterListsApiClient()
|
|||
_restClient = new RestClient(FilterListsApiBaseUrl) {UserAgent = "FilterLists.Agent"};
|
||||
}
|
||||
|
||||
public async Task<TResponse> ExecuteAsync<TResponse>(IRestRequest request)
|
||||
public async Task<IEnumerable<ListInfo>> GetListInfo()
|
||||
{
|
||||
var listsRequest = new RestRequest("lists");
|
||||
return await ExecuteAsync<IEnumerable<ListInfo>>(listsRequest);
|
||||
}
|
||||
|
||||
private async Task<TResponse> ExecuteAsync<TResponse>(IRestRequest request)
|
||||
{
|
||||
var response = await _restClient.ExecuteTaskAsync<TResponse>(request);
|
||||
if (response.ErrorException == null)
|
||||
|
|
|
|||
|
|
@ -1,12 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FilterLists.Agent.Entities;
|
||||
using FilterLists.Agent.Infrastructure;
|
||||
using LibGit2Sharp;
|
||||
using MediatR;
|
||||
using RestSharp;
|
||||
|
||||
namespace FilterLists.Agent.ListArchiver
|
||||
{
|
||||
|
|
@ -18,7 +13,6 @@ public class Command : IRequest
|
|||
|
||||
public class Handler : AsyncRequestHandler<Command>
|
||||
{
|
||||
private const string GitRepoDirectory = @"archives";
|
||||
private readonly IFilterListsApiClient _apiClient;
|
||||
private readonly IMediator _mediator;
|
||||
|
||||
|
|
@ -30,28 +24,9 @@ public Handler(IFilterListsApiClient apiClient, IMediator mediator)
|
|||
|
||||
protected override async Task Handle(Command request, CancellationToken cancellationToken)
|
||||
{
|
||||
var lists = await GetListInfo();
|
||||
var lists = await _apiClient.GetListInfo();
|
||||
await _mediator.Send(new DownloadLists.Command(lists), cancellationToken);
|
||||
CommitDownloadedLists();
|
||||
}
|
||||
|
||||
private async Task<IEnumerable<ListInfo>> GetListInfo()
|
||||
{
|
||||
var listsRequest = new RestRequest("lists");
|
||||
return await _apiClient.ExecuteAsync<IEnumerable<ListInfo>>(listsRequest);
|
||||
}
|
||||
|
||||
private static void CommitDownloadedLists()
|
||||
{
|
||||
if (!Repository.IsValid(GitRepoDirectory))
|
||||
Repository.Init(GitRepoDirectory);
|
||||
using (var repo = new Repository(GitRepoDirectory))
|
||||
{
|
||||
Commands.Stage(repo, "*");
|
||||
var utcNow = DateTime.UtcNow;
|
||||
var signature = new Signature("FilterLists.Agent", "noreply@filterlists.com", utcNow);
|
||||
repo.Commit(utcNow.ToShortDateString() + " FilterLists archive by FilterLists.Agent", signature, signature);
|
||||
}
|
||||
await _mediator.Send(new CommitDownloadedLists.Command(), cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
31
src/FilterLists.Agent/ListArchiver/CommitDownloadedLists.cs
Normal file
31
src/FilterLists.Agent/ListArchiver/CommitDownloadedLists.cs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
using System;
|
||||
using LibGit2Sharp;
|
||||
using MediatR;
|
||||
|
||||
namespace FilterLists.Agent.ListArchiver
|
||||
{
|
||||
public static class CommitDownloadedLists
|
||||
{
|
||||
public class Command : IRequest
|
||||
{
|
||||
}
|
||||
|
||||
public class Handler : RequestHandler<Command>
|
||||
{
|
||||
private const string GitRepoDirectory = @"archives";
|
||||
|
||||
protected override void Handle(Command request)
|
||||
{
|
||||
if (!Repository.IsValid(GitRepoDirectory))
|
||||
Repository.Init(GitRepoDirectory);
|
||||
using (var repo = new Repository(GitRepoDirectory))
|
||||
{
|
||||
Commands.Stage(repo, "*");
|
||||
var utcNow = DateTime.UtcNow;
|
||||
var signature = new Signature("FilterLists.Agent", "noreply@filterlists.com", utcNow);
|
||||
repo.Commit(utcNow.ToShortDateString() + " FilterLists archive by FilterLists.Agent", signature, signature);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue