From 2ac9155968c5c6c8cfea138adc4c00524a5f07f6 Mon Sep 17 00:00:00 2001 From: Elmer Thomas Date: Fri, 11 Dec 2015 13:28:24 -0800 Subject: [PATCH 1/5] Added Suppressions [GET, POST, DELETE] --- SendGrid/SendGrid/Client.cs | 2 + SendGrid/SendGrid/Resources/Suppressions.cs | 60 +++++++++++++++++++ .../SendGrid/Resources/UnsubscribeGroups.cs | 10 ++-- SendGrid/SendGrid/SendGrid.csproj | 1 + SendGrid/UnitTest/UnitTest.cs | 46 ++++++++++++++ 5 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 SendGrid/SendGrid/Resources/Suppressions.cs diff --git a/SendGrid/SendGrid/Client.cs b/SendGrid/SendGrid/Client.cs index 0b5383eed..fdcaf8b9f 100644 --- a/SendGrid/SendGrid/Client.cs +++ b/SendGrid/SendGrid/Client.cs @@ -15,6 +15,7 @@ public class Client private string _apiKey; public APIKeys ApiKeys; public UnsubscribeGroups UnsubscribeGroups; + public Suppressions Suppressions; public string Version; private Uri _baseUri; private const string MediaType = "application/json"; @@ -35,6 +36,7 @@ public Client(string apiKey, string baseUri = "https://api.sendgrid.com/") Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); ApiKeys = new APIKeys(this); UnsubscribeGroups = new UnsubscribeGroups(this); + Suppressions = new Suppressions(this); } /// diff --git a/SendGrid/SendGrid/Resources/Suppressions.cs b/SendGrid/SendGrid/Resources/Suppressions.cs new file mode 100644 index 000000000..91bb38d60 --- /dev/null +++ b/SendGrid/SendGrid/Resources/Suppressions.cs @@ -0,0 +1,60 @@ +using System.Net.Http; +using System.Threading.Tasks; +using Newtonsoft.Json.Linq; + +namespace SendGrid.Resources +{ + public class Suppressions + { + private string _endpoint; + private Client _client; + + /// + /// Constructs the SendGrid Suppressions object. + /// See https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/suppressions.html + /// + /// SendGrid Web API v3 client + /// Resource endpoint, do not prepend slash + public Suppressions(Client client, string endpoint = "v3/asm/groups") + { + _endpoint = endpoint; + _client = client; + } + + /// + /// Get suppressed addresses for a given group. + /// + /// ID of the suppression group + /// https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/suppressions.html + public async Task Get(int groupId) + { + return await _client.Get(_endpoint + "/" + groupId.ToString() + "/suppressions"); + } + + /// + /// Add recipient addresses to the suppressions list for a given group. + /// + /// If the group has been deleted, this request will add the address to the global suppression. + /// + /// ID of the suppression group + /// Array of email addresses to add to the suppression group + /// https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/suppressions.html + public async Task Post(int groupId, 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 + "/" + groupId.ToString() + "/suppressions", data); + } + + /// + /// Delete a suppression group. + /// + /// ID of the suppression group to delete + /// https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/suppressions.html + public async Task Delete(int groupId, string email) + { + return await _client.Delete(_endpoint + "/" + groupId.ToString() + "/suppressions/" + email); + } + } +} \ No newline at end of file diff --git a/SendGrid/SendGrid/Resources/UnsubscribeGroups.cs b/SendGrid/SendGrid/Resources/UnsubscribeGroups.cs index 17d8acee0..db8e4d228 100644 --- a/SendGrid/SendGrid/Resources/UnsubscribeGroups.cs +++ b/SendGrid/SendGrid/Resources/UnsubscribeGroups.cs @@ -22,7 +22,7 @@ public UnsubscribeGroups(Client client, string endpoint = "v3/asm/groups") } /// - /// Retrieve all suppression groups associated with the user.s + /// Retrieve all suppression groups associated with the user. /// /// https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/groups.html public async Task Get() @@ -31,13 +31,13 @@ public async Task Get() } /// - /// Retrieve all suppression groups associated with the user.s + /// Get information on a single suppression group. /// + /// ID of the suppression group to delete /// https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/groups.html - public async Task Get(int unsubscribeGroupID) + public async Task Get(int unsubscribeGroupId) { - string endpoint = _endpoint + "/" + unsubscribeGroupID; - return await _client.Get(endpoint); + return await _client.Get(_endpoint + "/" + unsubscribeGroupId); } /// diff --git a/SendGrid/SendGrid/SendGrid.csproj b/SendGrid/SendGrid/SendGrid.csproj index 49ea15893..9a57b8650 100644 --- a/SendGrid/SendGrid/SendGrid.csproj +++ b/SendGrid/SendGrid/SendGrid.csproj @@ -64,6 +64,7 @@ + diff --git a/SendGrid/UnitTest/UnitTest.cs b/SendGrid/UnitTest/UnitTest.cs index 853d59e8e..eab34814f 100644 --- a/SendGrid/UnitTest/UnitTest.cs +++ b/SendGrid/UnitTest/UnitTest.cs @@ -155,5 +155,51 @@ private void TestDelete() Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode); } + [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() + { + int unsubscribeGroupId = 69; + + TestGet(unsubscribeGroupId); + string[] emails = { "example@example.com", "example2@example.com" }; + TestPost(unsubscribeGroupId, emails); + TestDelete(unsubscribeGroupId, "example@example.com"); + TestDelete(unsubscribeGroupId, "example2@example.com"); + } + + 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 TestDelete(int unsubscribeGroupId, string email) + { + HttpResponseMessage response = client.Suppressions.Delete(unsubscribeGroupId, email).Result; + Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode); + } + + } + } } From 3ff44a3273bc0c5a78f2c1e28a3235150fb76f32 Mon Sep 17 00:00:00 2001 From: Elmer Thomas Date: Fri, 11 Dec 2015 13:54:36 -0800 Subject: [PATCH 2/5] Added example code --- SendGrid/Example/Program.cs | 40 ++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/SendGrid/Example/Program.cs b/SendGrid/Example/Program.cs index 8430e9642..753f38c4d 100644 --- a/SendGrid/Example/Program.cs +++ b/SendGrid/Example/Program.cs @@ -18,6 +18,7 @@ private static void Main() // Test viewing, creating, modifying and deleting API keys through our v3 Web API ApiKeys(); UnsubscribeGroups(); + Suppressions(); } private static void SendAsync(SendGrid.SendGridMessage message) @@ -115,7 +116,7 @@ private static void UnsubscribeGroups() HttpResponseMessage responseGetUnique = client.UnsubscribeGroups.Get(unsubscribeGroupID).Result; Console.WriteLine(responseGetUnique.StatusCode); Console.WriteLine(responseGetUnique.Content.ReadAsStringAsync().Result); - Console.WriteLine("These is an Unsubscribe Group with ID: " + unsubscribeGroupID.ToString() + ". Press any key to continue."); + Console.WriteLine("This is an Unsubscribe Group with ID: " + unsubscribeGroupID.ToString() + ". Press any key to continue."); Console.ReadKey(); // POST UNSUBSCRIBE GROUP @@ -138,5 +139,42 @@ private static void UnsubscribeGroups() Console.WriteLine("Unsubscribe Group Deleted, press any key to end"); Console.ReadKey(); } + + private static void Suppressions() + { + String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User); + var client = new SendGrid.Client(apiKey); + + // GET SUPPRESSED ADDRESSES FOR A GIVEN GROUP + int groupID = 69; + HttpResponseMessage responseGetUnique = client.Suppressions.Get(groupID).Result; + Console.WriteLine(responseGetUnique.StatusCode); + Console.WriteLine(responseGetUnique.Content.ReadAsStringAsync().Result); + Console.WriteLine("These are the suppressed emails with group ID: " + groupID.ToString() + ". Press any key to continue."); + Console.ReadKey(); + + // ADD EMAILS TO A SUPPRESSION GROUP + string[] emails = { "example@example.com", "example2@example.com" }; + HttpResponseMessage responsePost = client.Suppressions.Post(groupID, 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 Suppression Group:" + groupID.ToString() + ". Press any key to continue."); + Console.ReadKey(); + + // DELETE EMAILS FROM A SUPPRESSION GROUP + Console.WriteLine("Deleting emails from Suppression Group, please wait."); + HttpResponseMessage responseDelete1 = client.Suppressions.Delete(groupID, "example@example.com").Result; + Console.WriteLine(responseDelete1.StatusCode); + HttpResponseMessage responseDelete2 = client.Suppressions.Delete(groupID, "example2@example.com").Result; + Console.WriteLine(responseDelete2.StatusCode); + 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.ReadKey(); + } + } } From 1a73fc2f4a9ba8f4a149598ab063e11c0b08f74a Mon Sep 17 00:00:00 2001 From: Elmer Thomas Date: Fri, 11 Dec 2015 15:45:33 -0800 Subject: [PATCH 3/5] Update README --- README.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/README.md b/README.md index aa0255d7d..635fb4758 100644 --- a/README.md +++ b/README.md @@ -241,6 +241,42 @@ ver unsubscribeGroupId = ""; HttpResponseMessage responseDelete = client.UnsubscribeGroups.Delete(unsubscribeGroupId).Result; ``` +## Suppressions ## + +Please refer to [our documentation](https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/suppressions.html) for further details. + +Get suppressed addresses for a given group. [GET] + +```csharp +String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User); +var client = new SendGrid.Client(apiKey); +// Leave off .Result for an asyncronous call +int groupId = ; +HttpResponseMessage responseGet = client.Suppressions.Get(groupId).Result; +``` + +Add recipient addresses to the suppressions list for a given 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 = { "example@example.com", "example2@example.com" }; +// Leave off .Result for an asyncronous call +HttpResponseMessage responsePost = client.Suppressions.Post(groupID, emails).Result; +``` + +Delete a recipient email from the suppressions list for a group. [DELETE] + +```csharp +String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User); +var client = new SendGrid.Client(apiKey); +ver groupId = ""; +// Leave off .Result for an asyncronous call +HttpResponseMessage responseDelete1 = client.Suppressions.Delete(groupId, "example@example.com").Result; +``` + #How to: Testing * Load the solution (We have tested using the Visual Studio Community Edition) From c12d03142c6403e93b56a4a53d64e30b9fe38687 Mon Sep 17 00:00:00 2001 From: Elmer Thomas Date: Fri, 11 Dec 2015 16:00:33 -0800 Subject: [PATCH 4/5] Version Bump --- SendGrid/SendGrid/Properties/AssemblyInfo.cs | 4 ++-- SendGrid/SendGridMail/Properties/AssemblyInfo.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/SendGrid/SendGrid/Properties/AssemblyInfo.cs b/SendGrid/SendGrid/Properties/AssemblyInfo.cs index b78f1a6cc..b2996f407 100644 --- a/SendGrid/SendGrid/Properties/AssemblyInfo.cs +++ b/SendGrid/SendGrid/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("6.3.1")] -[assembly: AssemblyFileVersion("6.3.1")] +[assembly: AssemblyVersion("6.3.2")] +[assembly: AssemblyFileVersion("6.3.2")] diff --git a/SendGrid/SendGridMail/Properties/AssemblyInfo.cs b/SendGrid/SendGridMail/Properties/AssemblyInfo.cs index e5931d26d..debb32aae 100644 --- a/SendGrid/SendGridMail/Properties/AssemblyInfo.cs +++ b/SendGrid/SendGridMail/Properties/AssemblyInfo.cs @@ -58,5 +58,5 @@ // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("6.3.1")] -[assembly: AssemblyFileVersion("6.3.1")] \ No newline at end of file +[assembly: AssemblyVersion("6.3.2")] +[assembly: AssemblyFileVersion("6.3.2")] \ No newline at end of file From dad72f6bad25edc9760703d7bbe1a5203166066d Mon Sep 17 00:00:00 2001 From: Elmer Thomas Date: Fri, 11 Dec 2015 16:01:44 -0800 Subject: [PATCH 5/5] Update Changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0da8c6b23..47d83df29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Change Log All notable changes to this project will be documented in this file. +## [6.3.2] - 2015-12-11 +###Added +- Implemented the suppressions /asm/groups/:group_id/suppressions endpoint [GET, POST, DELETE] + ## [6.3.1] - 2015-12-10 ###Added - Implemented the unsubscribe groups /asm/groups endpoint [GET, POST, DELETE]