-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/globalsuppression' into develop
- Loading branch information
Showing
11 changed files
with
217 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using Newtonsoft.Json.Linq; | ||
using StrongGrid.Utilities; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace StrongGrid.Resources | ||
{ | ||
public class GlobalSuppressions | ||
{ | ||
private readonly string _endpoint; | ||
private readonly IClient _client; | ||
|
||
/// <summary> | ||
/// Constructs the SendGrid Global Suppressions object. | ||
/// See https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/global_suppressions.html | ||
/// </summary> | ||
/// <param name="client">SendGrid Web API v3 client</param> | ||
/// <param name="endpoint">Resource endpoint, do not prepend slash</param> | ||
public GlobalSuppressions(IClient client, string endpoint = "/asm/suppressions/global") | ||
{ | ||
_endpoint = endpoint; | ||
_client = client; | ||
} | ||
|
||
/// <summary> | ||
/// Check if a recipient address is in the global suppressions group. | ||
/// </summary> | ||
/// <param name="email">email address to check</param> | ||
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/global_suppressions.html</returns> | ||
public async Task<bool> IsUnsubscribedAsync(string email, CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
var response = await _client.GetAsync(string.Format("{0}/{1}", _endpoint, email), cancellationToken).ConfigureAwait(false); | ||
response.EnsureSuccess(); | ||
|
||
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false); | ||
|
||
// If the email address is on the global suppression list, the response will look like this: | ||
// { | ||
// "recipient_email": "{email}" | ||
// } | ||
// If the email address is not on the global suppression list, the response will be empty | ||
// | ||
// Therefore, we check for the presence of the 'recipient_email' to indicate if the email | ||
// address is on the global suppression list or not. | ||
var dynamicObject = JObject.Parse(responseContent); | ||
var propertyDictionary = (IDictionary<string, JToken>)dynamicObject; | ||
return propertyDictionary.ContainsKey("recipient_email"); | ||
} | ||
|
||
/// <summary> | ||
/// Add recipient addresses to the global suppression group. | ||
/// </summary> | ||
/// <param name="emails">Array of email addresses to add to the suppression group</param> | ||
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/global_suppressions.html</returns> | ||
public async Task AddAsync(IEnumerable<string> emails, CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
var data = new JObject(new JProperty("recipient_emails", JArray.FromObject(emails.ToArray()))); | ||
var response = await _client.PostAsync(_endpoint, data, cancellationToken).ConfigureAwait(false); | ||
response.EnsureSuccess(); | ||
} | ||
|
||
/// <summary> | ||
/// Delete a recipient email from the global suppressions group. | ||
/// </summary> | ||
/// <param name="email">email address to be removed from the global suppressions group</param> | ||
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/global_suppressions.html</returns> | ||
public async Task RemoveAsync(string email, CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
var response = await _client.DeleteAsync(string.Format("{0}/{1}", _endpoint, email), cancellationToken).ConfigureAwait(false); | ||
response.EnsureSuccess(); | ||
} | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
Source/StrongGrid.UnitTests/Resources/GlobalSuppressionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Moq; | ||
using Newtonsoft.Json.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading; | ||
|
||
namespace StrongGrid.Resources.UnitTests | ||
{ | ||
[TestClass] | ||
public class GlobalSuppressionTests | ||
{ | ||
private const string ENDPOINT = "/asm/suppressions/global"; | ||
|
||
[TestMethod] | ||
public void Add() | ||
{ | ||
// Arrange | ||
var emails = new[] { "test1@example.com", "test2@example.com" }; | ||
var apiResponse = @"{ | ||
'recipient_emails': [ | ||
'test1@example.com', | ||
'test2@example.com' | ||
] | ||
}"; | ||
|
||
var mockClient = new Mock<IClient>(MockBehavior.Strict); | ||
mockClient.Setup(c => c.PostAsync(ENDPOINT, It.Is<JObject>(o => o["recipient_emails"].ToObject<JArray>().Count == emails.Length), It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(apiResponse) }); | ||
|
||
var globalSuppressions = new GlobalSuppressions(mockClient.Object); | ||
|
||
// Act | ||
globalSuppressions.AddAsync(emails, CancellationToken.None).Wait(); | ||
|
||
// Assert | ||
} | ||
|
||
[TestMethod] | ||
public void Delete() | ||
{ | ||
// Arrange | ||
var email = "test1@example.com"; | ||
|
||
var mockClient = new Mock<IClient>(MockBehavior.Strict); | ||
mockClient.Setup(c => c.DeleteAsync(ENDPOINT + "/" + email, It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.NoContent)); | ||
|
||
var globalSuppressions = new GlobalSuppressions(mockClient.Object); | ||
|
||
// Act | ||
globalSuppressions.RemoveAsync(email, CancellationToken.None).Wait(); | ||
|
||
// Assert | ||
} | ||
|
||
[TestMethod] | ||
public void IsUnsubscribed_true() | ||
{ | ||
// Arrange | ||
var email = "test1@example.com"; | ||
|
||
var apiResponse = @"{ | ||
'recipient_email': 'test1@example.com' | ||
}"; | ||
var mockClient = new Mock<IClient>(MockBehavior.Strict); | ||
mockClient.Setup(c => c.GetAsync(ENDPOINT + "/" + email, It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(apiResponse) }); | ||
|
||
var globalSuppressions = new GlobalSuppressions(mockClient.Object); | ||
|
||
// Act | ||
var result = globalSuppressions.IsUnsubscribedAsync(email, CancellationToken.None).Result; | ||
|
||
// Assert | ||
Assert.IsTrue(result); | ||
} | ||
|
||
[TestMethod] | ||
public void IsUnsubscribed_false() | ||
{ | ||
// Arrange | ||
var email = "test1@example.com"; | ||
|
||
var apiResponse = @"{ | ||
}"; | ||
var mockClient = new Mock<IClient>(MockBehavior.Strict); | ||
mockClient.Setup(c => c.GetAsync(ENDPOINT + "/" + email, It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(apiResponse) }); | ||
|
||
var globalSuppressions = new GlobalSuppressions(mockClient.Object); | ||
|
||
// Act | ||
var result = globalSuppressions.IsUnsubscribedAsync(email, CancellationToken.None).Result; | ||
|
||
// Assert | ||
Assert.IsFalse(result); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters