summaryrefslogtreecommitdiffstats
path: root/SendGrid/SendGridMail/Transport/Web.cs
diff options
context:
space:
mode:
Diffstat (limited to 'SendGrid/SendGridMail/Transport/Web.cs')
-rw-r--r--SendGrid/SendGridMail/Transport/Web.cs55
1 files changed, 13 insertions, 42 deletions
diff --git a/SendGrid/SendGridMail/Transport/Web.cs b/SendGrid/SendGridMail/Transport/Web.cs
index a125833..81e7c72 100644
--- a/SendGrid/SendGridMail/Transport/Web.cs
+++ b/SendGrid/SendGridMail/Transport/Web.cs
@@ -19,11 +19,10 @@ namespace SendGrid
#region Properties
//TODO: Make this configurable
- public const String BaseUrl = "api.sendgrid.com";
- public const String Endpoint = "/api/mail.send";
-
+ public const String Endpoint = "https://api.sendgrid.com/api/mail.send.xml";
+
private readonly NetworkCredential _credentials;
- private readonly TimeSpan _timeout;
+ private readonly HttpClient _client;
#endregion
@@ -31,11 +30,8 @@ namespace SendGrid
/// Creates a new Web interface for sending mail
/// </summary>
/// <param name="credentials">SendGridMessage user parameters</param>
- public Web(NetworkCredential credentials)
- {
- _credentials = credentials;
- _timeout = TimeSpan.FromSeconds(100);
- }
+ public Web(NetworkCredential credentials)
+ : this(credentials, TimeSpan.FromSeconds(100)) { }
/// <summary>
/// Creates a new Web interface for sending mail.
@@ -44,30 +40,13 @@ namespace SendGrid
/// <param name="httpTimeout">HTTP request timeout</param>
public Web(NetworkCredential credentials, TimeSpan httpTimeout)
{
- _credentials = credentials;
- _timeout = httpTimeout;
- }
-
- /// <summary>
- /// Delivers a message over SendGrid's Web interface
- /// </summary>
- /// <param name="message"></param>
- public void Deliver(ISendGrid message)
- {
- var client = new HttpClient();
-
- client.BaseAddress = new Uri("https://" + BaseUrl);
- client.Timeout = _timeout;
-
+ _credentials = credentials;
+ _client = new HttpClient();
+
var version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
- client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "sendgrid/" + version + ";csharp");
-
- var content = new MultipartFormDataContent();
- AttachFormParams(message, content);
- AttachFiles(message, content);
- var response = client.PostAsync(Endpoint + ".xml", content).Result;
- ErrorChecker.CheckForErrors(response);
- }
+ _client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "sendgrid/" + version + ";csharp");
+ _client.Timeout = httpTimeout;
+ }
/// <summary>
/// Asynchronously delivers a message over SendGrid's Web interface
@@ -75,19 +54,11 @@ namespace SendGrid
/// <param name="message"></param>
public async Task DeliverAsync(ISendGrid message)
{
- var client = new HttpClient();
-
- client.BaseAddress = new Uri("https://" + BaseUrl);
- client.Timeout = _timeout;
-
- var version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
- client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "sendgrid/" + version + ";csharp");
-
var content = new MultipartFormDataContent();
AttachFormParams(message, content);
AttachFiles(message, content);
- var response = await client.PostAsync(Endpoint + ".xml", content);
- await ErrorChecker.CheckForErrorsAsync(response);
+ var response = await _client.PostAsync(Endpoint + ".xml", content);
+ await ErrorChecker.CheckForErrorsAsync(response);
}
#region Support Methods