-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTermiiClient.cs
More file actions
83 lines (67 loc) · 2.35 KB
/
Copy pathTermiiClient.cs
File metadata and controls
83 lines (67 loc) · 2.35 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
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
namespace Termii;
/// <summary>
/// Main entry point for the Termii SDK.
/// </summary>
public sealed class TermiiClient : IDisposable
{
private readonly HttpClient _httpClient;
private readonly bool _ownsHttpClient;
private readonly TermiiJsonHttpPipeline _pipeline;
public TermiiClient(TermiiOptions options)
: this(CreateDefaultHttpClient(options), options, ownsHttpClient: true)
{
}
public TermiiClient(HttpClient httpClient, TermiiOptions options)
: this(httpClient, options, ownsHttpClient: false)
{
}
private TermiiClient(HttpClient httpClient, TermiiOptions options, bool ownsHttpClient)
{
Options = options ?? throw new ArgumentNullException(nameof(options));
Options.Validate();
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
_ownsHttpClient = ownsHttpClient;
_httpClient.BaseAddress ??= Options.BaseUrl;
if (_ownsHttpClient)
{
_httpClient.Timeout = Options.Timeout;
}
_pipeline = new TermiiJsonHttpPipeline(_httpClient, Options);
Messaging = new TermiiMessagingClient(_pipeline);
SenderIds = new TermiiSenderIdClient(_pipeline);
Numbers = new TermiiNumberClient(_pipeline);
Tokens = new TermiiTokenClient(_pipeline);
}
public TermiiOptions Options { get; }
public ITermiiMessagingClient Messaging { get; }
public ITermiiSenderIdClient SenderIds { get; }
public ITermiiNumberClient Numbers { get; }
public ITermiiTokenClient Tokens { get; }
public void Dispose()
{
if (_ownsHttpClient)
{
_httpClient.Dispose();
}
}
internal Task<HttpResponseMessage> SendAsync(
HttpMethod method,
string path,
object? body = null,
TermiiAuthenticationLocation authenticationLocation = TermiiAuthenticationLocation.Query,
CancellationToken cancellationToken = default)
{
return _pipeline.SendAsync(method, path, body, authenticationLocation, cancellationToken);
}
private static HttpClient CreateDefaultHttpClient(TermiiOptions options)
{
if (options is null)
{
throw new ArgumentNullException(nameof(options));
}
return new HttpClient
{
BaseAddress = options.BaseUrl,
};
}
}