-
Notifications
You must be signed in to change notification settings - Fork 573
/
Copy pathClient.cs
84 lines (72 loc) · 2.45 KB
/
Client.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
namespace Stripe.Infrastructure
{
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Reflection;
using Newtonsoft.Json;
#if NET45
using Microsoft.Win32;
#else
using System.Runtime.InteropServices;
#endif
internal class Client
{
public Client(HttpRequestMessage requestMessage)
{
this.RequestMessage = requestMessage;
}
private HttpRequestMessage RequestMessage { get; set; }
public void ApplyUserAgent()
{
this.RequestMessage.Headers.UserAgent.ParseAdd($"Stripe/v1 .NetBindings/{StripeConfiguration.StripeNetVersion}");
}
public void ApplyClientData()
{
this.RequestMessage.Headers.Add("X-Stripe-Client-User-Agent", this.GetClientUserAgentString());
}
#if NET45
private static string GetMonoVersion()
{
Type monoRuntimeType = typeof(object).Assembly.GetType("Mono.Runtime");
if (monoRuntimeType != null)
{
MethodInfo getDisplayNameMethod = monoRuntimeType.GetMethod(
"GetDisplayName",
BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.ExactBinding,
null,
Type.EmptyTypes,
null);
if (getDisplayNameMethod != null)
{
return (string)getDisplayNameMethod.Invoke(null, null);
}
}
return null;
}
#endif
private string GetClientUserAgentString()
{
var values = new Dictionary<string, string>
{
{ "bindings_version", StripeConfiguration.StripeNetVersion },
{ "lang", ".net" },
{ "publisher", "stripe" },
};
#if NET45
values.Add("lang_version", ".NET Framework 4.5+");
values.Add("os_version", Environment.OSVersion.ToString());
string monoVersion = Client.GetMonoVersion();
if (monoVersion != null)
{
values.Add("mono_version", monoVersion);
}
#else
values.Add("lang_version", RuntimeInformation.FrameworkDescription);
values.Add("os_version", RuntimeInformation.OSDescription);
#endif
return JsonConvert.SerializeObject(values, Formatting.None);
}
}
}