2using System.Collections.Generic;
5using Microsoft.Extensions.Logging;
6using Microsoft.Extensions.Options;
36 readonly ILogger<GitHubClientFactory>
logger;
46 readonly Dictionary<string, (GitHubClient, DateTimeOffset)>
clientCache;
56 ILogger<GitHubClientFactory>
logger,
57 IOptions<GeneralConfiguration> generalConfigurationOptions)
60 this.logger =
logger ??
throw new ArgumentNullException(nameof(
logger));
61 generalConfiguration = generalConfigurationOptions?.Value ??
throw new ArgumentNullException(nameof(generalConfigurationOptions));
63 clientCache =
new Dictionary<string, (GitHubClient, DateTimeOffset)>();
72 accessToken ??
throw new ArgumentNullException(nameof(accessToken)));
86 if (String.IsNullOrWhiteSpace(accessToken))
92 cacheKey = accessToken;
94 cacheHit =
clientCache.TryGetValue(cacheKey, out var tuple);
96 var now = DateTimeOffset.UtcNow;
99 logger.LogTrace(
"Creating new GitHubClient...");
100 client =
new GitHubClient(
101 new ProductHeaderValue(
105 if (accessToken !=
null)
106 client.Credentials =
new Credentials(accessToken);
112 logger.LogTrace(
"Cache hit for GitHubClient");
113 client = tuple.Item1;
126 if (tuple.Item2 <= purgeAfter)
135 "Pruned {count} expired GitHub client(s) from cache that haven't been used in {purgeAfterHours} days.",
140 var rateLimitInfo = client.GetLastApiInfo()?.RateLimit;
141 if (rateLimitInfo !=
null)
142 if (rateLimitInfo.Remaining == 0)
144 "Requested GitHub client has no requests remaining! Limit resets at {resetTime}",
145 rateLimitInfo.Reset.ToString(
"o"));
146 else if (rateLimitInfo.Remaining < 25)
148 "Requested GitHub client has only {remainingRequests} requests remaining! Limit resets at {resetTime}",
149 rateLimitInfo.Remaining,
150 rateLimitInfo.Reset.ToString(
"o"));
153 "Requested GitHub client has {remainingRequests} requests remaining. Limit resets at {resetTime}",
154 rateLimitInfo.Remaining,
155 rateLimitInfo.Reset.ToString(
"o"));
General configuration options.
string GitHubAccessToken
A GitHub personal access token to use for bypassing rate limits on requests. Requires no scopes.
IGitHubClient CreateClient()
Create a IGitHubClient client. Low rate limit unless the server's GitHubAccessToken is set to bypass ...
const string DefaultCacheKey
The clientCache KeyValuePair<TKey, TValue>.Key used in place of null when accessing a configuration-b...
GitHubClientFactory(IAssemblyInformationProvider assemblyInformationProvider, ILogger< GitHubClientFactory > logger, IOptions< GeneralConfiguration > generalConfigurationOptions)
Initializes a new instance of the GitHubClientFactory class.
readonly IAssemblyInformationProvider assemblyInformationProvider
The IAssemblyInformationProvider for the GitHubClientFactory.
IGitHubClient CreateClient(string accessToken)
Create a client with authentication using a personal access token. A new IGitHubClient.
readonly ILogger< GitHubClientFactory > logger
The ILogger for the GitHubClientFactory.
GitHubClient GetOrCreateClient(string accessToken)
Retrieve a GitHubClient from the clientCache or add a new one based on a given accessToken .
readonly GeneralConfiguration generalConfiguration
The GeneralConfiguration for the GitHubClientFactory.
const uint ClientCacheDays
Limit to the amount of days a GitHubClient can live in the clientCache.
readonly Dictionary< string,(GitHubClient, DateTimeOffset)> clientCache
Cache of created GitHubClients and last used times, keyed by access token.
For creating IGitHubClients.