Skip to content

Commit 3e4877a

Browse files
authored
Merge pull request #1493 from stripe/ob-runtime-information
Refactor Client class
2 parents 9ec4355 + f1b00d0 commit 3e4877a

File tree

5 files changed

+98
-122
lines changed

5 files changed

+98
-122
lines changed

src/Stripe.net/Infrastructure/Client.cs

-84
This file was deleted.

src/Stripe.net/Infrastructure/Requestor.cs

+32-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace Stripe.Infrastructure
22
{
33
using System;
4+
using System.Collections.Generic;
45
using System.Globalization;
56
using System.IO;
67
using System.Linq;
@@ -10,9 +11,16 @@ namespace Stripe.Infrastructure
1011
using System.Text;
1112
using System.Threading;
1213
using System.Threading.Tasks;
14+
using Newtonsoft.Json;
1315

1416
internal static class Requestor
1517
{
18+
private static readonly string UserAgentString
19+
= $"Stripe/v1 .NetBindings/{StripeConfiguration.StripeNetVersion}";
20+
21+
private static readonly string StripeClientUserAgentString
22+
= BuildStripeClientUserAgentString();
23+
1624
static Requestor()
1725
{
1826
HttpClient =
@@ -148,9 +156,8 @@ internal static HttpRequestMessage GetRequestMessage(string url, HttpMethod meth
148156
request.Headers.Add("Stripe-Version", StripeConfiguration.ApiVersion);
149157
}
150158

151-
var client = new Client(request);
152-
client.ApplyUserAgent();
153-
client.ApplyClientData();
159+
request.Headers.UserAgent.ParseAdd(UserAgentString);
160+
request.Headers.Add("X-Stripe-Client-User-Agent", StripeClientUserAgentString);
154161

155162
return request;
156163
}
@@ -247,5 +254,27 @@ private static StripeResponse BuildResponseData(HttpResponseMessage response, st
247254

248255
return result;
249256
}
257+
258+
private static string BuildStripeClientUserAgentString()
259+
{
260+
var values = new Dictionary<string, string>
261+
{
262+
{ "bindings_version", StripeConfiguration.StripeNetVersion },
263+
{ "lang", ".net" },
264+
{ "publisher", "stripe" },
265+
{ "lang_version", RuntimeInformation.GetLanguageVersion() },
266+
{ "os_version", RuntimeInformation.GetOSVersion() },
267+
};
268+
269+
#if NET45
270+
string monoVersion = RuntimeInformation.GetMonoVersion();
271+
if (!string.IsNullOrEmpty(monoVersion))
272+
{
273+
values.Add("mono_version", monoVersion);
274+
}
275+
#endif
276+
277+
return JsonConvert.SerializeObject(values, Formatting.None);
278+
}
250279
}
251280
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
namespace Stripe.Infrastructure
2+
{
3+
#if NET45
4+
using System;
5+
using System.Reflection;
6+
using Microsoft.Win32;
7+
#endif
8+
9+
internal static class RuntimeInformation
10+
{
11+
public static string GetLanguageVersion()
12+
{
13+
#if NET45
14+
return ".NET Framework 4.5+";
15+
#else
16+
return System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription;
17+
#endif
18+
}
19+
20+
public static string GetOSVersion()
21+
{
22+
#if NET45
23+
return Environment.OSVersion.ToString();
24+
#else
25+
return System.Runtime.InteropServices.RuntimeInformation.OSDescription;
26+
#endif
27+
}
28+
29+
#if NET45
30+
public static string GetMonoVersion()
31+
{
32+
Type monoRuntimeType = typeof(object).Assembly.GetType("Mono.Runtime");
33+
34+
if (monoRuntimeType != null)
35+
{
36+
MethodInfo getDisplayNameMethod = monoRuntimeType.GetMethod(
37+
"GetDisplayName",
38+
BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.ExactBinding,
39+
null,
40+
Type.EmptyTypes,
41+
null);
42+
43+
if (getDisplayNameMethod != null)
44+
{
45+
return (string)getDisplayNameMethod.Invoke(null, null);
46+
}
47+
}
48+
49+
return null;
50+
}
51+
#endif
52+
}
53+
}

src/StripeTests/Infrastructure/ClientTest.cs

-35
This file was deleted.

src/StripeTests/Infrastructure/RequestorTest.cs

+13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ public void SetsHeaders()
2424
Assert.Equal(options.IdempotencyKey, request.Headers.GetValues("Idempotency-Key").FirstOrDefault());
2525
Assert.Equal(options.StripeConnectAccountId, request.Headers.GetValues("Stripe-Account").FirstOrDefault());
2626
Assert.Equal(StripeConfiguration.ApiVersion, request.Headers.GetValues("Stripe-Version").FirstOrDefault());
27+
28+
Assert.Equal(
29+
$"Stripe/v1 .NetBindings/{StripeConfiguration.StripeNetVersion}",
30+
request.Headers.UserAgent.ToString());
31+
32+
var json = request.Headers.GetValues("X-Stripe-Client-User-Agent").FirstOrDefault();
33+
Assert.NotNull(json);
34+
var data = JObject.Parse(json);
35+
Assert.Equal(StripeConfiguration.StripeNetVersion, data["bindings_version"]);
36+
Assert.Equal(".net", data["lang"]);
37+
Assert.Equal("stripe", data["publisher"]);
38+
Assert.NotNull(data["lang_version"]);
39+
Assert.NotNull(data["os_version"]);
2740
}
2841
}
2942
}

0 commit comments

Comments
 (0)