diff --git a/CHANGELOG.md b/CHANGELOG.md index 34aa6fadd..0da8c6b23 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.1] - 2015-12-10 +###Added +- Implemented the unsubscribe groups /asm/groups endpoint [GET, POST, DELETE] + ## [6.3.0] - 2015-11-24 ###Added - Send emails using API Key diff --git a/README.md b/README.md index dd28d28e3..aa0255d7d 100644 --- a/README.md +++ b/README.md @@ -193,6 +193,54 @@ ver apiKeyId = ""; HttpResponseMessage responseDelete = client.ApiKeys.Delete(apiKeyId).Result; ``` +## Unsubscribe Groups ## + +Please refer to [our documentation](https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/groups.html) for further details. + +Retrieve all suppression groups associated with the user. [GET] + +```csharp +String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User); +var client = new SendGrid.Client(apiKey); +// Leave off .Result for an asyncronous call +HttpResponseMessage responseGet = client.ApiKeys.Get().Result; +``` + +Get information on a single suppression group. [GET] + +```csharp +String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User); +var client = new SendGrid.Client(apiKey); +// Leave off .Result for an asyncronous call +HttpResponseMessage responseGet = client.UnsubscribeGroups.Get().Result; +``` + +Create a new suppression group. [POST] + +There is a limit of 25 groups per user. + +```csharp +String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User); +var client = new SendGrid.Client(apiKey); +var unsubscribeGroupName = "CSharpTestUnsubscribeGroup"; +var unsubscribeGroupDescription = "CSharp test Unsubscribe Group description."; +var unsubscribeGroupIsDefault = false; +// Leave off .Result for an asyncronous call +HttpResponseMessage responsePost = client.UnsubscribeGroups.Post(unsubscribeGroupName, unsubscribeGroupDescription, unsubscribeGroupIsDefault ).Result; +``` + +Delete a suppression group. [DELETE] + +You can only delete groups that have not been attached to sent mail in the last 60 days. If a recipient uses the “one-click unsubscribe” option on an email associated with a deleted group, that recipient will be added to the global suppression list. + +```csharp +String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User); +var client = new SendGrid.Client(apiKey); +ver unsubscribeGroupId = ""; +// Leave off .Result for an asyncronous call +HttpResponseMessage responseDelete = client.UnsubscribeGroups.Delete(unsubscribeGroupId).Result; +``` + #How to: Testing * Load the solution (We have tested using the Visual Studio Community Edition) diff --git a/SendGrid/Example/Program.cs b/SendGrid/Example/Program.cs index 62e80604e..8430e9642 100644 --- a/SendGrid/Example/Program.cs +++ b/SendGrid/Example/Program.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Net; using System.Net.Http; using System.Net.Mail; using Newtonsoft.Json.Linq; @@ -18,8 +17,9 @@ private static void Main() SendEmail(to, from, fromName); // Test viewing, creating, modifying and deleting API keys through our v3 Web API ApiKeys(); + UnsubscribeGroups(); } - + private static void SendAsync(SendGrid.SendGridMessage message) { string apikey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY"); @@ -97,5 +97,46 @@ private static void ApiKeys() Console.WriteLine("API Key Deleted, press any key to end"); Console.ReadKey(); } + + private static void UnsubscribeGroups() + { + String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User); + var client = new SendGrid.Client(apiKey); + + // GET UNSUBSCRIBE GROUPS + HttpResponseMessage responseGet = client.UnsubscribeGroups.Get().Result; + Console.WriteLine(responseGet.StatusCode); + Console.WriteLine(responseGet.Content.ReadAsStringAsync().Result); + Console.WriteLine("These are your current Unsubscribe Groups. Press any key to continue."); + Console.ReadKey(); + + // GET A PARTICULAR UNSUBSCRIBE GROUP + int unsubscribeGroupID = 69; + 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.ReadKey(); + + // POST UNSUBSCRIBE GROUP + HttpResponseMessage responsePost = client.UnsubscribeGroups.Post("C Sharp Unsubscribes", "Testing the C Sharp Library", false).Result; + var rawString = responsePost.Content.ReadAsStringAsync().Result; + dynamic jsonObject = JObject.Parse(rawString); + var unsubscribeGroupId = jsonObject.id.ToString(); + Console.WriteLine(responsePost.StatusCode); + Console.WriteLine(responsePost.Content.ReadAsStringAsync().Result); + Console.WriteLine("Unsubscribe Group created. Press any key to continue."); + Console.ReadKey(); + + // DELETE UNSUBSCRIBE GROUP + Console.WriteLine("Deleting Unsubscribe Group, please wait."); + HttpResponseMessage responseDelete = client.UnsubscribeGroups.Delete(unsubscribeGroupId).Result; + Console.WriteLine(responseDelete.StatusCode); + 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.ReadKey(); + } } } diff --git a/SendGrid/SendGrid/Client.cs b/SendGrid/SendGrid/Client.cs index dc45b85c3..0b5383eed 100644 --- a/SendGrid/SendGrid/Client.cs +++ b/SendGrid/SendGrid/Client.cs @@ -14,6 +14,7 @@ public class Client { private string _apiKey; public APIKeys ApiKeys; + public UnsubscribeGroups UnsubscribeGroups; public string Version; private Uri _baseUri; private const string MediaType = "application/json"; @@ -33,6 +34,7 @@ public Client(string apiKey, string baseUri = "https://api.sendgrid.com/") _apiKey = apiKey; Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); ApiKeys = new APIKeys(this); + UnsubscribeGroups = new UnsubscribeGroups(this); } /// diff --git a/SendGrid/SendGrid/Properties/AssemblyInfo.cs b/SendGrid/SendGrid/Properties/AssemblyInfo.cs index 84b1ab95b..b78f1a6cc 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.0")] -[assembly: AssemblyFileVersion("6.3.0")] +[assembly: AssemblyVersion("6.3.1")] +[assembly: AssemblyFileVersion("6.3.1")] diff --git a/SendGrid/SendGrid/Resources/APIKeys.cs b/SendGrid/SendGrid/Resources/APIKeys.cs index 9b98e7e7b..37bd4a7ff 100644 --- a/SendGrid/SendGrid/Resources/APIKeys.cs +++ b/SendGrid/SendGrid/Resources/APIKeys.cs @@ -1,5 +1,4 @@ using System.Net.Http; -using System.Runtime.InteropServices; using System.Threading.Tasks; using Newtonsoft.Json.Linq; @@ -53,7 +52,7 @@ public async Task Delete(string apiKeyId) } /// - /// Delete a API key + /// Patch a API key /// /// ID of the API Key to rename /// New API Key name diff --git a/SendGrid/SendGrid/Resources/UnsubscribeGroups.cs b/SendGrid/SendGrid/Resources/UnsubscribeGroups.cs new file mode 100644 index 000000000..17d8acee0 --- /dev/null +++ b/SendGrid/SendGrid/Resources/UnsubscribeGroups.cs @@ -0,0 +1,70 @@ +using System.Net.Http; +using System.Threading.Tasks; +using Newtonsoft.Json.Linq; + +namespace SendGrid.Resources +{ + public class UnsubscribeGroups + { + private string _endpoint; + private Client _client; + + /// + /// Constructs the SendGrid UnsubscribeGroups object. + /// See https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/groups.html + /// + /// SendGrid Web API v3 client + /// Resource endpoint, do not prepend slash + public UnsubscribeGroups(Client client, string endpoint = "v3/asm/groups") + { + _endpoint = endpoint; + _client = client; + } + + /// + /// Retrieve all suppression groups associated with the user.s + /// + /// https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/groups.html + public async Task Get() + { + return await _client.Get(_endpoint); + } + + /// + /// Retrieve all suppression groups associated with the user.s + /// + /// https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/groups.html + public async Task Get(int unsubscribeGroupID) + { + string endpoint = _endpoint + "/" + unsubscribeGroupID; + return await _client.Get(endpoint); + } + + /// + /// Create a new suppression group. + /// + /// The name of the new suppression group + /// A description of the suppression group + /// Default value is false + /// https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/groups.html + public async Task Post(string unsubscribeGroupName, + string unsubscribeGroupDescription, + bool unsubscribeGroupIsDefault) + { + var data = new JObject {{"name", unsubscribeGroupName}, + {"description", unsubscribeGroupDescription}, + {"is_default", unsubscribeGroupIsDefault}}; + return await _client.Post(_endpoint, data); + } + + /// + /// Delete a 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 Delete(string unsubscribeGroupId) + { + return await _client.Delete(_endpoint + "/" + unsubscribeGroupId); + } + } +} \ No newline at end of file diff --git a/SendGrid/SendGrid/SendGrid.csproj b/SendGrid/SendGrid/SendGrid.csproj index d9645d0aa..49ea15893 100644 --- a/SendGrid/SendGrid/SendGrid.csproj +++ b/SendGrid/SendGrid/SendGrid.csproj @@ -64,6 +64,7 @@ + diff --git a/SendGrid/SendGridMail/Properties/AssemblyInfo.cs b/SendGrid/SendGridMail/Properties/AssemblyInfo.cs index bedd624df..e5931d26d 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.0")] -[assembly: AssemblyFileVersion("6.3.0")] \ No newline at end of file +[assembly: AssemblyVersion("6.3.1")] +[assembly: AssemblyFileVersion("6.3.1")] \ No newline at end of file diff --git a/SendGrid/SendGridMail/Transport/Web.cs b/SendGrid/SendGridMail/Transport/Web.cs index eb664278c..dbf5d28cb 100644 --- a/SendGrid/SendGridMail/Transport/Web.cs +++ b/SendGrid/SendGridMail/Transport/Web.cs @@ -7,8 +7,6 @@ using System.Net.Http.Headers; using System.Reflection; using System.Threading.Tasks; -using System.Xml; -using Exceptions; using SendGrid.SmtpApi; // ReSharper disable MemberCanBePrivate.Global diff --git a/SendGrid/UnitTest/UnitTest.cs b/SendGrid/UnitTest/UnitTest.cs index 5d1b69607..853d59e8e 100644 --- a/SendGrid/UnitTest/UnitTest.cs +++ b/SendGrid/UnitTest/UnitTest.cs @@ -5,6 +5,7 @@ using NUnit.Framework; using Newtonsoft.Json.Linq; using SendGrid; +using Newtonsoft.Json; namespace UnitTest { @@ -94,4 +95,65 @@ public void TestGetTenTimesAsync() Task.WaitAll(tasks); } } + + [TestFixture] + public class UnsubscribeGroups + { + static string _baseUri = "https://api.sendgrid.com/"; + static string _apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY"); + public Client client = new Client(_apiKey, _baseUri); + private static string _unsubscribe_groups_key_id = ""; + + [Test] + public void UnsubscribeGroupsIntegrationTest() + { + int unsubscribeGroupId = 69; + + TestGet(); + TestGetUnique(unsubscribeGroupId); + TestPost(); + TestDelete(); + } + + private void TestGet() + { + HttpResponseMessage response = client.UnsubscribeGroups.Get().Result; + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); + string rawString = response.Content.ReadAsStringAsync().Result; + dynamic jsonObject = JsonConvert.DeserializeObject(rawString); + Assert.IsNotNull(jsonObject); + } + + private void TestGetUnique(int unsubscribeGroupId) + { + HttpResponseMessage response = client.UnsubscribeGroups.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() + { + HttpResponseMessage response = client.UnsubscribeGroups.Post("C Sharp Unsubscribes", "Testing the C Sharp Library", false).Result; + Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); + string rawString = response.Content.ReadAsStringAsync().Result; + dynamic jsonObject = JObject.Parse(rawString); + string name = jsonObject.name.ToString(); + string description = jsonObject.description.ToString(); + _unsubscribe_groups_key_id = jsonObject.id.ToString(); + bool is_default = jsonObject.is_default; + Assert.IsNotNull(name); + Assert.IsNotNull(description); + Assert.IsNotNull(_unsubscribe_groups_key_id); + Assert.IsNotNull(is_default); + } + + private void TestDelete() + { + HttpResponseMessage response = client.UnsubscribeGroups.Delete(_unsubscribe_groups_key_id).Result; + Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode); + } + + } }