1
1
using Newtonsoft . Json ;
2
2
using Newtonsoft . Json . Linq ;
3
3
using System ;
4
- using System . Collections . Generic ;
5
- using System . Linq ;
6
- using System . Net ;
7
4
using System . Net . Http ;
8
5
using System . Net . Http . Headers ;
9
6
using System . Text ;
@@ -15,6 +12,7 @@ public enum ApiVersion
15
12
{
16
13
V3 ,
17
14
V3_1 ,
15
+ V4 ,
18
16
}
19
17
20
18
/// <summary>
@@ -27,29 +25,26 @@ public class MailjetClient : IMailjetClient
27
25
private const string JsonMediaType = "application/json" ;
28
26
private const string ApiVersionPathV3 = "v3" ;
29
27
private const string ApiVersionPathV3_1 = "v3.1" ;
28
+ private const string ApiVersionPathV4 = "v4" ;
30
29
31
- private readonly HttpClient _httpClient ;
30
+ private HttpClient _httpClient ;
32
31
33
32
public MailjetClient ( string apiKey , string apiSecret , HttpMessageHandler httpMessageHandler = null )
34
33
{
35
- // Create HttpClient
36
- _httpClient = ( httpMessageHandler != null ) ? new HttpClient ( httpMessageHandler ) : new HttpClient ( ) ;
37
-
38
- // Set base URI
39
- _httpClient . BaseAddress = new Uri ( DefaultBaseAdress ) ;
40
-
41
- // Set accepted media type
42
- _httpClient . DefaultRequestHeaders . Accept . Clear ( ) ;
43
- _httpClient . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( JsonMediaType ) ) ;
44
-
45
- // Set user-agent
46
- _httpClient . DefaultRequestHeaders . UserAgent . ParseAdd ( UserAgent ) ;
34
+ InitHttpClient ( httpMessageHandler ) ;
47
35
48
36
// Set basic authentification
49
37
var byteArray = Encoding . UTF8 . GetBytes ( string . Format ( "{0}:{1}" , apiKey , apiSecret ) ) ;
50
38
_httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Basic" , Convert . ToBase64String ( byteArray ) ) ;
51
39
}
52
40
41
+ public MailjetClient ( string token , HttpMessageHandler httpMessageHandler = null )
42
+ {
43
+ InitHttpClient ( httpMessageHandler ) ;
44
+
45
+ _httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , token ) ;
46
+ }
47
+
53
48
public ApiVersion Version { get ; set ; } = ApiVersion . V3 ;
54
49
55
50
public string BaseAdress
@@ -131,6 +126,22 @@ private async Task<JObject> GetContent(HttpResponseMessage responseMessage)
131
126
return content ;
132
127
}
133
128
129
+ private void InitHttpClient ( HttpMessageHandler httpMessageHandler )
130
+ {
131
+ // Create HttpClient
132
+ _httpClient = ( httpMessageHandler != null ) ? new HttpClient ( httpMessageHandler ) : new HttpClient ( ) ;
133
+
134
+ // Set base URI
135
+ _httpClient . BaseAddress = new Uri ( DefaultBaseAdress ) ;
136
+
137
+ // Set accepted media type
138
+ _httpClient . DefaultRequestHeaders . Accept . Clear ( ) ;
139
+ _httpClient . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( JsonMediaType ) ) ;
140
+
141
+ // Set user-agent
142
+ _httpClient . DefaultRequestHeaders . UserAgent . ParseAdd ( UserAgent ) ;
143
+ }
144
+
134
145
private string BuildUrl ( MailjetRequest request )
135
146
{
136
147
return UrlHelper . CombineUrl ( GetApiVersionPath ( ) , request . BuildUrl ( ) ) ;
@@ -140,8 +151,12 @@ private string GetApiVersionPath()
140
151
{
141
152
switch ( Version )
142
153
{
143
- case ApiVersion . V3_1 : return ApiVersionPathV3_1 ;
144
- default : return ApiVersionPathV3 ;
154
+ case ApiVersion . V3_1 :
155
+ return ApiVersionPathV3_1 ;
156
+ case ApiVersion . V4 :
157
+ return ApiVersionPathV4 ;
158
+ default :
159
+ return ApiVersionPathV3 ;
145
160
}
146
161
}
147
162
}
0 commit comments