-
Notifications
You must be signed in to change notification settings - Fork 584
/
Copy pathSendGridClient.cs
400 lines (355 loc) · 16.6 KB
/
SendGridClient.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// <copyright file="SendGridClient.cs" company="Twilio SendGrid">
// Copyright (c) Twilio SendGrid. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>
using Newtonsoft.Json;
using SendGrid.Helpers.Mail;
using SendGrid.Helpers.Reliability;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SendGrid
{
/// <summary>
/// A HTTP client wrapper for interacting with Twilio SendGrid's API.
/// </summary>
public class SendGridClient : ISendGridClient
{
private const string Scheme = "Bearer";
private const string ContentType = "Content-Type";
private const string DefaultMediaType = "application/json";
/// <summary>
/// The <see cref="SendGridClient"/> assembly version to send in request User-Agent header.
/// </summary>
private static readonly string ClientVersion = typeof(SendGridClient).GetTypeInfo().Assembly.GetName().Version.ToString();
/// <summary>
/// The default configuration settings to use with the SendGrid client.
/// </summary>
private static readonly SendGridClientOptions DefaultOptions = new SendGridClientOptions();
/// <summary>
/// The configuration to use with current <see cref="SendGridClient"/> instance.
/// </summary>
private readonly SendGridClientOptions options;
/// <summary>
/// The HttpClient instance to use for all calls from this SendGridClient instance.
/// </summary>
private readonly HttpClient client;
/// <summary>
/// Initializes a new instance of the <see cref="SendGridClient"/> class.
/// </summary>
/// <param name="webProxy">Web proxy.</param>
/// <param name="apiKey">Your Twilio SendGrid API key.</param>
/// <param name="host">Base url (e.g. https://api.sendgrid.com).</param>
/// <param name="requestHeaders">A dictionary of request headers.</param>
/// <param name="version">API version, override AddVersion to customize.</param>
/// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint).</param>
/// <returns>Interface to the Twilio SendGrid REST API.</returns>
public SendGridClient(IWebProxy webProxy, string apiKey, string host = null, Dictionary<string, string> requestHeaders = null, string version = "v3", string urlPath = null)
: this(CreateHttpClientWithWebProxy(webProxy), new SendGridClientOptions { ApiKey = apiKey, Host = host, RequestHeaders = requestHeaders, Version = version, UrlPath = urlPath })
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SendGridClient"/> class.
/// </summary>
/// <param name="options">A <see cref="SendGridClientOptions"/> instance that defines the configuration settings to use with the client.</param>
/// <returns>Interface to the Twilio SendGrid REST API.</returns>
public SendGridClient(SendGridClientOptions options)
: this(null, options)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SendGridClient"/> class.
/// </summary>
/// <param name="httpClient">An optional http client which may me injected in order to facilitate testing.</param>
/// <param name="apiKey">Your Twilio SendGrid API key.</param>
/// <param name="host">Base url (e.g. https://api.sendgrid.com).</param>
/// <param name="requestHeaders">A dictionary of request headers.</param>
/// <param name="version">API version, override AddVersion to customize.</param>
/// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint).</param>
/// <returns>Interface to the Twilio SendGrid REST API.</returns>
public SendGridClient(HttpClient httpClient, string apiKey, string host = null, Dictionary<string, string> requestHeaders = null, string version = "v3", string urlPath = null)
: this(httpClient, new SendGridClientOptions { ApiKey = apiKey, Host = host, RequestHeaders = requestHeaders, Version = version, UrlPath = urlPath })
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SendGridClient"/> class.
/// </summary>
/// <param name="apiKey">Your Twilio SendGrid API key.</param>
/// <param name="host">Base url (e.g. https://api.sendgrid.com).</param>
/// <param name="requestHeaders">A dictionary of request headers.</param>
/// <param name="version">API version, override AddVersion to customize.</param>
/// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint).</param>
/// <returns>Interface to the Twilio SendGrid REST API.</returns>
public SendGridClient(string apiKey, string host = null, Dictionary<string, string> requestHeaders = null, string version = "v3", string urlPath = null)
: this(null, new SendGridClientOptions { ApiKey = apiKey, Host = host, RequestHeaders = requestHeaders, Version = version, UrlPath = urlPath })
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SendGridClient"/> class.
/// </summary>
/// <param name="httpClient">An optional HTTP client which may me injected in order to facilitate testing.</param>
/// <param name="options">A <see cref="SendGridClientOptions"/> instance that defines the configuration settings to use with the client.</param>
/// <returns>Interface to the Twilio SendGrid REST API.</returns>
public SendGridClient(HttpClient httpClient, SendGridClientOptions options)
{
this.options = options ?? throw new ArgumentNullException(nameof(options));
if (string.IsNullOrWhiteSpace(options.ApiKey))
{
throw new ArgumentNullException(nameof(options.ApiKey));
}
this.client = httpClient ?? CreateHttpClientWithRetryHandler();
if (this.options.RequestHeaders != null && this.options.RequestHeaders.TryGetValue(ContentType, out var contentType))
{
this.MediaType = contentType;
}
}
/// <summary>
/// The supported API methods.
/// </summary>
public enum Method
{
/// <summary>
/// Remove a resource.
/// </summary>
DELETE,
/// <summary>
/// Get a resource.
/// </summary>
GET,
/// <summary>
/// Modify a portion of the resource.
/// </summary>
PATCH,
/// <summary>
/// Create a resource or execute a function (e.g. send an email).
/// </summary>
POST,
/// <summary>
/// Update an entire resource.
/// </summary>
PUT,
}
/// <summary>
/// Gets or sets the path to the API resource.
/// </summary>
public string UrlPath
{
get => this.options.UrlPath;
set => this.options.UrlPath = value;
}
/// <summary>
/// Gets or sets the API version.
/// </summary>
public string Version
{
get => this.options.Version;
set => this.options.Version = value;
}
/// <summary>
/// Gets or sets the request media type.
/// </summary>
public string MediaType { get; set; } = DefaultMediaType;
/// <summary>
/// Add the authorization header, override to customize.
/// </summary>
/// <param name="header">Authorization header.</param>
/// <returns>Authorization value to add to the header.</returns>
public virtual AuthenticationHeaderValue AddAuthorization(KeyValuePair<string, string> header)
{
string[] split = header.Value.Split();
return new AuthenticationHeaderValue(split[0], split[1]);
}
/// <summary>
/// Make the call to the API server, override for testing or customization.
/// </summary>
/// <param name="request">The parameters for the API call.</param>
/// <param name="cancellationToken">Cancel the asynchronous call.</param>
/// <returns>Response object.</returns>
public virtual async Task<Response> MakeRequest(HttpRequestMessage request, CancellationToken cancellationToken = default(CancellationToken))
{
HttpResponseMessage response = await this.client.SendAsync(request, cancellationToken).ConfigureAwait(false);
return new Response(response.StatusCode, response.Content, response.Headers);
}
/// <summary>
/// Prepare for async call to the API server.
/// </summary>
/// <param name="method">HTTP verb.</param>
/// <param name="requestBody">JSON formatted string.</param>
/// <param name="queryParams">JSON formatted query parameters.</param>
/// <param name="urlPath">The path to the API endpoint.</param>
/// <param name="cancellationToken">Cancel the asynchronous call.</param>
/// <returns>Response object.</returns>
/// <exception cref="Exception">The method will NOT catch and swallow exceptions generated by sending a request through the internal HTTP client. Any underlying exception will pass right through.
/// In particular, this means that you may expect a TimeoutException if you are not connected to the Internet.</exception>
public async Task<Response> RequestAsync(
SendGridClient.Method method,
string requestBody = null,
string queryParams = null,
string urlPath = null,
CancellationToken cancellationToken = default(CancellationToken))
{
var baseAddress = new Uri(string.IsNullOrWhiteSpace(this.options.Host) ? DefaultOptions.Host : this.options.Host);
if (!baseAddress.OriginalString.EndsWith("/"))
{
baseAddress = new Uri(baseAddress.OriginalString + "/");
}
// Build the final request
var request = new HttpRequestMessage
{
Method = new HttpMethod(method.ToString()),
RequestUri = new Uri(baseAddress, this.BuildUrl(urlPath, queryParams)),
Content = requestBody == null ? null : new StringContent(requestBody, Encoding.UTF8, this.MediaType),
};
// Drop the default UTF-8 content type charset for JSON payloads since some APIs may not accept it.
if (request.Content != null && this.MediaType == DefaultMediaType)
{
request.Content.Headers.ContentType.CharSet = null;
}
// set header overrides
if (this.options.RequestHeaders?.Count > 0)
{
foreach (var header in this.options.RequestHeaders)
{
request.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
}
// set standard headers
request.Headers.Authorization = new AuthenticationHeaderValue(Scheme, this.options.ApiKey);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(this.MediaType));
request.Headers.UserAgent.TryParseAdd($"sendgrid/{ClientVersion} csharp");
return await this.MakeRequest(request, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Make a request to send an email through Twilio SendGrid asynchronously.
/// </summary>
/// <param name="msg">A SendGridMessage object with the details for the request.</param>
/// <param name="cancellationToken">Cancel the asynchronous call.</param>
/// <returns>A Response object.</returns>
public async Task<Response> SendEmailAsync(SendGridMessage msg, CancellationToken cancellationToken = default(CancellationToken))
{
return await this.RequestAsync(
Method.POST,
msg.Serialize(),
urlPath: "mail/send",
cancellationToken: cancellationToken).ConfigureAwait(false);
}
private static HttpClient CreateHttpClientWithRetryHandler()
{
return new HttpClient(new RetryDelegatingHandler(DefaultOptions.ReliabilitySettings));
}
/// <summary>
/// Create client with WebProxy if set.
/// </summary>
/// <param name="webProxy">the WebProxy.</param>
/// <returns>HttpClient with RetryDelegatingHandler and WebProxy if set.</returns>
private static HttpClient CreateHttpClientWithWebProxy(IWebProxy webProxy)
{
if (webProxy != null)
{
var httpClientHandler = new HttpClientHandler()
{
Proxy = webProxy,
PreAuthenticate = true,
UseDefaultCredentials = false,
};
var retryHandler = new RetryDelegatingHandler(httpClientHandler, DefaultOptions.ReliabilitySettings);
return new HttpClient(retryHandler);
}
else
{
return CreateHttpClientWithRetryHandler();
}
}
/// <summary>
/// Build the final URL.
/// </summary>
/// <param name="urlPath">The URL path.</param>
/// <param name="queryParams">A string of JSON formatted query parameters (e.g. {'param': 'param_value'}).</param>
/// <returns>
/// Final URL.
/// </returns>
private string BuildUrl(string urlPath, string queryParams = null)
{
string url = null;
// create urlPAth - from parameter if overridden on call or from constructor parameter
var urlpath = urlPath ?? this.options.UrlPath;
if (this.options.Version != null)
{
url = this.options.Version + "/" + urlpath;
}
else
{
url = urlpath;
}
if (queryParams != null)
{
var ds_query_params = this.ParseJson(queryParams);
string query = "?";
foreach (var pair in ds_query_params)
{
foreach (var element in pair.Value)
{
if (query != "?")
{
query += "&";
}
query = query + pair.Key + "=" + element;
}
}
url += query;
}
return url;
}
/// <summary>
/// Parses a JSON string without removing duplicate keys.
/// </summary>
/// <remarks>
/// This function flattens all Objects/Array.
/// This means that for example {'id': 1, 'id': 2, 'id': 3} and {'id': [1, 2, 3]} result in the same output.
/// </remarks>
/// <param name="json">The JSON string to parse.</param>
/// <returns>A dictionary of all values.</returns>
private Dictionary<string, List<object>> ParseJson(string json)
{
var dict = new Dictionary<string, List<object>>();
using (var sr = new StringReader(json))
using (var reader = new JsonTextReader(sr))
{
var propertyName = string.Empty;
while (reader.Read())
{
switch (reader.TokenType)
{
case JsonToken.PropertyName:
{
propertyName = reader.Value.ToString();
if (!dict.ContainsKey(propertyName))
{
dict.Add(propertyName, new List<object>());
}
break;
}
case JsonToken.Boolean:
case JsonToken.Integer:
case JsonToken.Float:
case JsonToken.Bytes:
case JsonToken.String:
case JsonToken.Date:
{
dict[propertyName].Add(reader.Value);
break;
}
}
}
}
return dict;
}
}
}