mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
separate out IListService and IUrlService
This commit is contained in:
parent
84cef36388
commit
7af5e0080e
10 changed files with 89 additions and 39 deletions
|
|
@ -1,9 +0,0 @@
|
|||
using System.Net.Http;
|
||||
|
||||
namespace FilterLists.Agent.Core.Interfaces.Clients
|
||||
{
|
||||
public interface IAgentHttpClientFactory
|
||||
{
|
||||
HttpClient HttpClient { get; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
using System.Net.Http;
|
||||
|
||||
namespace FilterLists.Agent.Core.Interfaces.Services
|
||||
{
|
||||
public interface IListService
|
||||
{
|
||||
HttpClient HttpClient { get; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
using System.Net.Http;
|
||||
|
||||
namespace FilterLists.Agent.Core.Interfaces.Services
|
||||
{
|
||||
public interface IUrlService
|
||||
{
|
||||
HttpClient HttpClient { get; }
|
||||
}
|
||||
}
|
||||
|
|
@ -5,8 +5,10 @@
|
|||
using FilterLists.Agent.AppSettings;
|
||||
using FilterLists.Agent.Core.Interfaces.Clients;
|
||||
using FilterLists.Agent.Core.Interfaces.Repositories;
|
||||
using FilterLists.Agent.Core.Interfaces.Services;
|
||||
using FilterLists.Agent.Infrastructure.Clients;
|
||||
using FilterLists.Agent.Infrastructure.Repositories;
|
||||
using FilterLists.Agent.Infrastructure.Services;
|
||||
using LibGit2Sharp;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
|
@ -30,8 +32,9 @@ public static void RegisterAgentServices(this IServiceCollection services)
|
|||
services.AddTransient<Parser>();
|
||||
services.AddMediatR(AppDomain.CurrentDomain.GetAssemblies());
|
||||
services.AddSingleton<IFilterListsApiClient, FilterListsApiClient>();
|
||||
services.AddAgentHttpClient();
|
||||
services.AddGitHubClient();
|
||||
services.AddListService();
|
||||
services.AddUrlService();
|
||||
services.AddArchiveRepository();
|
||||
services.AddTransient<IListInfoRepository, ListInfoRepository>();
|
||||
services.AddTransient<IUrlRepository, UrlRepository>();
|
||||
|
|
@ -72,19 +75,6 @@ private static void AddLoggingCustom(this IServiceCollection services)
|
|||
});
|
||||
}
|
||||
|
||||
private static void AddAgentHttpClient(this IServiceCollection services)
|
||||
{
|
||||
services.AddHttpClient<IAgentHttpClientFactory, AgentHttpClientFactory>()
|
||||
.ConfigureHttpMessageHandlerBuilder(b =>
|
||||
{
|
||||
b.PrimaryHandler = new HttpClientHandler {AllowAutoRedirect = false};
|
||||
b.Build();
|
||||
})
|
||||
.AddTransientHttpErrorPolicy(b =>
|
||||
b.OrResult(r => r.StatusCode == HttpStatusCode.TooManyRequests)
|
||||
.WaitAndRetryAsync(5, i => i * TimeSpan.FromSeconds(3)));
|
||||
}
|
||||
|
||||
private static void AddGitHubClient(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IGitHubClient, GitHubClient>(s =>
|
||||
|
|
@ -97,6 +87,32 @@ private static void AddGitHubClient(this IServiceCollection services)
|
|||
});
|
||||
}
|
||||
|
||||
private static void AddListService(this IServiceCollection services)
|
||||
{
|
||||
services.AddHttpClient<IListService, ListService>()
|
||||
.ConfigureHttpMessageHandlerBuilder(b =>
|
||||
{
|
||||
b.PrimaryHandler = new HttpClientHandler {AllowAutoRedirect = false};
|
||||
b.Build();
|
||||
})
|
||||
.AddTransientHttpErrorPolicy(b =>
|
||||
b.OrResult(r => r.StatusCode == HttpStatusCode.TooManyRequests)
|
||||
.WaitAndRetryAsync(5, i => i * TimeSpan.FromSeconds(3)));
|
||||
}
|
||||
|
||||
private static void AddUrlService(this IServiceCollection services)
|
||||
{
|
||||
services.AddHttpClient<IUrlService, UrlService>()
|
||||
.ConfigureHttpMessageHandlerBuilder(b =>
|
||||
{
|
||||
b.PrimaryHandler = new HttpClientHandler {AllowAutoRedirect = false};
|
||||
b.Build();
|
||||
})
|
||||
.AddTransientHttpErrorPolicy(b =>
|
||||
b.OrResult(r => r.StatusCode == HttpStatusCode.TooManyRequests)
|
||||
.WaitAndRetryAsync(5, i => i * TimeSpan.FromSeconds(3)));
|
||||
}
|
||||
|
||||
private static void AddArchiveRepository(this IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<IRepository, Repository>(s =>
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FilterLists.Agent.Core.Entities;
|
||||
using FilterLists.Agent.Core.Interfaces.Clients;
|
||||
using FilterLists.Agent.Core.Interfaces.Services;
|
||||
using FilterLists.Agent.Extensions;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
|
@ -30,9 +30,9 @@ public class Handler : AsyncRequestHandler<Command>
|
|||
private readonly HttpClient _httpClient;
|
||||
private readonly ILogger<Handler> _logger;
|
||||
|
||||
public Handler(IAgentHttpClientFactory agentHttpClientFactory, ILogger<Handler> logger)
|
||||
public Handler(IListService listService, ILogger<Handler> logger)
|
||||
{
|
||||
_httpClient = agentHttpClientFactory.HttpClient;
|
||||
_httpClient = listService.HttpClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FilterLists.Agent.Core.Entities;
|
||||
using FilterLists.Agent.Core.Interfaces.Clients;
|
||||
using FilterLists.Agent.Core.Interfaces.Services;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SharpCompress.Archives;
|
||||
|
|
@ -30,9 +30,9 @@ public class Handler : AsyncRequestHandler<Command>
|
|||
private readonly HttpClient _httpClient;
|
||||
private readonly ILogger<Handler> _logger;
|
||||
|
||||
public Handler(IAgentHttpClientFactory agentHttpClientFactory, ILogger<Handler> logger)
|
||||
public Handler(IListService listService, ILogger<Handler> logger)
|
||||
{
|
||||
_httpClient = agentHttpClientFactory.HttpClient;
|
||||
_httpClient = listService.HttpClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FilterLists.Agent.Core.Entities;
|
||||
using FilterLists.Agent.Core.Interfaces.Clients;
|
||||
using FilterLists.Agent.Core.Interfaces.Services;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SharpCompress.Common;
|
||||
|
|
@ -29,9 +29,9 @@ public class Handler : AsyncRequestHandler<Command>
|
|||
private readonly HttpClient _httpClient;
|
||||
private readonly ILogger<Handler> _logger;
|
||||
|
||||
public Handler(IAgentHttpClientFactory agentHttpClientFactory, ILogger<Handler> logger)
|
||||
public Handler(IListService listService, ILogger<Handler> logger)
|
||||
{
|
||||
_httpClient = agentHttpClientFactory.HttpClient;
|
||||
_httpClient = listService.HttpClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading.Tasks.Dataflow;
|
||||
using FilterLists.Agent.Core.Interfaces.Clients;
|
||||
using FilterLists.Agent.Core.Interfaces.Services;
|
||||
using FilterLists.Agent.Extensions;
|
||||
using FilterLists.Agent.Features.Urls.Models.ValidationResults;
|
||||
using MediatR;
|
||||
|
|
@ -31,9 +31,9 @@ public class Handler : IRequestHandler<Command, List<UrlValidationResult>>
|
|||
private readonly HttpClient _httpClient;
|
||||
private readonly ILogger<Handler> _logger;
|
||||
|
||||
public Handler(IAgentHttpClientFactory agentHttpClientFactory, ILogger<Handler> logger)
|
||||
public Handler(IUrlService urlService, ILogger<Handler> logger)
|
||||
{
|
||||
_httpClient = agentHttpClientFactory.HttpClient;
|
||||
_httpClient = urlService.HttpClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using FilterLists.Agent.Core.Interfaces.Clients;
|
||||
using FilterLists.Agent.Core.Interfaces.Services;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace FilterLists.Agent.Infrastructure.Clients
|
||||
namespace FilterLists.Agent.Infrastructure.Services
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public class AgentHttpClientFactory : IAgentHttpClientFactory
|
||||
public class ListService : IListService
|
||||
{
|
||||
public AgentHttpClientFactory(HttpClient client)
|
||||
public ListService(HttpClient client)
|
||||
{
|
||||
client.Timeout = TimeSpan.FromSeconds(90);
|
||||
|
||||
25
src/FilterLists.Agent/Infrastructure/Services/UrlService.cs
Normal file
25
src/FilterLists.Agent/Infrastructure/Services/UrlService.cs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using FilterLists.Agent.Core.Interfaces.Services;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace FilterLists.Agent.Infrastructure.Services
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public class UrlService : IUrlService
|
||||
{
|
||||
public UrlService(HttpClient client)
|
||||
{
|
||||
client.Timeout = TimeSpan.FromSeconds(90);
|
||||
|
||||
var header = new ProductHeaderValue("FilterLists.Agent");
|
||||
var userAgent = new ProductInfoHeaderValue(header);
|
||||
client.DefaultRequestHeaders.UserAgent.Add(userAgent);
|
||||
|
||||
HttpClient = client;
|
||||
}
|
||||
|
||||
public HttpClient HttpClient { get; }
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue