-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #169 from sendgrid/global_suppressions
Global Suppressions [GET, POST, DELETE]
- Loading branch information
Showing
9 changed files
with
230 additions
and
44 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -277,6 +277,42 @@ ver groupId = "<UNSUBSCRIBE GROUP ID>"; | |
HttpResponseMessage responseDelete1 = client.Suppressions.Delete(groupId, "[email protected]").Result; | ||
``` | ||
|
||
## Global Suppressions ## | ||
|
||
Please refer to [our documentation](https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/global_suppressions.html) for further details. | ||
|
||
Check if a recipient address is in the global suppressions group. [GET] | ||
|
||
```csharp | ||
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User); | ||
var client = new SendGrid.Client(apiKey); | ||
// Leave off .Result for an asyncronous call | ||
string email = "[email protected]"; | ||
HttpResponseMessage responseGet = client.GlobalSuppressions.Get(email).Result; | ||
``` | ||
|
||
Add recipient addresses to the global suppression group. [POST] | ||
|
||
If the group has been deleted, this request will add the address to the global suppression. | ||
|
||
```csharp | ||
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User); | ||
var client = new SendGrid.Client(apiKey); | ||
string[] emails = { "[email protected]", "[email protected]" }; | ||
// Leave off .Result for an asyncronous call | ||
HttpResponseMessage responsePost = client.GlobalSuppressions.Post(emails).Result; | ||
``` | ||
|
||
Delete a recipient email from the global suppressions group. [DELETE] | ||
|
||
```csharp | ||
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User); | ||
var client = new SendGrid.Client(apiKey); | ||
string email = "[email protected]"; | ||
// Leave off .Result for an asyncronous call | ||
HttpResponseMessage responseDelete1 = client.GlobalSuppressions.Delete(email).Result; | ||
``` | ||
|
||
#How to: Testing | ||
|
||
* Load the solution (We have tested using the Visual Studio Community Edition) | ||
|
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ private static void Main() | |
ApiKeys(); | ||
UnsubscribeGroups(); | ||
Suppressions(); | ||
GlobalSuppressions(); | ||
} | ||
|
||
private static void SendAsync(SendGrid.SendGridMessage message) | ||
|
@@ -95,7 +96,7 @@ private static void ApiKeys() | |
HttpResponseMessage responseFinal = client.ApiKeys.Get().Result; | ||
Console.WriteLine(responseFinal.StatusCode); | ||
Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result); | ||
Console.WriteLine("API Key Deleted, press any key to end"); | ||
Console.WriteLine("API Key Deleted, press any key to end."); | ||
Console.ReadKey(); | ||
} | ||
|
||
|
@@ -136,7 +137,7 @@ private static void UnsubscribeGroups() | |
HttpResponseMessage responseFinal = client.UnsubscribeGroups.Get().Result; | ||
Console.WriteLine(responseFinal.StatusCode); | ||
Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result); | ||
Console.WriteLine("Unsubscribe Group Deleted, press any key to end"); | ||
Console.WriteLine("Unsubscribe Group Deleted, press any key to end."); | ||
Console.ReadKey(); | ||
} | ||
|
||
|
@@ -172,7 +173,46 @@ private static void Suppressions() | |
HttpResponseMessage responseFinal = client.Suppressions.Get(groupID).Result; | ||
Console.WriteLine(responseFinal.StatusCode); | ||
Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result); | ||
Console.WriteLine("Emails removed from Suppression Group" + groupID.ToString() + "Deleted. Press any key to end"); | ||
Console.WriteLine("Emails removed from Suppression Group" + groupID.ToString() + "Deleted. Press any key to end."); | ||
Console.ReadKey(); | ||
} | ||
|
||
private static void GlobalSuppressions() | ||
{ | ||
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User); | ||
var client = new SendGrid.Client(apiKey); | ||
|
||
// GET SUPPRESSED ADDRESSES FOR A GIVEN GROUP | ||
var email = "[email protected]"; | ||
HttpResponseMessage responseGetUnique = client.GlobalSuppressions.Get(email).Result; | ||
Console.WriteLine(responseGetUnique.StatusCode); | ||
Console.WriteLine(responseGetUnique.Content.ReadAsStringAsync().Result); | ||
Console.WriteLine("Determines if the given email is listed on the Global Suppressions list. Press any key to continue."); | ||
Console.ReadKey(); | ||
|
||
// ADD EMAILS TO A SUPPRESSION GROUP | ||
string[] emails = { "[email protected]", "[email protected]" }; | ||
HttpResponseMessage responsePost = client.GlobalSuppressions.Post(emails).Result; | ||
var rawString = responsePost.Content.ReadAsStringAsync().Result; | ||
dynamic jsonObject = JObject.Parse(rawString); | ||
Console.WriteLine(responsePost.StatusCode); | ||
Console.WriteLine(responsePost.Content.ReadAsStringAsync().Result); | ||
Console.WriteLine("Emails added to Global Suppression Group. Press any key to continue."); | ||
Console.ReadKey(); | ||
|
||
// DELETE EMAILS FROM A SUPPRESSION GROUP | ||
Console.WriteLine("Deleting emails from Global Suppression Group, please wait."); | ||
HttpResponseMessage responseDelete1 = client.GlobalSuppressions.Delete("[email protected]").Result; | ||
Console.WriteLine(responseDelete1.StatusCode); | ||
HttpResponseMessage responseDelete2 = client.GlobalSuppressions.Delete("[email protected]").Result; | ||
Console.WriteLine(responseDelete2.StatusCode); | ||
HttpResponseMessage responseFinal = client.GlobalSuppressions.Get("[email protected]").Result; | ||
Console.WriteLine(responseFinal.StatusCode); | ||
Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result); | ||
HttpResponseMessage responseFinal2 = client.GlobalSuppressions.Get("[email protected]").Result; | ||
Console.WriteLine(responseFinal2.StatusCode); | ||
Console.WriteLine(responseFinal2.Content.ReadAsStringAsync().Result); | ||
Console.WriteLine("Emails removed from Global Suppression Group. Press any key to end."); | ||
Console.ReadKey(); | ||
} | ||
|
||
|
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,57 @@ | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace SendGrid.Resources | ||
{ | ||
public class GlobalSuppressions | ||
{ | ||
private string _endpoint; | ||
private Client _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(Client client, string endpoint = "v3/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<HttpResponseMessage> Get(string email) | ||
{ | ||
return await _client.Get(_endpoint + "/" + email); | ||
} | ||
|
||
/// <summary> | ||
/// Add recipient addresses to the global suppression group. | ||
/// </summary> | ||
/// <param name="recipient_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<HttpResponseMessage> Post(string[] emails) | ||
{ | ||
JArray receipient_emails = new JArray(); | ||
foreach (string email in emails) { receipient_emails.Add(email); } | ||
var data = new JObject(new JProperty("recipient_emails", receipient_emails)); | ||
return await _client.Post(_endpoint, data); | ||
} | ||
|
||
/// <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<HttpResponseMessage> Delete(string email) | ||
{ | ||
return await _client.Delete(_endpoint + "/" + email); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -155,50 +155,96 @@ private void TestDelete() | |
Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode); | ||
} | ||
|
||
[TestFixture] | ||
public class Suppressions | ||
} | ||
|
||
[TestFixture] | ||
public class Suppressions | ||
{ | ||
static string _baseUri = "https://api.sendgrid.com/"; | ||
static string _apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY"); | ||
public Client client = new Client(_apiKey, _baseUri); | ||
|
||
[Test] | ||
public void SuppressionsIntegrationTest() | ||
{ | ||
static string _baseUri = "https://api.sendgrid.com/"; | ||
static string _apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY"); | ||
public Client client = new Client(_apiKey, _baseUri); | ||
int unsubscribeGroupId = 69; | ||
|
||
[Test] | ||
public void SuppressionsIntegrationTest() | ||
{ | ||
int unsubscribeGroupId = 69; | ||
TestGet(unsubscribeGroupId); | ||
string[] emails = { "[email protected]", "[email protected]" }; | ||
TestPost(unsubscribeGroupId, emails); | ||
TestDelete(unsubscribeGroupId, "[email protected]"); | ||
TestDelete(unsubscribeGroupId, "[email protected]"); | ||
} | ||
|
||
TestGet(unsubscribeGroupId); | ||
string[] emails = { "[email protected]", "[email protected]" }; | ||
TestPost(unsubscribeGroupId, emails); | ||
TestDelete(unsubscribeGroupId, "[email protected]"); | ||
TestDelete(unsubscribeGroupId, "[email protected]"); | ||
} | ||
private void TestGet(int unsubscribeGroupId) | ||
{ | ||
HttpResponseMessage response = client.Suppressions.Get(unsubscribeGroupId).Result; | ||
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); | ||
string rawString = response.Content.ReadAsStringAsync().Result; | ||
dynamic jsonObject = JsonConvert.DeserializeObject(rawString); | ||
Assert.IsNotNull(jsonObject); | ||
} | ||
|
||
private void TestGet(int unsubscribeGroupId) | ||
{ | ||
HttpResponseMessage response = client.Suppressions.Get(unsubscribeGroupId).Result; | ||
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); | ||
string rawString = response.Content.ReadAsStringAsync().Result; | ||
dynamic jsonObject = JsonConvert.DeserializeObject(rawString); | ||
Assert.IsNotNull(jsonObject); | ||
} | ||
private void TestPost(int unsubscribeGroupId, string[] emails) | ||
{ | ||
HttpResponseMessage response = client.Suppressions.Post(unsubscribeGroupId, emails).Result; | ||
Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); | ||
string rawString = response.Content.ReadAsStringAsync().Result; | ||
dynamic jsonObject = JObject.Parse(rawString); | ||
string recipient_emails = jsonObject.recipient_emails.ToString(); | ||
Assert.IsNotNull(recipient_emails); | ||
} | ||
|
||
private void TestPost(int unsubscribeGroupId, string[] emails) | ||
{ | ||
HttpResponseMessage response = client.Suppressions.Post(unsubscribeGroupId, emails).Result; | ||
Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); | ||
string rawString = response.Content.ReadAsStringAsync().Result; | ||
dynamic jsonObject = JObject.Parse(rawString); | ||
string recipient_emails = jsonObject.recipient_emails.ToString(); | ||
Assert.IsNotNull(recipient_emails); | ||
} | ||
private void TestDelete(int unsubscribeGroupId, string email) | ||
{ | ||
HttpResponseMessage response = client.Suppressions.Delete(unsubscribeGroupId, email).Result; | ||
Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode); | ||
} | ||
|
||
private void TestDelete(int unsubscribeGroupId, string email) | ||
{ | ||
HttpResponseMessage response = client.Suppressions.Delete(unsubscribeGroupId, email).Result; | ||
Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode); | ||
} | ||
} | ||
|
||
[TestFixture] | ||
public class GlobalSuppressions | ||
{ | ||
static string _baseUri = "https://api.sendgrid.com/"; | ||
static string _apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY"); | ||
public Client client = new Client(_apiKey, _baseUri); | ||
|
||
[Test] | ||
public void SuppressionsIntegrationTest() | ||
{ | ||
string email = "[email protected]"; | ||
|
||
TestGet(email); | ||
string[] emails = { "[email protected]", "[email protected]" }; | ||
TestPost(emails); | ||
TestDelete("[email protected]"); | ||
TestDelete("[email protected]"); | ||
} | ||
|
||
private void TestGet(string email) | ||
{ | ||
HttpResponseMessage response = client.GlobalSuppressions.Get(email).Result; | ||
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); | ||
string rawString = response.Content.ReadAsStringAsync().Result; | ||
dynamic jsonObject = JsonConvert.DeserializeObject(rawString); | ||
Assert.IsNotNull(jsonObject); | ||
} | ||
|
||
private void TestPost(string[] emails) | ||
{ | ||
HttpResponseMessage response = client.GlobalSuppressions.Post(emails).Result; | ||
Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); | ||
string rawString = response.Content.ReadAsStringAsync().Result; | ||
dynamic jsonObject = JObject.Parse(rawString); | ||
string recipient_emails = jsonObject.recipient_emails.ToString(); | ||
Assert.IsNotNull(recipient_emails); | ||
} | ||
|
||
private void TestDelete(string email) | ||
{ | ||
HttpResponseMessage response = client.GlobalSuppressions.Delete(email).Result; | ||
Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode); | ||
} | ||
|
||
} | ||
|