diff options
-rw-r--r-- | SendGrid/SendGridMail/Transport/Web.cs | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/SendGrid/SendGridMail/Transport/Web.cs b/SendGrid/SendGridMail/Transport/Web.cs index 69fed4c..7abf104 100644 --- a/SendGrid/SendGridMail/Transport/Web.cs +++ b/SendGrid/SendGridMail/Transport/Web.cs @@ -23,29 +23,41 @@ namespace SendGrid public const String Endpoint = "/api/mail.send";
private readonly NetworkCredential _credentials;
+ private readonly TimeSpan _timeout;
#endregion
/// <summary>
- /// Creates a new Web interface for sending mail. Preference is using the Factory method.
+ /// Creates a new Web interface for sending mail
/// </summary>
/// <param name="credentials">SendGridMessage user parameters</param>
- /// <param name="https">Use https?</param>
public Web(NetworkCredential credentials)
{
_credentials = credentials;
+ _timeout = TimeSpan.FromSeconds(100);
}
+ /// <summary>
+ /// Creates a new Web interface for sending mail.
+ /// </summary>
+ /// <param name="credentials">SendGridMessage user parameters</param>
+ /// <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
- {
- BaseAddress = new Uri("https://" + BaseUrl)
- };
+ 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");
@@ -63,10 +75,10 @@ namespace SendGrid /// <param name="message"></param>
public async Task DeliverAsync(ISendGrid message)
{
- var client = new HttpClient
- {
- BaseAddress = new Uri("https://" + BaseUrl)
- };
+ 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");
@@ -78,7 +90,7 @@ namespace SendGrid await CheckForErrorsAsync(response);
}
- #region Support Methods
+ #region Support Methods
private void AttachFormParams(ISendGrid message, MultipartFormDataContent content)
{
@@ -171,12 +183,6 @@ namespace SendGrid private static async Task CheckForErrorsAsync(HttpResponseMessage response)
{
- //transport error
- if (response.StatusCode != HttpStatusCode.OK)
- {
- throw new Exception(response.ReasonPhrase);
- }
-
var content = await response.Content.ReadAsStreamAsync();
var errors = GetErrorsInResponse(content);
|