• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stripe / stripe-dotnet / 11519002614

25 Oct 2024 01:18PM UTC coverage: 46.558% (-0.05%) from 46.606%
11519002614

Pull #3011

coveralls.net

web-flow
Merge e47f89dd2 into 2c30f9d61
Pull Request #3011: Do not allow setting API Version directly on StripeConfiguration

5194 of 11156 relevant lines covered (46.56%)

96.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.17
/src/Stripe.net/Infrastructure/Public/StripeConfiguration.cs
1
namespace Stripe
2
{
3
    using System;
4
    using System.Collections.Generic;
5
    using System.Configuration;
6
    using System.Reflection;
7
    using System.Runtime.Serialization;
8
    using Newtonsoft.Json;
9
    using Stripe.Infrastructure;
10

11
    /// <summary>
12
    /// Global configuration class for Stripe.net settings.
13
    /// </summary>
14
    public static class StripeConfiguration
15
    {
16
        private static string apiKey;
17

18
        private static AppInfo appInfo;
19

20
        private static string clientId;
21

22
        private static bool enableTelemetry = true;
1✔
23

24
        private static int maxNetworkRetries = SystemNetHttpClient.DefaultMaxNumberRetries;
1✔
25

26
        private static IStripeClient stripeClient;
27

28
        static StripeConfiguration()
29
        {
1✔
30
            StripeNetVersion = new AssemblyName(typeof(StripeConfiguration).GetTypeInfo().Assembly.FullName).Version.ToString(3);
1✔
31
            ApiVersion = Stripe.ApiVersion.Current;
1✔
32
        }
1✔
33

34
        /// <summary>API version used by Stripe.net.</summary>
35
        public static string ApiVersion
36
        {
37
            get;
38
            private set;
39
        }
40

41
        /// <summary>Gets or sets the API key.</summary>
42
        /// <remarks>
43
        /// You can also set the API key using the <c>StripeApiKey</c> key in
44
        /// <see cref="System.Configuration.ConfigurationManager.AppSettings"/>.
45
        /// </remarks>
46
        public static string ApiKey
47
        {
48
            get
49
            {
19✔
50
                if (string.IsNullOrEmpty(apiKey) &&
19✔
51
                    !string.IsNullOrEmpty(ConfigurationManager.AppSettings["StripeApiKey"]))
19✔
52
                {
×
53
                    apiKey = ConfigurationManager.AppSettings["StripeApiKey"];
×
54
                }
×
55

56
                return apiKey;
19✔
57
            }
19✔
58

59
            set
60
            {
10✔
61
                if (value != apiKey)
10✔
62
                {
7✔
63
                    StripeClient = null;
7✔
64
                }
7✔
65

66
                apiKey = value;
10✔
67
            }
10✔
68
        }
69

70
        /// <summary>Gets or sets the client ID.</summary>
71
        /// <remarks>
72
        /// You can also set the client ID using the <c>StripeClientId</c> key in
73
        /// <see cref="System.Configuration.ConfigurationManager.AppSettings"/>.
74
        /// </remarks>
75
        public static string ClientId
76
        {
77
            get
78
            {
4✔
79
                if (string.IsNullOrEmpty(clientId) &&
4✔
80
                    !string.IsNullOrEmpty(ConfigurationManager.AppSettings["StripeClientId"]))
4✔
81
                {
×
82
                    clientId = ConfigurationManager.AppSettings["StripeClientId"];
×
83
                }
×
84

85
                return clientId;
4✔
86
            }
4✔
87

88
            set
89
            {
2✔
90
                if (value != clientId)
2✔
91
                {
2✔
92
                    StripeClient = null;
2✔
93
                }
2✔
94

95
                clientId = value;
2✔
96
            }
2✔
97
        }
98

99
        /// <summary>
100
        /// Gets or sets the settings used for deserializing JSON objects returned by Stripe's API.
101
        /// It is highly recommended you do not change these settings, as doing so can produce
102
        /// unexpected results. If you do change these settings, make sure that
103
        /// <see cref="Stripe.Infrastructure.StripeObjectConverter"/> is among the converters,
104
        /// otherwise Stripe.net will no longer be able to deserialize polymorphic resources
105
        /// represented by interfaces (e.g. <see cref="IPaymentSource"/>).
106
        /// </summary>
107
        public static JsonSerializerSettings SerializerSettings { get; set; } = DefaultSerializerSettings();
1✔
108

109
        /// <summary>
110
        /// Gets or sets the maximum number of times that the library will retry requests that
111
        /// appear to have failed due to an intermittent problem.
112
        /// </summary>
113
        public static int MaxNetworkRetries
114
        {
115
            get => maxNetworkRetries;
2✔
116

117
            set
118
            {
×
119
                if (value != maxNetworkRetries)
×
120
                {
×
121
                    StripeClient = null;
×
122
                }
×
123

124
                maxNetworkRetries = value;
×
125
            }
×
126
        }
127

128
        /// <summary>
129
        /// Gets or sets the flag enabling request latency telemetry. Enabled by default.
130
        /// </summary>
131
        public static bool EnableTelemetry
132
        {
133
            get => enableTelemetry;
2✔
134

135
            set
136
            {
×
137
                if (value != enableTelemetry)
×
138
                {
×
139
                    StripeClient = null;
×
140
                }
×
141

142
                enableTelemetry = value;
×
143
            }
×
144
        }
145

146
        /// <summary>
147
        /// Gets or sets a custom <see cref="StripeClient"/> for sending requests to Stripe's
148
        /// API. You can use this to use a custom message handler, set proxy parameters, etc.
149
        /// </summary>
150
        /// <example>
151
        /// To use a custom message handler:
152
        /// <code>
153
        /// System.Net.Http.HttpMessageHandler messageHandler = ...;
154
        /// var httpClient = new System.Net.HttpClient(messageHandler);
155
        /// var stripeClient = new Stripe.StripeClient(
156
        ///     stripeApiKey,
157
        ///     httpClient: new Stripe.SystemNetHttpClient(httpClient));
158
        /// Stripe.StripeConfiguration.StripeClient = stripeClient;
159
        /// </code>
160
        /// </example>
161
        public static IStripeClient StripeClient
162
        {
163
            get
164
            {
8✔
165
                if (stripeClient == null)
8✔
166
                {
4✔
167
                    stripeClient = BuildDefaultStripeClient();
4✔
168
                }
2✔
169

170
                return stripeClient;
6✔
171
            }
6✔
172

173
            set => stripeClient = value;
15✔
174
        }
175

176
        /// <summary>Gets the version of the Stripe.net client library.</summary>
177
        public static string StripeNetVersion { get; }
178

179
        /// <summary>
180
        /// Sets information about the "app" which this integration belongs to. This should be
181
        /// reserved for plugins that wish to identify themselves with Stripe.
182
        /// </summary>
183
        public static AppInfo AppInfo
184
        {
185
            internal get => appInfo;
2✔
186

187
            set
188
            {
×
189
                if ((value != null) && string.IsNullOrEmpty(value.Name))
×
190
                {
×
191
                    throw new ArgumentException("AppInfo.Name cannot be empty");
×
192
                }
193

194
                if (value != appInfo)
×
195
                {
×
196
                    StripeClient = null;
×
197
                }
×
198

199
                appInfo = value;
×
200
            }
×
201
        }
202

203
        /// <summary>
204
        /// Returns a new instance of <see cref="Newtonsoft.Json.JsonSerializerSettings"/> with
205
        /// the default settings used by Stripe.net.
206
        /// </summary>
207
        /// <returns>A <see cref="Newtonsoft.Json.JsonSerializerSettings"/> instance.</returns>
208
        public static JsonSerializerSettings DefaultSerializerSettings()
209
        {
1✔
210
            return DefaultSerializerSettings(null);
1✔
211
        }
1✔
212

213
        /// <summary>
214
        /// Returns a new instance of <see cref="Newtonsoft.Json.JsonSerializerSettings"/> with
215
        /// the default settings used by Stripe.net.
216
        /// </summary>
217
        /// <returns>A <see cref="Newtonsoft.Json.JsonSerializerSettings"/> instance.</returns>
218
        internal static JsonSerializerSettings DefaultSerializerSettings(ApiRequestor requestor)
219
        {
3,200✔
220
            return new JsonSerializerSettings
3,200✔
221
            {
3,200✔
222
                // Disable the warning (applies to net8.0).
3,200✔
223
#pragma warning disable SYSLIB0050
3,200✔
224
                Context = new StreamingContext(StreamingContextStates.All, new DeserializationContext { Requestor = requestor }),
3,200✔
225

3,200✔
226
                // Re-enable the warning.
3,200✔
227
#pragma warning restore SYSLIB0050
3,200✔
228
                Converters = new List<JsonConverter>
3,200✔
229
                {
3,200✔
230
                    new StripeObjectConverter(),
3,200✔
231
                },
3,200✔
232
                DateParseHandling = DateParseHandling.None,
3,200✔
233
                MaxDepth = 128,
3,200✔
234
            };
3,200✔
235
        }
3,200✔
236

237
        /// <summary>
238
        /// Sets the API key.
239
        /// This method is deprecated and will be removed in a future version, please use the
240
        /// <see cref="ApiKey"/> property setter instead.
241
        /// </summary>
242
        /// <param name="newApiKey">API key.</param>
243
        // TODO; remove this method in a future major version
244
        [Obsolete("Use StripeConfiguration.ApiKey setter instead.")]
245
        public static void SetApiKey(string newApiKey)
246
        {
×
247
            ApiKey = newApiKey;
×
248
        }
×
249

250
        /// <summary>Add beta version to ApiVersion.</summary>
251
        /// <param name="betaName">Name of beta.</param>
252
        /// <param name="betaVersion">Desired beta version.</param>
253
        public static void AddBetaVersion(string betaName, string betaVersion)
254
        {
2✔
255
            if (ApiVersion.Contains($"; {betaName}="))
2✔
256
            {
1✔
257
                throw new Exception($"Stripe version header {ApiVersion} already contains entry for beta {betaName}");
1✔
258
            }
259

260
            ApiVersion = $"{ApiVersion}; {betaName}={betaVersion}";
1✔
261
        }
1✔
262

263
        private static StripeClient BuildDefaultStripeClient()
264
        {
4✔
265
            if (ApiKey != null && ApiKey.Length == 0)
4✔
266
            {
1✔
267
                var message = "Your API key is invalid, as it is an empty string. You can "
1✔
268
                    + "double-check your API key from the Stripe Dashboard. See "
1✔
269
                    + "https://stripe.com/docs/api/authentication for details or contact support "
1✔
270
                    + "at https://support.stripe.com/email if you have any questions.";
1✔
271
                throw new StripeException(message);
1✔
272
            }
273

274
            if (ApiKey != null && StringUtils.ContainsWhitespace(ApiKey))
3✔
275
            {
1✔
276
                var message = "Your API key is invalid, as it contains whitespace. You can "
1✔
277
                    + "double-check your API key from the Stripe Dashboard. See "
1✔
278
                    + "https://stripe.com/docs/api/authentication for details or contact support "
1✔
279
                    + "at https://support.stripe.com/email if you have any questions.";
1✔
280
                throw new StripeException(message);
1✔
281
            }
282

283
            var httpClient = new SystemNetHttpClient(
2✔
284
                httpClient: null,
2✔
285
                maxNetworkRetries: MaxNetworkRetries,
2✔
286
                appInfo: AppInfo,
2✔
287
                enableTelemetry: EnableTelemetry);
2✔
288
            return new StripeClient(ApiKey, ClientId, httpClient: httpClient);
2✔
289
        }
2✔
290
    }
291
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc