From fc933a9f53d1afe25c39670dc707eb57f336d01d Mon Sep 17 00:00:00 2001 From: Brandon West Date: Wed, 22 Apr 2015 18:31:08 -0600 Subject: [PATCH] add api key based constructors and Authorization: Bearer token to HTTP request if api key present --- SendGrid/SendGridMail/Transport/Web.cs | 42 +++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/SendGrid/SendGridMail/Transport/Web.cs b/SendGrid/SendGridMail/Transport/Web.cs index 4d2f484a2..d3a6de1c4 100644 --- a/SendGrid/SendGridMail/Transport/Web.cs +++ b/SendGrid/SendGridMail/Transport/Web.cs @@ -23,27 +23,48 @@ public class Web : ITransport private readonly NetworkCredential _credentials; private readonly HttpClient _client; + private readonly string _apiKey; #endregion + /// + /// Creates a new Web interface for sending mail + /// + /// The API Key with which to send + public Web(string apiKey) + : this(apiKey, null, TimeSpan.FromSeconds(100)) { } + + /// Creates a new Web interface for sending mail + /// + /// The API Key with which to send + /// HTTP request timeout + public Web(string apiKey, TimeSpan httpTimeout) + : this(apiKey, null, httpTimeout) { } + /// /// Creates a new Web interface for sending mail /// /// SendGridMessage user parameters public Web(NetworkCredential credentials) - : this(credentials, TimeSpan.FromSeconds(100)) { } + : this(null, credentials, TimeSpan.FromSeconds(100)) { } /// /// Creates a new Web interface for sending mail. /// + /// The API Key with which to send /// SendGridMessage user parameters /// HTTP request timeout - public Web(NetworkCredential credentials, TimeSpan httpTimeout) + public Web(string apiKey, NetworkCredential credentials, TimeSpan httpTimeout) { _credentials = credentials; _client = new HttpClient(); - + _apiKey = apiKey; + var version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); + if (credentials == null) + { + _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _apiKey); + } _client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "sendgrid/" + version + ";csharp"); _client.Timeout = httpTimeout; } @@ -157,8 +178,7 @@ internal List> FetchFormParams(ISendGrid message) { var result = new List> { - new KeyValuePair("api_user", _credentials.UserName), - new KeyValuePair("api_key", _credentials.Password), + new KeyValuePair("headers", message.Headers.Count == 0 ? null : Utils.SerializeDictionary(message.Headers)), new KeyValuePair("replyto", @@ -170,6 +190,18 @@ internal List> FetchFormParams(ISendGrid message) new KeyValuePair("html", message.Html), new KeyValuePair("x-smtpapi", message.Header.JsonString() ?? "") }; + + //if the api key is not specified, use the username and password + if (_credentials != null) + { + var creds = new List> + { + new KeyValuePair("api_user", _credentials.UserName), + new KeyValuePair("api_key", _credentials.Password) + }; + result.AddRange(creds); + } + if (message.To != null) { result = result.Concat(message.To.ToList().Select(a => new KeyValuePair("to[]", a.Address)))