diff options
-rw-r--r-- | SendGrid/Example/Program.cs | 30 | ||||
-rw-r--r-- | SendGrid/SendGrid/Client.cs | 2 | ||||
-rw-r--r-- | SendGrid/SendGrid/Resources/APIKeys.cs | 3 | ||||
-rw-r--r-- | SendGrid/SendGrid/Resources/UnsubscribeGroups.cs | 83 | ||||
-rw-r--r-- | SendGrid/SendGrid/SendGrid.csproj | 1 | ||||
-rw-r--r-- | SendGrid/UnitTest/UnitTest.cs | 41 |
6 files changed, 155 insertions, 5 deletions
diff --git a/SendGrid/Example/Program.cs b/SendGrid/Example/Program.cs index 4cdc71a..096eae3 100644 --- a/SendGrid/Example/Program.cs +++ b/SendGrid/Example/Program.cs @@ -15,11 +15,13 @@ namespace Example var to = "example@example.com";
var from = "example@example.com";
var fromName = "Jane Doe";
- SendEmail(to, from, fromName);
+ // SendEmail(to, from, fromName);
// Test viewing, creating, modifying and deleting API keys through our v3 Web API
- ApiKeys();
+ // ApiKeys();
+ UnsubscribeGroups();
}
-
+
+ /*
private static void SendAsync(SendGrid.SendGridMessage message)
{
// Create credentials, specifying your user Name and password.
@@ -103,5 +105,27 @@ 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();
+ }
}
}
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/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..a9de080 --- /dev/null +++ b/SendGrid/SendGrid/Resources/UnsubscribeGroups.cs @@ -0,0 +1,83 @@ +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); + } + + /// <summary> + /// Delete a suppression group. + /// </summary> + /// <param name="apiKeyId">ID of the suppression group to rename</param> + /// <param name="apiKeyName">New supression group name</param> + /// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/groups.html</returns> + public async Task<HttpResponseMessage> Patch(string unsubscribeGroupId, string unsubscribeGroupName) + { + var data = new JObject { { "name", unsubscribeGroupName } }; + return await _client.Patch(_endpoint + "/" + unsubscribeGroupId, data); + } + + } +}
\ 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/UnitTest/UnitTest.cs b/SendGrid/UnitTest/UnitTest.cs index 5d1b696..fa1210e 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,44 @@ 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(); + //TestPatch(); + //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); + } + + } } |