forked from alpacahq/alpaca-trade-api-csharp
/
AlpacaClientConfigurationBase.cs
65 lines (54 loc) · 2.19 KB
/
AlpacaClientConfigurationBase.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
namespace Alpaca.Markets;
/// <summary>
/// Configuration parameters object for all REST API client instances.
/// </summary>
[SuppressMessage("ReSharper", "PropertyCanBeMadeInitOnly.Global")]
public abstract class AlpacaClientConfigurationBase
{
private static readonly Func<HttpMessageHandler, HttpMessageHandler> _defaultHttpMessageHandlerFactory = handler => handler;
/// <summary>
/// Creates new instance of <see cref="AlpacaClientConfigurationBase"/> class.
/// </summary>
/// <exception cref="ArgumentNullException">
/// The <paramref name="apiEndpoint"/> argument is <c>null</c>.
/// </exception>
protected AlpacaClientConfigurationBase(
Uri apiEndpoint)
{
SecurityId = new SecretKey(String.Empty, String.Empty);
ThrottleParameters = ThrottleParameters.Default;
ApiEndpoint = apiEndpoint.EnsureNotNull();
}
/// <summary>
/// Security identifier for API authentication.
/// </summary>
public SecurityKey SecurityId { get; set; }
/// <summary>
/// Gets or sets Alpaca Data API base URL.
/// </summary>
public Uri ApiEndpoint { get; set; }
/// <summary>
/// Gets or sets REST API throttling parameters.
/// </summary>
[UsedImplicitly]
public ThrottleParameters ThrottleParameters { get; [UsedImplicitly] set; }
/// <summary>
/// Gets or sets <see cref="HttpClient"/> instance for connecting.
/// </summary>
[UsedImplicitly]
public HttpClient? HttpClient { get; set; }
[UsedImplicitly]
internal Func<HttpMessageHandler, HttpMessageHandler>? HttpMessageHandlerFactory { get; set; }
// ReSharper disable once MemberCanBeProtected.Global
internal abstract Uri GetApiEndpoint();
internal HttpClient GetConfiguredHttpClient() =>
ensureIsValidAndGetHttpClient().Configure(SecurityId, GetApiEndpoint());
private HttpClient ensureIsValidAndGetHttpClient()
{
ThrottleParameters.EnsurePropertyNotNull();
ApiEndpoint.EnsurePropertyNotNull();
SecurityId.EnsurePropertyNotNull();
return HttpClient ?? ThrottleParameters.GetHttpClient(
HttpMessageHandlerFactory ?? _defaultHttpMessageHandlerFactory);
}
}