Skip to content

Commit f098f33

Browse files
Merge pull request #149 from darrencauthon/useragent
User agent
2 parents 948c733 + c24887b commit f098f33

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

src/SparkPost.Tests/ClientTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Net.Http;
3+
using AutoMoq.Helpers;
34
using NUnit.Framework;
45
using Should;
56

@@ -61,5 +62,29 @@ public void It_should_set_any_subaccount_id_passed_to_it()
6162
.SubaccountId.ShouldEqual(1234);
6263
}
6364
}
65+
66+
[TestFixture]
67+
public class UserAgentTests : AutoMoqTestFixture<Client.Settings>
68+
{
69+
[SetUp]
70+
public void Setup()
71+
{
72+
ResetSubject();
73+
}
74+
75+
[Test]
76+
public void It_should_default_to_the_library_version()
77+
{
78+
Subject.UserAgent.ShouldEqual($"csharp-sparkpost/1.13.1");
79+
}
80+
81+
[Test]
82+
public void It_should_allow_the_user_agent_to_be_changed()
83+
{
84+
var userAgent = Guid.NewGuid().ToString();
85+
Subject.UserAgent = userAgent;
86+
Subject.UserAgent.ShouldEqual(userAgent);
87+
}
88+
}
6489
}
6590
}

src/SparkPost/Client.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24
using System.Net.Http;
35
using SparkPost.RequestSenders;
6+
using SparkPost.Utilities;
47

58
namespace SparkPost
69
{
@@ -79,9 +82,14 @@ public Settings()
7982
httpClient.DefaultRequestHeaders.Accept.Clear();
8083
return httpClient;
8184
};
85+
86+
var currentVersion = GetTheCurrentVersion();
87+
if (string.IsNullOrEmpty(currentVersion) == false)
88+
UserAgent = $"csharp-sparkpost/{currentVersion}";
8289
}
8390

8491
public SendingModes SendingMode { get; set; }
92+
public string UserAgent { get; set; }
8593

8694
public HttpClient CreateANewHttpClient()
8795
{
@@ -92,7 +100,26 @@ public void BuildHttpClientsUsing(Func<HttpClient> httpClient)
92100
{
93101
httpClientBuilder = httpClient;
94102
}
95-
}
96103

104+
private static string GetTheCurrentVersion()
105+
{
106+
try
107+
{
108+
return AttemptToPullTheVersionNumberOutOf(typeof(Client).AssemblyQualifiedName);
109+
}
110+
catch
111+
{
112+
return null;
113+
}
114+
}
115+
116+
private static string AttemptToPullTheVersionNumberOutOf(string value)
117+
{
118+
return value.SplitOn("Version=")[1]
119+
.SplitOn(",")[0]
120+
.SplitOn(".").Take(3)
121+
.JoinWith(".");
122+
}
123+
}
97124
}
98125
}

src/SparkPost/RequestSenders/AsyncRequestSender.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public virtual async Task<Response> Send(Request request)
2323
httpClient.BaseAddress = new Uri(client.ApiHost);
2424
httpClient.DefaultRequestHeaders.Add("Authorization", client.ApiKey);
2525

26+
SetTheUserAgentIfItIsProvided(httpClient);
27+
2628
if (client.SubaccountId != 0)
2729
httpClient.DefaultRequestHeaders.Add("X-MSYS-SUBACCOUNT",
2830
client.SubaccountId.ToString(CultureInfo.InvariantCulture));
@@ -38,6 +40,12 @@ public virtual async Task<Response> Send(Request request)
3840
}
3941
}
4042

43+
private void SetTheUserAgentIfItIsProvided(HttpClient httpClient)
44+
{
45+
if (string.IsNullOrEmpty(client.CustomSettings.UserAgent) == false)
46+
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", client.CustomSettings.UserAgent);
47+
}
48+
4149
protected virtual async Task<HttpResponseMessage> GetTheResponse(Request request, HttpClient httpClient)
4250
{
4351
return await new RequestMethodFinder(httpClient, dataMapper)

src/SparkPost/SparkPost.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
<Compile Include="Utilities\Jsonification.cs" />
9999
<Compile Include="Utilities\MailMessageMapping.cs" />
100100
<Compile Include="Utilities\SnakeCase.cs" />
101+
<Compile Include="Utilities\StringHelper.cs" />
101102
<Compile Include="ValueMappers\AnonymousValueMapper.cs" />
102103
<Compile Include="ValueMappers\BooleanValueMapper.cs" />
103104
<Compile Include="RequestSenders\AsyncRequestSender.cs" />
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace SparkPost.Utilities
5+
{
6+
public static class StringHelper
7+
{
8+
public static string[] SplitOn(this string value, string separator)
9+
{
10+
return value.Split(new[] {separator}, StringSplitOptions.RemoveEmptyEntries);
11+
}
12+
13+
public static string JoinWith(this IEnumerable<string> value, string separator)
14+
{
15+
return string.Join(separator, value);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)