File tree Expand file tree Collapse file tree 5 files changed +80
-1
lines changed Expand file tree Collapse file tree 5 files changed +80
-1
lines changed Original file line number Diff line number Diff line change 1
1
using System ;
2
2
using System . Net . Http ;
3
+ using AutoMoq . Helpers ;
3
4
using NUnit . Framework ;
4
5
using Should ;
5
6
@@ -61,5 +62,29 @@ public void It_should_set_any_subaccount_id_passed_to_it()
61
62
. SubaccountId . ShouldEqual ( 1234 ) ;
62
63
}
63
64
}
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
+ }
64
89
}
65
90
}
Original file line number Diff line number Diff line change 1
1
using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
2
4
using System . Net . Http ;
3
5
using SparkPost . RequestSenders ;
6
+ using SparkPost . Utilities ;
4
7
5
8
namespace SparkPost
6
9
{
@@ -79,9 +82,14 @@ public Settings()
79
82
httpClient . DefaultRequestHeaders . Accept . Clear ( ) ;
80
83
return httpClient ;
81
84
} ;
85
+
86
+ var currentVersion = GetTheCurrentVersion ( ) ;
87
+ if ( string . IsNullOrEmpty ( currentVersion ) == false )
88
+ UserAgent = $ "csharp-sparkpost/{ currentVersion } ";
82
89
}
83
90
84
91
public SendingModes SendingMode { get ; set ; }
92
+ public string UserAgent { get ; set ; }
85
93
86
94
public HttpClient CreateANewHttpClient ( )
87
95
{
@@ -92,7 +100,26 @@ public void BuildHttpClientsUsing(Func<HttpClient> httpClient)
92
100
{
93
101
httpClientBuilder = httpClient ;
94
102
}
95
- }
96
103
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
+ }
97
124
}
98
125
}
Original file line number Diff line number Diff line change @@ -23,6 +23,8 @@ public virtual async Task<Response> Send(Request request)
23
23
httpClient . BaseAddress = new Uri ( client . ApiHost ) ;
24
24
httpClient . DefaultRequestHeaders . Add ( "Authorization" , client . ApiKey ) ;
25
25
26
+ SetTheUserAgentIfItIsProvided ( httpClient ) ;
27
+
26
28
if ( client . SubaccountId != 0 )
27
29
httpClient . DefaultRequestHeaders . Add ( "X-MSYS-SUBACCOUNT" ,
28
30
client . SubaccountId . ToString ( CultureInfo . InvariantCulture ) ) ;
@@ -38,6 +40,12 @@ public virtual async Task<Response> Send(Request request)
38
40
}
39
41
}
40
42
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
+
41
49
protected virtual async Task < HttpResponseMessage > GetTheResponse ( Request request , HttpClient httpClient )
42
50
{
43
51
return await new RequestMethodFinder ( httpClient , dataMapper )
Original file line number Diff line number Diff line change 98
98
<Compile Include =" Utilities\Jsonification.cs" />
99
99
<Compile Include =" Utilities\MailMessageMapping.cs" />
100
100
<Compile Include =" Utilities\SnakeCase.cs" />
101
+ <Compile Include =" Utilities\StringHelper.cs" />
101
102
<Compile Include =" ValueMappers\AnonymousValueMapper.cs" />
102
103
<Compile Include =" ValueMappers\BooleanValueMapper.cs" />
103
104
<Compile Include =" RequestSenders\AsyncRequestSender.cs" />
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments