summaryrefslogtreecommitdiffstats
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
parentd9a3ace4faefa9222c64db0f549285767209e8a6 (diff)
parent059f2cec5ef1e85183dab640e571786d90887573 (diff)
downloadsendgrid-csharp-5155ba90dc9046039162ab97358dbffd4bf0c1bf.zip
sendgrid-csharp-5155ba90dc9046039162ab97358dbffd4bf0c1bf.tar.gz
sendgrid-csharp-5155ba90dc9046039162ab97358dbffd4bf0c1bf.tar.bz2
merge
-rw-r--r--SendGrid/SendGridMail/Exceptions/InvalidApiRequestException.cs19
-rw-r--r--SendGrid/SendGridMail/Mail.csproj8
-rw-r--r--SendGrid/SendGridMail/Transport/Web.cs50
-rw-r--r--SendGrid/SendGridMail/packages.config2
4 files changed, 55 insertions, 24 deletions
diff --git a/SendGrid/SendGridMail/Exceptions/InvalidApiRequestException.cs b/SendGrid/SendGridMail/Exceptions/InvalidApiRequestException.cs
new file mode 100644
index 0000000..7fd51ea
--- /dev/null
+++ b/SendGrid/SendGridMail/Exceptions/InvalidApiRequestException.cs
@@ -0,0 +1,19 @@
+using System;
+using System.Net;
+
+namespace Exceptions
+{
+ public class InvalidApiRequestException : Exception
+ {
+ public InvalidApiRequestException(HttpStatusCode httpStatusCode, string[] errors, string httpResponsePhrase)
+ : base(httpResponsePhrase + " Check `Errors` for a list of errors returned by the API.")
+ {
+ ResponseStatusCode = httpStatusCode;
+ Errors = errors;
+ }
+
+ public String[] Errors { get; set; }
+
+ public HttpStatusCode ResponseStatusCode { get; private set; }
+ }
+}
diff --git a/SendGrid/SendGridMail/Mail.csproj b/SendGrid/SendGridMail/Mail.csproj
index 19ec745..9dcb4ad 100644
--- a/SendGrid/SendGridMail/Mail.csproj
+++ b/SendGrid/SendGridMail/Mail.csproj
@@ -98,6 +98,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="Exceptions\InvalidApiRequestException.cs" />
<Compile Include="ISendGrid.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SendGrid.cs" />
@@ -105,7 +106,9 @@
<Compile Include="Transport\Web.cs" />
</ItemGroup>
<ItemGroup>
- <None Include="packages.config" />
+ <None Include="packages.config">
+ <SubType>Designer</SubType>
+ </None>
<None Include="sendgrid-csharp.snk" />
</ItemGroup>
<ItemGroup>
@@ -130,6 +133,7 @@
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
+ <ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
@@ -139,4 +143,4 @@
<Target Name="AfterBuild">
</Target>
-->
-</Project>
+</Project> \ No newline at end of file
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
diff --git a/SendGrid/SendGridMail/packages.config b/SendGrid/SendGridMail/packages.config
index d05ec44..003f620 100644
--- a/SendGrid/SendGridMail/packages.config
+++ b/SendGrid/SendGridMail/packages.config
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="SendGrid.SmtpApi" version="1.1.3" targetFramework="net40" />
-</packages> \ No newline at end of file
+</packages>