diff options
-rw-r--r-- | SendGrid/Example/Example.csproj | 12 | ||||
-rw-r--r-- | SendGrid/SendGridMail/Exceptions/InvalidApiRequestException.cs | 19 | ||||
-rw-r--r-- | SendGrid/SendGridMail/Mail.csproj | 8 | ||||
-rw-r--r-- | SendGrid/SendGridMail/Properties/AssemblyInfo.cs | 4 | ||||
-rw-r--r-- | SendGrid/SendGridMail/Transport/Web.cs | 56 | ||||
-rw-r--r-- | SendGrid/SendGridMail/packages.config | 2 |
6 files changed, 69 insertions, 32 deletions
diff --git a/SendGrid/Example/Example.csproj b/SendGrid/Example/Example.csproj index 8152179..f604aaa 100644 --- a/SendGrid/Example/Example.csproj +++ b/SendGrid/Example/Example.csproj @@ -60,6 +60,12 @@ <ItemGroup>
<WCFMetadata Include="Service References\" />
</ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\SendGridMail\Mail.csproj">
+ <Project>{3c687bef-ff50-44ad-8315-2d4237281af8}</Project>
+ <Name>Mail</Name>
+ </ProjectReference>
+ </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.
@@ -69,10 +75,4 @@ <Target Name="AfterBuild">
</Target>
-->
- <ItemGroup>
- <ProjectReference Include="..\SendGridMail\Mail.csproj">
- <Project>{3C687BEF-FF50-44AD-8315-2D4237281AF8}</Project>
- <Name>Mail</Name>
- </ProjectReference>
- </ItemGroup>
</Project>
\ No newline at end of file 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/Properties/AssemblyInfo.cs b/SendGrid/SendGridMail/Properties/AssemblyInfo.cs index dbff93a..c2d2a11 100644 --- a/SendGrid/SendGridMail/Properties/AssemblyInfo.cs +++ b/SendGrid/SendGridMail/Properties/AssemblyInfo.cs @@ -48,5 +48,5 @@ using System.Runtime.InteropServices; // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("4.0.0")]
-[assembly: AssemblyFileVersion("4.0.0")]
\ No newline at end of file +[assembly: AssemblyVersion("4.1.0")]
+[assembly: AssemblyFileVersion("4.1.0")]
\ No newline at end of file diff --git a/SendGrid/SendGridMail/Transport/Web.cs b/SendGrid/SendGridMail/Transport/Web.cs index f5ea706..1458da8 100644 --- a/SendGrid/SendGridMail/Transport/Web.cs +++ b/SendGrid/SendGridMail/Transport/Web.cs @@ -5,8 +5,10 @@ using System.Linq; using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
+using System.Reflection;
using System.Threading.Tasks;
using System.Xml;
+using Exceptions;
using SendGrid.SmtpApi;
// ReSharper disable MemberCanBePrivate.Global
@@ -45,7 +47,8 @@ namespace SendGrid BaseAddress = new Uri("https://" + BaseUrl)
};
- client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "sendgrid/4.0.1;csharp");
+ var version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
+ client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "sendgrid/" + version + ";csharp");
var content = new MultipartFormDataContent();
AttachFormParams(message, content);
@@ -58,19 +61,22 @@ 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 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 CheckForErrorsAsync(response);
+ }
#region Support Methods
@@ -120,15 +126,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 +162,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>
|