Skip to content

Commit

Permalink
Merge pull request #168 from sendgrid/asm_suppressions_get_post_delete
Browse files Browse the repository at this point in the history
Asm suppressions get post delete
  • Loading branch information
thinkingserious committed Dec 12, 2015
2 parents 5391fbe + dad72f6 commit 1fef90a
Show file tree
Hide file tree
Showing 10 changed files with 197 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.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]
Expand Down
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,42 @@ ver unsubscribeGroupId = "<UNSUBSCRIBE GROUP ID>";
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 = <Group ID>;
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 = { "[email protected]", "[email protected]" };
// 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 = "<UNSUBSCRIBE GROUP ID>";
// Leave off .Result for an asyncronous call
HttpResponseMessage responseDelete1 = client.Suppressions.Delete(groupId, "[email protected]").Result;
```

#How to: Testing

* Load the solution (We have tested using the Visual Studio Community Edition)
Expand Down
40 changes: 39 additions & 1 deletion SendGrid/Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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 = { "[email protected]", "[email protected]" };
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, "[email protected]").Result;
Console.WriteLine(responseDelete1.StatusCode);
HttpResponseMessage responseDelete2 = client.Suppressions.Delete(groupID, "[email protected]").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();
}

}
}
2 changes: 2 additions & 0 deletions SendGrid/SendGrid/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
}

/// <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.1")]
[assembly: AssemblyFileVersion("6.3.1")]
[assembly: AssemblyVersion("6.3.2")]
[assembly: AssemblyFileVersion("6.3.2")]
60 changes: 60 additions & 0 deletions SendGrid/SendGrid/Resources/Suppressions.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Constructs the SendGrid Suppressions object.
/// See https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/suppressions.html
/// </summary>
/// <param name="client">SendGrid Web API v3 client</param>
/// <param name="endpoint">Resource endpoint, do not prepend slash</param>
public Suppressions(Client client, string endpoint = "v3/asm/groups")
{
_endpoint = endpoint;
_client = client;
}

/// <summary>
/// Get suppressed addresses for a given group.
/// </summary>
/// <param name="groupId">ID of the suppression group</param>
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/suppressions.html</returns>
public async Task<HttpResponseMessage> Get(int groupId)
{
return await _client.Get(_endpoint + "/" + groupId.ToString() + "/suppressions");
}

/// <summary>
/// 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.
/// </summary>
/// <param name="groupId">ID of the suppression group</param>
/// <param name="recipient_emails">Array of email addresses to add to the suppression group</param>
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/suppressions.html</returns>
public async Task<HttpResponseMessage> 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);
}

/// <summary>
/// Delete a suppression group.
/// </summary>
/// <param name="groupId">ID of the suppression group to delete</param>
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/suppressions.html</returns>
public async Task<HttpResponseMessage> Delete(int groupId, string email)
{
return await _client.Delete(_endpoint + "/" + groupId.ToString() + "/suppressions/" + email);
}
}
}
10 changes: 5 additions & 5 deletions SendGrid/SendGrid/Resources/UnsubscribeGroups.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public UnsubscribeGroups(Client client, string endpoint = "v3/asm/groups")
}

/// <summary>
/// Retrieve all suppression groups associated with the user.s
/// Retrieve all suppression groups associated with the user.
/// </summary>
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/groups.html</returns>
public async Task<HttpResponseMessage> Get()
Expand All @@ -31,13 +31,13 @@ public async Task<HttpResponseMessage> Get()
}

/// <summary>
/// Retrieve all suppression groups associated with the user.s
/// Get information on a single 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> Get(int unsubscribeGroupID)
public async Task<HttpResponseMessage> Get(int unsubscribeGroupId)
{
string endpoint = _endpoint + "/" + unsubscribeGroupID;
return await _client.Get(endpoint);
return await _client.Get(_endpoint + "/" + unsubscribeGroupId);
}

/// <summary>
Expand Down
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\Suppressions.cs" />
<Compile Include="Resources\UnsubscribeGroups.cs" />
<Compile Include="Resources\APIKeys.cs" />
</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.1")]
[assembly: AssemblyFileVersion("6.3.1")]
[assembly: AssemblyVersion("6.3.2")]
[assembly: AssemblyFileVersion("6.3.2")]
46 changes: 46 additions & 0 deletions SendGrid/UnitTest/UnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = { "[email protected]", "[email protected]" };
TestPost(unsubscribeGroupId, emails);
TestDelete(unsubscribeGroupId, "[email protected]");
TestDelete(unsubscribeGroupId, "[email protected]");
}

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);
}

}

}
}

0 comments on commit 1fef90a

Please sign in to comment.