-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientOptions.cs
More file actions
107 lines (96 loc) · 3.94 KB
/
ClientOptions.cs
File metadata and controls
107 lines (96 loc) · 3.94 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Net.Http;
using Sentdm.Exceptions;
namespace Sentdm.Core;
/// <summary>
/// A class representing the SDK client configuration.
/// </summary>
public record struct ClientOptions()
{
/// <summary>
/// The default value used for <see cref="MaxRetries"/>.
/// </summary>
public static readonly int DefaultMaxRetries = 2;
/// <summary>
/// The default value used for <see cref="Timeout"/>.
/// </summary>
public static readonly TimeSpan DefaultTimeout = TimeSpan.FromMinutes(1);
/// <summary>
/// The HTTP client to use for making requests in the SDK.
/// </summary>
public HttpClient HttpClient { get; set; } = new();
Lazy<string> _baseUrl = new(() =>
Environment.GetEnvironmentVariable("SENT_DM_BASE_URL") ?? EnvironmentUrl.Production
);
/// <summary>
/// The base URL to use for every request.
///
/// <para>Defaults to the production environment: <see cref="EnvironmentUrl.Production"/></para>
/// </summary>
public string BaseUrl
{
readonly get { return _baseUrl.Value; }
set { _baseUrl = new(() => value); }
}
/// <summary>
/// Whether to validate response bodies before returning them.
///
/// <para>Defaults to false, which means the shape of the response body will not be validated upfront.
/// Instead, validation will only occur for the parts of the response body that are accessed.</para>
///
/// <para>Note that when set to true, the response body is only validated if the response is
/// deserialized. Methods that don't eagerly deserialize the response, such as those on
/// <see cref="ISentDmClient.WithRawResponse"/>, don't perform validation until deserialization
/// is triggered.</para>
/// </summary>
public bool ResponseValidation { get; set; } = false;
/// <summary>
/// The maximum number of times to retry failed requests, with a short exponential backoff between requests.
///
/// <para>
/// Only the following error types are retried:
/// <list type="bullet">
/// <item>Connection errors (for example, due to a network connectivity problem)</item>
/// <item>408 Request Timeout</item>
/// <item>409 Conflict</item>
/// <item>429 Rate Limit</item>
/// <item>5xx Internal</item>
/// </list>
/// </para>
///
/// <para>The API may also explicitly instruct the SDK to retry or not retry a request.</para>
///
/// <para>Defaults to 2 when null. Set to 0 to
/// disable retries, which also ignores API instructions to retry.</para>
/// </summary>
public int? MaxRetries { get; set; } = null;
/// <summary>
/// Sets the maximum time allowed for a complete HTTP call, not including retries.
///
/// <para>This includes resolving DNS, connecting, writing the request body, server processing, as
/// well as reading the response body.</para>
///
/// <para>Defaults to <c>TimeSpan.FromMinutes(1)</c> when null.</para>
/// </summary>
public TimeSpan? Timeout { get; set; } = null;
/// <summary>
/// Customer API key for authentication. Use `sk_live_*` keys for production
/// and `sk_test_*` keys for sandbox/testing. Pass via the `x-api-key` header.
/// </summary>
Lazy<string> _apiKey = new(() =>
Environment.GetEnvironmentVariable("SENT_DM_API_KEY")
?? throw new SentDmInvalidDataException(
string.Format("{0} cannot be null", nameof(ApiKey)),
new ArgumentNullException(nameof(ApiKey))
)
);
/// <summary>
/// Customer API key for authentication. Use `sk_live_*` keys for production
/// and `sk_test_*` keys for sandbox/testing. Pass via the `x-api-key` header.
/// </summary>
public string ApiKey
{
readonly get { return _apiKey.Value; }
set { _apiKey = new(() => value); }
}
}