support CancellationTokenon async APIs

This commit is contained in:
Collin M. Barrett 2019-07-08 13:53:35 -05:00
parent 4202bce222
commit d035e05bd2
8 changed files with 20 additions and 17 deletions

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using FilterLists.Agent.Core.Entities;
@ -6,6 +7,6 @@ namespace FilterLists.Agent.Core.Interfaces.Repositories
{
public interface IListInfoRepository
{
Task<IEnumerable<ListInfo>> GetAllAsync();
Task<IEnumerable<ListInfo>> GetAllAsync(CancellationToken cancellationToken);
}
}

View file

@ -8,7 +8,7 @@ namespace FilterLists.Agent.Core.Interfaces.Repositories
{
public interface IUrlRepository
{
Task<IEnumerable<Uri>> GetAllAsync<TModel>();
Task<IEnumerable<Uri>> GetAllAsync<TModel>(CancellationToken cancellationToken);
Task<UrlValidationResult> ValidateAsync(Uri u, CancellationToken cancellationToken);
}

View file

@ -24,7 +24,7 @@ public Handler(IMediator mediator, IListInfoRepository listInfoRepository)
protected override async Task Handle(Command request, CancellationToken cancellationToken)
{
var lists = await _repo.GetAllAsync();
var lists = await _repo.GetAllAsync(cancellationToken);
await _mediator.Send(new DownloadLists.Command(lists), cancellationToken);
await _mediator.Send(new CommitLists.Command(), cancellationToken);
}

View file

@ -30,28 +30,28 @@ protected override async Task Handle(Command request, CancellationToken cancella
{
var results = new List<DataFileUrlValidationResults>();
var licenseUrls = await _repo.GetAllAsync<LicenseUrls>();
var licenseUrls = await _repo.GetAllAsync<LicenseUrls>(cancellationToken);
var licenseUrlErrors = await _mediator.Send(new ValidateUrls.Command(licenseUrls), cancellationToken);
if (licenseUrlErrors.Any())
results.Add(new DataFileUrlValidationResults("License.json", licenseUrlErrors));
var listUrls = await _repo.GetAllAsync<ListUrls>();
var listUrls = await _repo.GetAllAsync<ListUrls>(cancellationToken);
var listUrlErrors = await _mediator.Send(new ValidateUrls.Command(listUrls), cancellationToken);
if (listUrlErrors.Any())
results.Add(new DataFileUrlValidationResults("FilterList.json", listUrlErrors));
var maintainerUrls = await _repo.GetAllAsync<MaintainerUrls>();
var maintainerUrls = await _repo.GetAllAsync<MaintainerUrls>(cancellationToken);
var maintainerUrlErrors =
await _mediator.Send(new ValidateUrls.Command(maintainerUrls), cancellationToken);
if (maintainerUrlErrors.Any())
results.Add(new DataFileUrlValidationResults("Maintainer.json", maintainerUrlErrors));
var softwareUrls = await _repo.GetAllAsync<SoftwareUrls>();
var softwareUrls = await _repo.GetAllAsync<SoftwareUrls>(cancellationToken);
var softwareUrlErrors = await _mediator.Send(new ValidateUrls.Command(softwareUrls), cancellationToken);
if (softwareUrlErrors.Any())
results.Add(new DataFileUrlValidationResults("Software.json", softwareUrlErrors));
var syntaxUrls = await _repo.GetAllAsync<SyntaxUrls>();
var syntaxUrls = await _repo.GetAllAsync<SyntaxUrls>(cancellationToken);
var syntaxUrlErrors = await _mediator.Send(new ValidateUrls.Command(syntaxUrls), cancellationToken);
if (syntaxUrlErrors.Any())
results.Add(new DataFileUrlValidationResults("Syntax.json", syntaxUrlErrors));

View file

@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using FilterLists.Agent.AppSettings;
using Microsoft.Extensions.Localization;
@ -19,9 +20,9 @@ public FilterListsApiClient(IStringLocalizer<FilterListsApiClient> stringLocaliz
_restClient = new RestClient(filterListsApiOptions.Value.BaseUrl) {UserAgent = "FilterLists.Agent"};
}
public async Task<TResponse> ExecuteAsync<TResponse>(IRestRequest request)
public async Task<TResponse> ExecuteAsync<TResponse>(IRestRequest request, CancellationToken cancellationToken)
{
var response = await _restClient.ExecuteTaskAsync<TResponse>(request);
var response = await _restClient.ExecuteTaskAsync<TResponse>(request, cancellationToken);
if (response.ErrorException == null)
return response.Data;
throw new ApplicationException(_localizer["Error retrieving response from the FilterLists API."],

View file

@ -1,10 +1,11 @@
using System.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
using RestSharp;
namespace FilterLists.Agent.Infrastructure.Clients
{
public interface IFilterListsApiClient
{
Task<TResponse> ExecuteAsync<TResponse>(IRestRequest request);
Task<TResponse> ExecuteAsync<TResponse>(IRestRequest request, CancellationToken cancellationToken);
}
}

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using FilterLists.Agent.Core.Entities;
using FilterLists.Agent.Core.Interfaces.Repositories;
@ -16,10 +17,10 @@ public ListInfoRepository(IFilterListsApiClient apiClient)
_apiClient = apiClient;
}
public async Task<IEnumerable<ListInfo>> GetAllAsync()
public async Task<IEnumerable<ListInfo>> GetAllAsync(CancellationToken cancellationToken)
{
var listsRequest = new RestRequest("lists");
return await _apiClient.ExecuteAsync<IEnumerable<ListInfo>>(listsRequest);
return await _apiClient.ExecuteAsync<IEnumerable<ListInfo>>(listsRequest, cancellationToken);
}
}
}

View file

@ -50,17 +50,16 @@ public UrlRepository(IFilterListsApiClient apiClient, HttpClient httpClient,
_logger = logger;
}
public async Task<IEnumerable<Uri>> GetAllAsync<TModel>()
public async Task<IEnumerable<Uri>> GetAllAsync<TModel>(CancellationToken cancellationToken)
{
if (!EntityUrlsEndpoints.ContainsKey(typeof(TModel).Name))
throw new InvalidEnumArgumentException(_localizer["The type of TModel is not valid."]);
var request = new RestRequest($"{EntityUrlsEndpoints[typeof(TModel).Name]}/seed");
var response = await _apiClient.ExecuteAsync<IEnumerable<TModel>>(request);
var response = await _apiClient.ExecuteAsync<IEnumerable<TModel>>(request, cancellationToken);
return response.SelectMany(r =>
r.GetType().GetProperties().Where(p => p.GetValue(r) != null).Select(p => (Uri)p.GetValue(r)));
}
public async Task<UrlValidationResult> ValidateAsync(Uri u, CancellationToken cancellationToken)
{
var result = new UrlValidationResult(u);