diff options
author | Elmer Thomas <elmer@ThinkingSerious.com> | 2015-12-10 23:23:24 -0800 |
---|---|---|
committer | Elmer Thomas <elmer@ThinkingSerious.com> | 2015-12-10 23:23:24 -0800 |
commit | 5391fbe59c8be7e23f4b89786bdca0063d4730d0 (patch) | |
tree | e1a72df8cf438b53341b747e190eeba23cc92d39 | |
parent | 6d5b3a4c261f25fa308e5a1e87ff1242804db0b1 (diff) | |
parent | 56af17b2b4c5021fa1a5584b662cd8bfd053b3b0 (diff) | |
download | sendgrid-csharp-5391fbe59c8be7e23f4b89786bdca0063d4730d0.zip sendgrid-csharp-5391fbe59c8be7e23f4b89786bdca0063d4730d0.tar.gz sendgrid-csharp-5391fbe59c8be7e23f4b89786bdca0063d4730d0.tar.bz2 |
Merge pull request #166 from sendgrid/asm_groups_get_postv6.3.1
Asm groups [GET, POST, DELETE]
-rw-r--r-- | CHANGELOG.md | 4 | ||||
-rw-r--r-- | README.md | 48 | ||||
-rw-r--r-- | SendGrid/Example/Program.cs | 45 | ||||
-rw-r--r-- | SendGrid/SendGrid/Client.cs | 2 | ||||
-rw-r--r-- | SendGrid/SendGrid/Properties/AssemblyInfo.cs | 4 | ||||
-rw-r--r-- | SendGrid/SendGrid/Resources/APIKeys.cs | 3 | ||||
-rw-r--r-- | SendGrid/SendGrid/Resources/UnsubscribeGroups.cs | 70 | ||||
-rw-r--r-- | SendGrid/SendGrid/SendGrid.csproj | 1 | ||||
-rw-r--r-- | SendGrid/SendGridMail/Properties/AssemblyInfo.cs | 4 | ||||
-rw-r--r-- | SendGrid/SendGridMail/Transport/Web.cs | 2 | ||||
-rw-r--r-- | SendGrid/UnitTest/UnitTest.cs | 62 |
11 files changed, 235 insertions, 10 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 34aa6fa..0da8c6b 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 @@ -193,6 +193,54 @@ ver apiKeyId = "<API Key ID>"; 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 = "<UNSUBSCRIBE GROUP ID>"; +// 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 62e8060..8430e96 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 @@ namespace Example 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 @@ namespace Example 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 dc45b85..0b5383e 100644 --- a/SendGrid/SendGrid/Client.cs +++ b/SendGrid/SendGrid/Client.cs @@ -14,6 +14,7 @@ namespace SendGrid { 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 @@ namespace SendGrid _apiKey = apiKey; Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); ApiKeys = new APIKeys(this); + UnsubscribeGroups = new UnsubscribeGroups(this); } /// <summary> diff --git a/SendGrid/SendGrid/Properties/AssemblyInfo.cs b/SendGrid/SendGrid/Properties/AssemblyInfo.cs index 84b1ab9..b78f1a6 100644 --- a/SendGrid/SendGrid/Properties/AssemblyInfo.cs +++ b/SendGrid/SendGrid/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // 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 9b98e7e..37bd4a7 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 @@ namespace SendGrid.Resources } /// <summary> - /// Delete a API key + /// Patch a API key /// </summary> /// <param name="apiKeyId">ID of the API Key to rename</param> /// <param name="apiKeyName">New API Key name</param> diff --git a/SendGrid/SendGrid/Resources/UnsubscribeGroups.cs b/SendGrid/SendGrid/Resources/UnsubscribeGroups.cs new file mode 100644 index 0000000..17d8ace --- /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; + + /// <summary> + /// Constructs the SendGrid UnsubscribeGroups object. + /// See https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/groups.html + /// </summary> + /// <param name="client">SendGrid Web API v3 client</param> + /// <param name="endpoint">Resource endpoint, do not prepend slash</param> + public UnsubscribeGroups(Client client, string endpoint = "v3/asm/groups") + { + _endpoint = endpoint; + _client = client; + } + + /// <summary> + /// Retrieve all suppression groups associated with the user.s + /// </summary> + /// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/groups.html</returns> + public async Task<HttpResponseMessage> Get() + { + return await _client.Get(_endpoint); + } + + /// <summary> + /// Retrieve all suppression groups associated with the user.s + /// </summary> + /// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/groups.html</returns> + public async Task<HttpResponseMessage> Get(int unsubscribeGroupID) + { + string endpoint = _endpoint + "/" + unsubscribeGroupID; + return await _client.Get(endpoint); + } + + /// <summary> + /// Create a new suppression group. + /// </summary> + /// <param name="unsubscribeGroupName">The name of the new suppression group</param> + /// <param name="unsubscribeGroupDescription">A description of the suppression group</param> + /// <param name="unsubscribeGroupIsDefault">Default value is false</param> + /// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/groups.html</returns> + public async Task<HttpResponseMessage> Post(string unsubscribeGroupName, + string unsubscribeGroupDescription, + bool unsubscribeGroupIsDefault) + { + var data = new JObject {{"name", unsubscribeGroupName}, + {"description", unsubscribeGroupDescription}, + {"is_default", unsubscribeGroupIsDefault}}; + return await _client.Post(_endpoint, data); + } + + /// <summary> + /// Delete a suppression group. + /// </summary> + /// <param name="unsubscribeGroupId">ID of the suppression group to delete</param> + /// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/groups.html</returns> + public async Task<HttpResponseMessage> 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 d9645d0..49ea158 100644 --- a/SendGrid/SendGrid/SendGrid.csproj +++ b/SendGrid/SendGrid/SendGrid.csproj @@ -64,6 +64,7 @@ <ItemGroup> <Compile Include="Client.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="Resources\UnsubscribeGroups.cs" /> <Compile Include="Resources\APIKeys.cs" /> </ItemGroup> <ItemGroup> diff --git a/SendGrid/SendGridMail/Properties/AssemblyInfo.cs b/SendGrid/SendGridMail/Properties/AssemblyInfo.cs index bedd624..e5931d2 100644 --- a/SendGrid/SendGridMail/Properties/AssemblyInfo.cs +++ b/SendGrid/SendGridMail/Properties/AssemblyInfo.cs @@ -58,5 +58,5 @@ using System.Runtime.InteropServices; // 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 eb66427..dbf5d28 100644 --- a/SendGrid/SendGridMail/Transport/Web.cs +++ b/SendGrid/SendGridMail/Transport/Web.cs @@ -7,8 +7,6 @@ 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
diff --git a/SendGrid/UnitTest/UnitTest.cs b/SendGrid/UnitTest/UnitTest.cs index 5d1b696..853d59e 100644 --- a/SendGrid/UnitTest/UnitTest.cs +++ b/SendGrid/UnitTest/UnitTest.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using NUnit.Framework; using Newtonsoft.Json.Linq; using SendGrid; +using Newtonsoft.Json; namespace UnitTest { @@ -94,4 +95,65 @@ namespace UnitTest 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); + } + + } } |