register IGitHubClient in DI

This commit is contained in:
Collin M. Barrett 2019-07-05 17:19:57 -05:00
parent 3b3e238af1
commit 3a7b202c58
2 changed files with 21 additions and 8 deletions

View file

@ -11,6 +11,9 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Octokit;
using Credentials = Octokit.Credentials;
using Repository = LibGit2Sharp.Repository;
namespace FilterLists.Agent.Extensions
{
@ -25,6 +28,7 @@ public static void RegisterAgentServices(this IServiceCollection services)
services.AddMediatR(AppDomain.CurrentDomain.GetAssemblies());
services.AddAgentHttpClient();
services.AddSingleton<IFilterListsApiClient, FilterListsApiClient>();
services.AddGitHubClient();
services.AddSingleton<IAgentGitHubClient, AgentGitHubClient>();
services.AddArchiveRepository();
services.AddTransient<IListInfoRepository, ListInfoRepository>();
@ -77,6 +81,18 @@ private static void AddAgentHttpClient(this IServiceCollection services)
});
}
private static void AddGitHubClient(this IServiceCollection services)
{
services.AddSingleton<IGitHubClient, GitHubClient>(s =>
{
var gitHubSettings = s.GetService<IOptions<GitHubSettings>>().Value;
return new GitHubClient(new ProductHeaderValue(gitHubSettings.ProductHeaderValue))
{
Credentials = new Credentials(gitHubSettings.PersonalAccessToken)
};
});
}
private static void AddArchiveRepository(this IServiceCollection services)
{
services.AddTransient<IRepository, Repository>(s =>

View file

@ -19,21 +19,18 @@ public interface IAgentGitHubClient
public class AgentGitHubClient : IAgentGitHubClient
{
private readonly GitHubClient _gitHubClient;
private readonly IGitHubClient _gitHubClient;
private readonly GitHubSettings _gitHubSettings;
private readonly IStringLocalizer<AgentGitHubClient> _localizer;
private readonly ILogger<AgentGitHubClient> _logger;
public AgentGitHubClient(IOptions<GitHubSettings> gitHubOptions, ILogger<AgentGitHubClient> logger,
IStringLocalizer<AgentGitHubClient> stringLocalizer)
public AgentGitHubClient(IGitHubClient gitHubClient, IOptions<GitHubSettings> gitHubOptions,
IStringLocalizer<AgentGitHubClient> stringLocalizer, ILogger<AgentGitHubClient> logger)
{
_gitHubClient = gitHubClient;
_gitHubSettings = gitHubOptions.Value;
_logger = logger;
_localizer = stringLocalizer;
_gitHubClient = new GitHubClient(new ProductHeaderValue(_gitHubSettings.ProductHeaderValue))
{
Credentials = new Credentials(_gitHubSettings.PersonalAccessToken)
};
_logger = logger;
}
public async Task<IReadOnlyList<Issue>> GetAllIssues(RepositoryIssueRequest repositoryIssueRequest)