Skip to content

Commit 1fef90a

Browse files
Merge pull request #168 from sendgrid/asm_suppressions_get_post_delete
Asm suppressions get post delete
2 parents 5391fbe + dad72f6 commit 1fef90a

File tree

10 files changed

+197
-10
lines changed

10 files changed

+197
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Change Log
22
All notable changes to this project will be documented in this file.
33

4+
## [6.3.2] - 2015-12-11
5+
###Added
6+
- Implemented the suppressions /asm/groups/:group_id/suppressions endpoint [GET, POST, DELETE]
7+
48
## [6.3.1] - 2015-12-10
59
###Added
610
- Implemented the unsubscribe groups /asm/groups endpoint [GET, POST, DELETE]

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,42 @@ ver unsubscribeGroupId = "<UNSUBSCRIBE GROUP ID>";
241241
HttpResponseMessage responseDelete = client.UnsubscribeGroups.Delete(unsubscribeGroupId).Result;
242242
```
243243

244+
## Suppressions ##
245+
246+
Please refer to [our documentation](https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/suppressions.html) for further details.
247+
248+
Get suppressed addresses for a given group. [GET]
249+
250+
```csharp
251+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
252+
var client = new SendGrid.Client(apiKey);
253+
// Leave off .Result for an asyncronous call
254+
int groupId = <Group ID>;
255+
HttpResponseMessage responseGet = client.Suppressions.Get(groupId).Result;
256+
```
257+
258+
Add recipient addresses to the suppressions list for a given group. [POST]
259+
260+
If the group has been deleted, this request will add the address to the global suppression.
261+
262+
```csharp
263+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
264+
var client = new SendGrid.Client(apiKey);
265+
string[] emails = { "[email protected]", "[email protected]" };
266+
// Leave off .Result for an asyncronous call
267+
HttpResponseMessage responsePost = client.Suppressions.Post(groupID, emails).Result;
268+
```
269+
270+
Delete a recipient email from the suppressions list for a group. [DELETE]
271+
272+
```csharp
273+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
274+
var client = new SendGrid.Client(apiKey);
275+
ver groupId = "<UNSUBSCRIBE GROUP ID>";
276+
// Leave off .Result for an asyncronous call
277+
HttpResponseMessage responseDelete1 = client.Suppressions.Delete(groupId, "[email protected]").Result;
278+
```
279+
244280
#How to: Testing
245281

246282
* Load the solution (We have tested using the Visual Studio Community Edition)

SendGrid/Example/Program.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ private static void Main()
1818
// Test viewing, creating, modifying and deleting API keys through our v3 Web API
1919
ApiKeys();
2020
UnsubscribeGroups();
21+
Suppressions();
2122
}
2223

2324
private static void SendAsync(SendGrid.SendGridMessage message)
@@ -115,7 +116,7 @@ private static void UnsubscribeGroups()
115116
HttpResponseMessage responseGetUnique = client.UnsubscribeGroups.Get(unsubscribeGroupID).Result;
116117
Console.WriteLine(responseGetUnique.StatusCode);
117118
Console.WriteLine(responseGetUnique.Content.ReadAsStringAsync().Result);
118-
Console.WriteLine("These is an Unsubscribe Group with ID: " + unsubscribeGroupID.ToString() + ". Press any key to continue.");
119+
Console.WriteLine("This is an Unsubscribe Group with ID: " + unsubscribeGroupID.ToString() + ". Press any key to continue.");
119120
Console.ReadKey();
120121

121122
// POST UNSUBSCRIBE GROUP
@@ -138,5 +139,42 @@ private static void UnsubscribeGroups()
138139
Console.WriteLine("Unsubscribe Group Deleted, press any key to end");
139140
Console.ReadKey();
140141
}
142+
143+
private static void Suppressions()
144+
{
145+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
146+
var client = new SendGrid.Client(apiKey);
147+
148+
// GET SUPPRESSED ADDRESSES FOR A GIVEN GROUP
149+
int groupID = 69;
150+
HttpResponseMessage responseGetUnique = client.Suppressions.Get(groupID).Result;
151+
Console.WriteLine(responseGetUnique.StatusCode);
152+
Console.WriteLine(responseGetUnique.Content.ReadAsStringAsync().Result);
153+
Console.WriteLine("These are the suppressed emails with group ID: " + groupID.ToString() + ". Press any key to continue.");
154+
Console.ReadKey();
155+
156+
// ADD EMAILS TO A SUPPRESSION GROUP
157+
string[] emails = { "[email protected]", "[email protected]" };
158+
HttpResponseMessage responsePost = client.Suppressions.Post(groupID, emails).Result;
159+
var rawString = responsePost.Content.ReadAsStringAsync().Result;
160+
dynamic jsonObject = JObject.Parse(rawString);
161+
Console.WriteLine(responsePost.StatusCode);
162+
Console.WriteLine(responsePost.Content.ReadAsStringAsync().Result);
163+
Console.WriteLine("Emails added to Suppression Group:" + groupID.ToString() + ". Press any key to continue.");
164+
Console.ReadKey();
165+
166+
// DELETE EMAILS FROM A SUPPRESSION GROUP
167+
Console.WriteLine("Deleting emails from Suppression Group, please wait.");
168+
HttpResponseMessage responseDelete1 = client.Suppressions.Delete(groupID, "[email protected]").Result;
169+
Console.WriteLine(responseDelete1.StatusCode);
170+
HttpResponseMessage responseDelete2 = client.Suppressions.Delete(groupID, "[email protected]").Result;
171+
Console.WriteLine(responseDelete2.StatusCode);
172+
HttpResponseMessage responseFinal = client.Suppressions.Get(groupID).Result;
173+
Console.WriteLine(responseFinal.StatusCode);
174+
Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result);
175+
Console.WriteLine("Emails removed from Suppression Group" + groupID.ToString() + "Deleted. Press any key to end");
176+
Console.ReadKey();
177+
}
178+
141179
}
142180
}

SendGrid/SendGrid/Client.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class Client
1515
private string _apiKey;
1616
public APIKeys ApiKeys;
1717
public UnsubscribeGroups UnsubscribeGroups;
18+
public Suppressions Suppressions;
1819
public string Version;
1920
private Uri _baseUri;
2021
private const string MediaType = "application/json";
@@ -35,6 +36,7 @@ public Client(string apiKey, string baseUri = "https://api.sendgrid.com/")
3536
Version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
3637
ApiKeys = new APIKeys(this);
3738
UnsubscribeGroups = new UnsubscribeGroups(this);
39+
Suppressions = new Suppressions(this);
3840
}
3941

4042
/// <summary>

SendGrid/SendGrid/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("6.3.1")]
36-
[assembly: AssemblyFileVersion("6.3.1")]
35+
[assembly: AssemblyVersion("6.3.2")]
36+
[assembly: AssemblyFileVersion("6.3.2")]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System.Net.Http;
2+
using System.Threading.Tasks;
3+
using Newtonsoft.Json.Linq;
4+
5+
namespace SendGrid.Resources
6+
{
7+
public class Suppressions
8+
{
9+
private string _endpoint;
10+
private Client _client;
11+
12+
/// <summary>
13+
/// Constructs the SendGrid Suppressions object.
14+
/// See https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/suppressions.html
15+
/// </summary>
16+
/// <param name="client">SendGrid Web API v3 client</param>
17+
/// <param name="endpoint">Resource endpoint, do not prepend slash</param>
18+
public Suppressions(Client client, string endpoint = "v3/asm/groups")
19+
{
20+
_endpoint = endpoint;
21+
_client = client;
22+
}
23+
24+
/// <summary>
25+
/// Get suppressed addresses for a given group.
26+
/// </summary>
27+
/// <param name="groupId">ID of the suppression group</param>
28+
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/suppressions.html</returns>
29+
public async Task<HttpResponseMessage> Get(int groupId)
30+
{
31+
return await _client.Get(_endpoint + "/" + groupId.ToString() + "/suppressions");
32+
}
33+
34+
/// <summary>
35+
/// Add recipient addresses to the suppressions list for a given group.
36+
///
37+
/// If the group has been deleted, this request will add the address to the global suppression.
38+
/// </summary>
39+
/// <param name="groupId">ID of the suppression group</param>
40+
/// <param name="recipient_emails">Array of email addresses to add to the suppression group</param>
41+
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/suppressions.html</returns>
42+
public async Task<HttpResponseMessage> Post(int groupId, string[] emails)
43+
{
44+
JArray receipient_emails = new JArray();
45+
foreach (string email in emails) { receipient_emails.Add(email); }
46+
var data = new JObject(new JProperty("recipient_emails", receipient_emails));
47+
return await _client.Post(_endpoint + "/" + groupId.ToString() + "/suppressions", data);
48+
}
49+
50+
/// <summary>
51+
/// Delete a suppression group.
52+
/// </summary>
53+
/// <param name="groupId">ID of the suppression group to delete</param>
54+
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/suppressions.html</returns>
55+
public async Task<HttpResponseMessage> Delete(int groupId, string email)
56+
{
57+
return await _client.Delete(_endpoint + "/" + groupId.ToString() + "/suppressions/" + email);
58+
}
59+
}
60+
}

SendGrid/SendGrid/Resources/UnsubscribeGroups.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public UnsubscribeGroups(Client client, string endpoint = "v3/asm/groups")
2222
}
2323

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

3333
/// <summary>
34-
/// Retrieve all suppression groups associated with the user.s
34+
/// Get information on a single suppression group.
3535
/// </summary>
36+
/// <param name="unsubscribeGroupId">ID of the suppression group to delete</param>
3637
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/groups.html</returns>
37-
public async Task<HttpResponseMessage> Get(int unsubscribeGroupID)
38+
public async Task<HttpResponseMessage> Get(int unsubscribeGroupId)
3839
{
39-
string endpoint = _endpoint + "/" + unsubscribeGroupID;
40-
return await _client.Get(endpoint);
40+
return await _client.Get(_endpoint + "/" + unsubscribeGroupId);
4141
}
4242

4343
/// <summary>

SendGrid/SendGrid/SendGrid.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<ItemGroup>
6565
<Compile Include="Client.cs" />
6666
<Compile Include="Properties\AssemblyInfo.cs" />
67+
<Compile Include="Resources\Suppressions.cs" />
6768
<Compile Include="Resources\UnsubscribeGroups.cs" />
6869
<Compile Include="Resources\APIKeys.cs" />
6970
</ItemGroup>

SendGrid/SendGridMail/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@
5858
// by using the '*' as shown below:
5959
// [assembly: AssemblyVersion("1.0.*")]
6060

61-
[assembly: AssemblyVersion("6.3.1")]
62-
[assembly: AssemblyFileVersion("6.3.1")]
61+
[assembly: AssemblyVersion("6.3.2")]
62+
[assembly: AssemblyFileVersion("6.3.2")]

SendGrid/UnitTest/UnitTest.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,5 +155,51 @@ private void TestDelete()
155155
Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode);
156156
}
157157

158+
[TestFixture]
159+
public class Suppressions
160+
{
161+
static string _baseUri = "https://api.sendgrid.com/";
162+
static string _apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
163+
public Client client = new Client(_apiKey, _baseUri);
164+
165+
[Test]
166+
public void SuppressionsIntegrationTest()
167+
{
168+
int unsubscribeGroupId = 69;
169+
170+
TestGet(unsubscribeGroupId);
171+
string[] emails = { "[email protected]", "[email protected]" };
172+
TestPost(unsubscribeGroupId, emails);
173+
TestDelete(unsubscribeGroupId, "[email protected]");
174+
TestDelete(unsubscribeGroupId, "[email protected]");
175+
}
176+
177+
private void TestGet(int unsubscribeGroupId)
178+
{
179+
HttpResponseMessage response = client.Suppressions.Get(unsubscribeGroupId).Result;
180+
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
181+
string rawString = response.Content.ReadAsStringAsync().Result;
182+
dynamic jsonObject = JsonConvert.DeserializeObject(rawString);
183+
Assert.IsNotNull(jsonObject);
184+
}
185+
186+
private void TestPost(int unsubscribeGroupId, string[] emails)
187+
{
188+
HttpResponseMessage response = client.Suppressions.Post(unsubscribeGroupId, emails).Result;
189+
Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
190+
string rawString = response.Content.ReadAsStringAsync().Result;
191+
dynamic jsonObject = JObject.Parse(rawString);
192+
string recipient_emails = jsonObject.recipient_emails.ToString();
193+
Assert.IsNotNull(recipient_emails);
194+
}
195+
196+
private void TestDelete(int unsubscribeGroupId, string email)
197+
{
198+
HttpResponseMessage response = client.Suppressions.Delete(unsubscribeGroupId, email).Result;
199+
Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode);
200+
}
201+
202+
}
203+
158204
}
159205
}

0 commit comments

Comments
 (0)