summaryrefslogtreecommitdiffstats
path: root/SendGrid/SendGridMail/Transport/Web.cs
diff options
context:
space:
mode:
authorBrandon West <brawest@gmail.com>2014-10-09 14:36:25 -0600
committerBrandon West <brawest@gmail.com>2014-10-09 14:36:25 -0600
commit5155ba90dc9046039162ab97358dbffd4bf0c1bf (patch)
tree496726994f0b01b84c9ac3523db9657b8e714d23 /SendGrid/SendGridMail/Transport/Web.cs
parentd9a3ace4faefa9222c64db0f549285767209e8a6 (diff)
parent059f2cec5ef1e85183dab640e571786d90887573 (diff)
downloadsendgrid-csharp-5155ba90dc9046039162ab97358dbffd4bf0c1bf.zip
sendgrid-csharp-5155ba90dc9046039162ab97358dbffd4bf0c1bf.tar.gz
sendgrid-csharp-5155ba90dc9046039162ab97358dbffd4bf0c1bf.tar.bz2
merge
Diffstat (limited to 'SendGrid/SendGridMail/Transport/Web.cs')
-rw-r--r--SendGrid/SendGridMail/Transport/Web.cs50
1 files changed, 29 insertions, 21 deletions
diff --git a/SendGrid/SendGridMail/Transport/Web.cs b/SendGrid/SendGridMail/Transport/Web.cs
index f5ea706..9784097 100644
--- a/SendGrid/SendGridMail/Transport/Web.cs
+++ b/SendGrid/SendGridMail/Transport/Web.cs
@@ -45,7 +45,7 @@ namespace SendGrid
BaseAddress = new Uri("https://" + BaseUrl)
};
- client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "sendgrid/4.0.1;csharp");
+ client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "sendgrid/4.0.1;csharp");
var content = new MultipartFormDataContent();
AttachFormParams(message, content);
@@ -58,19 +58,19 @@ namespace SendGrid
/// Asynchronously delivers a message over SendGrid's Web interface
/// </summary>
/// <param name="message"></param>
- public async Task DeliverAsync(ISendGrid message)
- {
- var client = new HttpClient
- {
- BaseAddress = new Uri("https://" + BaseUrl)
- };
-
- var content = new MultipartFormDataContent();
- AttachFormParams(message, content);
- AttachFiles(message, content);
- var response = await client.PostAsync(Endpoint + ".xml", content);
- await CheckForErrorsAsync(response);
- }
+ public async Task DeliverAsync(ISendGrid message)
+ {
+ var client = new HttpClient
+ {
+ BaseAddress = new Uri("https://" + BaseUrl)
+ };
+
+ var content = new MultipartFormDataContent();
+ AttachFormParams(message, content);
+ AttachFiles(message, content);
+ var response = await client.PostAsync(Endpoint + ".xml", content);
+ await CheckForErrorsAsync(response);
+ }
#region Support Methods
@@ -120,15 +120,16 @@ namespace SendGrid
private static void CheckForErrors(HttpResponseMessage response)
{
- //transport error
- if (response.StatusCode != HttpStatusCode.OK)
- {
- throw new Exception(response.ReasonPhrase);
- }
-
var content = response.Content.ReadAsStreamAsync().Result;
+ var errors = GetErrorsInResponse(content);
- FindErrorsInResponse(content);
+ // API error
+ if (errors.Any())
+ throw new InvalidApiRequestException(response.StatusCode, errors, response.ReasonPhrase);
+
+ // Other error
+ if (response.StatusCode != HttpStatusCode.OK)
+ FindErrorsInResponse(content);
}
private static void FindErrorsInResponse(Stream content)
@@ -155,6 +156,13 @@ namespace SendGrid
}
}
+ private static string[] GetErrorsInResponse(Stream content)
+ {
+ var xmlDoc = new XmlDocument();
+ xmlDoc.Load(content);
+ return (from XmlNode errorNode in xmlDoc.SelectNodes("//error") select errorNode.InnerText).ToArray();
+ }
+
private static async Task CheckForErrorsAsync(HttpResponseMessage response)
{
//transport error