-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGroundControlConfigurationSource.cs
More file actions
43 lines (35 loc) · 1.91 KB
/
Copy pathGroundControlConfigurationSource.cs
File metadata and controls
43 lines (35 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System.Net.Http.Headers;
using Microsoft.Extensions.Logging.Abstractions;
namespace GroundControl.Link;
/// <summary>
/// An <see cref="IConfigurationSource"/> that creates a <see cref="GroundControlConfigurationProvider"/>
/// for loading configuration from a GroundControl server.
/// </summary>
internal sealed class GroundControlConfigurationSource : IConfigurationSource
{
private readonly GroundControlOptions _options;
public GroundControlConfigurationSource(GroundControlOptions options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
}
/// <inheritdoc />
public IConfigurationProvider Build(IConfigurationBuilder builder)
{
var store = new GroundControlStore(_options);
IConfigurationCache cache = _options.EnableLocalCache
? new FileConfigurationCache(_options, NullLogger<FileConfigurationCache>.Instance)
: NullConfigurationCache.Instance;
// Short-lived HttpClient for the conditional GET in Load().
// Background services use IHttpClientFactory for long-lived connections.
var httpClient = new HttpClient { BaseAddress = _options.ServerUrl };
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(HeaderNames.ApiKey, $"{_options.ClientId}:{_options.ClientSecret}");
httpClient.DefaultRequestHeaders.Add(HeaderNames.ApiVersion, _options.ApiVersion);
if (_options.Scopes.Count > 0)
{
var scopeValue = string.Join(",", _options.Scopes.Select(s => $"{Uri.EscapeDataString(s.Key)}:{Uri.EscapeDataString(s.Value)}"));
httpClient.DefaultRequestHeaders.Add(HeaderNames.GroundControlScopes, scopeValue);
}
IGroundControlApiClient client = new GroundControlApiClient(httpClient, NullLogger<GroundControlApiClient>.Instance);
return new GroundControlConfigurationProvider(store, cache, client);
}
}