2using System.Collections.Generic;
5using Microsoft.Extensions.Logging;
6using Microsoft.Extensions.Options;
36 readonly ILogger<GitHubClientFactory>
logger;
46 readonly Dictionary<string, (GitHubClient Client, DateTimeOffset LastUsed)>
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)));
83 DateTimeOffset? lastUsed;
87 if (String.IsNullOrWhiteSpace(accessToken))
93 cacheKey = accessToken;
95 cacheHit =
clientCache.TryGetValue(cacheKey, out var tuple);
97 var now = DateTimeOffset.UtcNow;
101 client =
new GitHubClient(
102 new ProductHeaderValue(
106 if (accessToken !=
null)
107 client.Credentials =
new Credentials(accessToken);
109 clientCache.Add(cacheKey, (Client: client, LastUsed: now));
114 logger.LogTrace(
"Cache hit for GitHubClient");
115 client = tuple.Client;
116 lastUsed = tuple.LastUsed;
117 tuple.LastUsed = now;
129 if (tuple.LastUsed <= purgeAfter)
138 "Pruned {count} expired GitHub client(s) from cache that haven't been used in {purgeAfterHours} days.",
143 var rateLimitInfo = client.GetLastApiInfo()?.RateLimit;
144 if (rateLimitInfo !=
null)
145 if (rateLimitInfo.Remaining == 0)
147 "Requested GitHub client has no requests remaining! Limit resets at {resetTime}",
148 rateLimitInfo.Reset.ToString(
"o"));
149 else if (rateLimitInfo.Remaining < 25)
151 "Requested GitHub client has only {remainingRequests} requests remaining after the usage at {lastUse}! Limit resets at {resetTime}",
152 rateLimitInfo.Remaining,
154 rateLimitInfo.Reset.ToString(
"o"));
157 "Requested GitHub client has {remainingRequests} requests remaining after the usage at {lastUse}. Limit resets at {resetTime}",
158 rateLimitInfo.Remaining,
160 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...
readonly Dictionary< string,(GitHubClient Client, DateTimeOffset LastUsed)> clientCache
Cache of created GitHubClients and last used times, keyed by access token.
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.
readonly GeneralConfiguration generalConfiguration
The GeneralConfiguration for the GitHubClientFactory.
GitHubClient GetOrCreateClient(string? accessToken)
Retrieve a GitHubClient from the clientCache or add a new one based on a given accessToken .
const uint ClientCacheDays
Limit to the amount of days a GitHubClient can live in the clientCache.
For creating IGitHubClients.