forked from Vonage/vonage-dotnet-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfiguration.cs
111 lines (91 loc) · 4.25 KB
/
Configuration.cs
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Nexmo.Api.ConfigurationExtensions;
using Nexmo.Api.Request;
namespace Nexmo.Api
{
public sealed class Configuration
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Configuration()
{
}
private Configuration()
{
_serviceProvider = new ServiceCollection()
.AddLogging()
.BuildServiceProvider()
;
var loggerFactory = _serviceProvider.GetService<ILoggerFactory>();
var loggingConfiguration = new ConfigurationBuilder()
.AddJsonFile("logging.json", true, true)
.Build();
loggerFactory.AddConsole(loggingConfiguration);
var configLogger = loggerFactory.CreateLogger<Configuration>();
ApiLogger = loggerFactory.CreateLogger("Nexmo.Api");
AuthenticationLogger = loggerFactory.CreateLogger("Nexmo.Api.Authentication");
var builder = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{ "appSettings:Nexmo.Url.Rest", "https://rest.nexmo.com"},
{ "appSettings:Nexmo.Url.Api", "https://api.nexmo.com"}
})
.AddConfigFile("web.config", true, configLogger)
.AddConfigFile("app.config", true, configLogger)
.AddConfigFile($"{System.IO.Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName)}.config", true, configLogger)
.AddJsonFile("settings.json", true, true)
.AddJsonFile("appsettings.json", true, true)
;
Settings = builder.Build();
// verify we have a minimum amount of configuration
var authCapabilities = new List<string>();
if (!string.IsNullOrWhiteSpace(Settings["appSettings:Nexmo.api_key"]) &&
!string.IsNullOrWhiteSpace(Settings["appSettings:Nexmo.api_secret"]))
{
authCapabilities.Add("Key/Secret");
}
if (!string.IsNullOrWhiteSpace(Settings["appSettings:Nexmo.security_secret"]))
{
authCapabilities.Add("Security/Signing");
}
if (!string.IsNullOrWhiteSpace(Settings["appSettings:Nexmo.Application.Id"]) &&
!string.IsNullOrWhiteSpace(Settings["appSettings:Nexmo.Application.Key"]))
{
authCapabilities.Add("Application");
}
if (authCapabilities.Count == 0)
{
throw new ArgumentException("You must provide at least one type of authentication via configuration!");
}
configLogger.LogInformation("Available authentication: {0}", string.Join(",", authCapabilities));
}
private HttpClient _client;
private readonly IServiceProvider _serviceProvider;
internal ILogger ApiLogger;
internal ILogger AuthenticationLogger;
// not convinced we want/need to expose this
//public ILoggerFactory Logger => _serviceProvider.GetService<ILoggerFactory>();
public static Configuration Instance { get; } = new Configuration();
public IConfiguration Settings { get; }
public HttpMessageHandler ClientHandler { get; set; }
public HttpClient Client
{
get
{
var reqPerSec = Instance.Settings["appSettings:Nexmo.Api.RequestsPerSecond"];
if (string.IsNullOrEmpty(reqPerSec))
return _client ?? (_client = new HttpClient());
var delay = 1 / double.Parse(reqPerSec);
var execTimeSpanSemaphore = new TimeSpanSemaphore(1, TimeSpan.FromSeconds(delay));
var handler = ClientHandler != null ? new ThrottlingMessageHandler(execTimeSpanSemaphore, ClientHandler) : new ThrottlingMessageHandler(execTimeSpanSemaphore);
return _client ?? (_client = new HttpClient(handler));
}
}
}
}