Skip to content

Commit

Permalink
Merge pull request #166 from sendgrid/asm_groups_get_post
Browse files Browse the repository at this point in the history
Asm groups [GET, POST, DELETE]
  • Loading branch information
thinkingserious committed Dec 11, 2015
2 parents 6d5b3a4 + 56af17b commit 5391fbe
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 43 additions & 2 deletions SendGrid/Example/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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();
}
}
}
2 changes: 2 additions & 0 deletions SendGrid/SendGrid/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions SendGrid/SendGrid/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
3 changes: 1 addition & 2 deletions SendGrid/SendGrid/Resources/APIKeys.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

Expand Down Expand Up @@ -53,7 +52,7 @@ public async Task<HttpResponseMessage> Delete(string apiKeyId)
}

/// <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>
Expand Down
70 changes: 70 additions & 0 deletions SendGrid/SendGrid/Resources/UnsubscribeGroups.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
1 change: 1 addition & 0 deletions SendGrid/SendGrid/SendGrid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 2 additions & 2 deletions SendGrid/SendGridMail/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@
// 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")]
2 changes: 0 additions & 2 deletions SendGrid/SendGridMail/Transport/Web.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 62 additions & 0 deletions SendGrid/UnitTest/UnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using NUnit.Framework;
using Newtonsoft.Json.Linq;
using SendGrid;
using Newtonsoft.Json;

namespace UnitTest
{
Expand Down Expand Up @@ -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);
}

}
}

0 comments on commit 5391fbe

Please sign in to comment.