mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
re-extract IFilterListsApiClient
This commit is contained in:
parent
d3ea23ecd7
commit
4659dd1945
4 changed files with 52 additions and 24 deletions
|
|
@ -0,0 +1,35 @@
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FilterLists.Agent.AppSettings;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using RestSharp;
|
||||
|
||||
namespace FilterLists.Agent.Infrastructure.FilterListsApi
|
||||
{
|
||||
public class FilterListsApiClient : IFilterListsApiClient
|
||||
{
|
||||
private readonly IStringLocalizer<FilterListsApiClient> _localizer;
|
||||
private readonly ILogger<FilterListsApiClient> _logger;
|
||||
private readonly IRestClient _restClient;
|
||||
|
||||
public FilterListsApiClient(IStringLocalizer<FilterListsApiClient> stringLocalizer,
|
||||
ILogger<FilterListsApiClient> logger, IOptions<FilterListsApiSettings> filterListsApiOptions)
|
||||
{
|
||||
_localizer = stringLocalizer;
|
||||
_logger = logger;
|
||||
_restClient = new RestClient(filterListsApiOptions.Value.BaseUrl) {UserAgent = "FilterLists.Agent"};
|
||||
}
|
||||
|
||||
public async Task<TResponse> ExecuteAsync<TResponse>(IRestRequest restRequest,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var response = await _restClient.ExecuteTaskAsync<TResponse>(restRequest, cancellationToken);
|
||||
if (response.ErrorException != null)
|
||||
_logger.LogError(response.ErrorException,
|
||||
_localizer["Error retrieving response from the FilterLists API."]);
|
||||
return response.Data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using RestSharp;
|
||||
|
||||
namespace FilterLists.Agent.Infrastructure.FilterListsApi
|
||||
{
|
||||
public interface IFilterListsApiClient
|
||||
{
|
||||
Task<TResponse> ExecuteAsync<TResponse>(IRestRequest restRequest, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
|
|
@ -2,34 +2,23 @@
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FilterLists.Agent.Core.List;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using RestSharp;
|
||||
|
||||
namespace FilterLists.Agent.Infrastructure.FilterListsApi
|
||||
{
|
||||
public class ListUrlRepository : IListUrlRepository
|
||||
{
|
||||
private readonly IRestClient _apiClient;
|
||||
private readonly IStringLocalizer<ListUrlRepository> _localizer;
|
||||
private readonly ILogger<ListUrlRepository> _logger;
|
||||
private readonly IFilterListsApiClient _apiClient;
|
||||
|
||||
public ListUrlRepository(IRestClient apiClient, IStringLocalizer<ListUrlRepository> stringLocalizer,
|
||||
ILogger<ListUrlRepository> logger)
|
||||
public ListUrlRepository(IFilterListsApiClient filterListsApiClient)
|
||||
{
|
||||
_apiClient = apiClient;
|
||||
_localizer = stringLocalizer;
|
||||
_logger = logger;
|
||||
_apiClient = filterListsApiClient;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ListUrl>> GetAllAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var listsRequest = new RestRequest("lists");
|
||||
var response = await _apiClient.ExecuteTaskAsync<IEnumerable<ListUrl>>(listsRequest, cancellationToken);
|
||||
if (response.ErrorException != null)
|
||||
_logger.LogError(response.ErrorException,
|
||||
_localizer["Error retrieving response from the FilterLists API."]);
|
||||
return response.Data;
|
||||
return await _apiClient.ExecuteAsync<IEnumerable<ListUrl>>(listsRequest, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,5 @@
|
|||
using FilterLists.Agent.AppSettings;
|
||||
using FilterLists.Agent.Core.List;
|
||||
using FilterLists.Agent.Core.List;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using RestSharp;
|
||||
|
||||
namespace FilterLists.Agent.Infrastructure.FilterListsApi
|
||||
{
|
||||
|
|
@ -10,11 +7,7 @@ public static class ServiceCollectionExtensions
|
|||
{
|
||||
public static void AddFilterListsApiResources(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IRestClient, RestClient>(b =>
|
||||
{
|
||||
var filterListsApiSettings = b.GetService<IOptions<FilterListsApiSettings>>().Value;
|
||||
return new RestClient(filterListsApiSettings.BaseUrl) {UserAgent = "FilterLists.Agent"};
|
||||
});
|
||||
services.AddSingleton<IFilterListsApiClient, FilterListsApiClient>();
|
||||
services.AddTransient<IListUrlRepository, ListUrlRepository>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue