Skip to content

Commit 8c6b06f

Browse files
Merge pull request #169 from sendgrid/global_suppressions
Global Suppressions [GET, POST, DELETE]
2 parents 1fef90a + 3f6197a commit 8c6b06f

File tree

9 files changed

+230
-44
lines changed

9 files changed

+230
-44
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.3] - 2015-12-14
5+
###Added
6+
- Implemented the global suppressions /asm/suppressions/global endpoint [GET, POST, DELETE]
7+
48
## [6.3.2] - 2015-12-11
59
###Added
610
- Implemented the suppressions /asm/groups/:group_id/suppressions endpoint [GET, POST, DELETE]

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,42 @@ ver groupId = "<UNSUBSCRIBE GROUP ID>";
277277
HttpResponseMessage responseDelete1 = client.Suppressions.Delete(groupId, "[email protected]").Result;
278278
```
279279

280+
## Global Suppressions ##
281+
282+
Please refer to [our documentation](https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/global_suppressions.html) for further details.
283+
284+
Check if a recipient address is in the global suppressions group. [GET]
285+
286+
```csharp
287+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
288+
var client = new SendGrid.Client(apiKey);
289+
// Leave off .Result for an asyncronous call
290+
string email = "[email protected]";
291+
HttpResponseMessage responseGet = client.GlobalSuppressions.Get(email).Result;
292+
```
293+
294+
Add recipient addresses to the global suppression group. [POST]
295+
296+
If the group has been deleted, this request will add the address to the global suppression.
297+
298+
```csharp
299+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
300+
var client = new SendGrid.Client(apiKey);
301+
string[] emails = { "[email protected]", "[email protected]" };
302+
// Leave off .Result for an asyncronous call
303+
HttpResponseMessage responsePost = client.GlobalSuppressions.Post(emails).Result;
304+
```
305+
306+
Delete a recipient email from the global suppressions group. [DELETE]
307+
308+
```csharp
309+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
310+
var client = new SendGrid.Client(apiKey);
311+
string email = "[email protected]";
312+
// Leave off .Result for an asyncronous call
313+
HttpResponseMessage responseDelete1 = client.GlobalSuppressions.Delete(email).Result;
314+
```
315+
280316
#How to: Testing
281317

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

SendGrid/Example/Program.cs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ private static void Main()
1919
ApiKeys();
2020
UnsubscribeGroups();
2121
Suppressions();
22+
GlobalSuppressions();
2223
}
2324

2425
private static void SendAsync(SendGrid.SendGridMessage message)
@@ -95,7 +96,7 @@ private static void ApiKeys()
9596
HttpResponseMessage responseFinal = client.ApiKeys.Get().Result;
9697
Console.WriteLine(responseFinal.StatusCode);
9798
Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result);
98-
Console.WriteLine("API Key Deleted, press any key to end");
99+
Console.WriteLine("API Key Deleted, press any key to end.");
99100
Console.ReadKey();
100101
}
101102

@@ -136,7 +137,7 @@ private static void UnsubscribeGroups()
136137
HttpResponseMessage responseFinal = client.UnsubscribeGroups.Get().Result;
137138
Console.WriteLine(responseFinal.StatusCode);
138139
Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result);
139-
Console.WriteLine("Unsubscribe Group Deleted, press any key to end");
140+
Console.WriteLine("Unsubscribe Group Deleted, press any key to end.");
140141
Console.ReadKey();
141142
}
142143

@@ -172,7 +173,46 @@ private static void Suppressions()
172173
HttpResponseMessage responseFinal = client.Suppressions.Get(groupID).Result;
173174
Console.WriteLine(responseFinal.StatusCode);
174175
Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result);
175-
Console.WriteLine("Emails removed from Suppression Group" + groupID.ToString() + "Deleted. Press any key to end");
176+
Console.WriteLine("Emails removed from Suppression Group" + groupID.ToString() + "Deleted. Press any key to end.");
177+
Console.ReadKey();
178+
}
179+
180+
private static void GlobalSuppressions()
181+
{
182+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
183+
var client = new SendGrid.Client(apiKey);
184+
185+
// GET SUPPRESSED ADDRESSES FOR A GIVEN GROUP
186+
var email = "[email protected]";
187+
HttpResponseMessage responseGetUnique = client.GlobalSuppressions.Get(email).Result;
188+
Console.WriteLine(responseGetUnique.StatusCode);
189+
Console.WriteLine(responseGetUnique.Content.ReadAsStringAsync().Result);
190+
Console.WriteLine("Determines if the given email is listed on the Global Suppressions list. Press any key to continue.");
191+
Console.ReadKey();
192+
193+
// ADD EMAILS TO A SUPPRESSION GROUP
194+
string[] emails = { "[email protected]", "[email protected]" };
195+
HttpResponseMessage responsePost = client.GlobalSuppressions.Post(emails).Result;
196+
var rawString = responsePost.Content.ReadAsStringAsync().Result;
197+
dynamic jsonObject = JObject.Parse(rawString);
198+
Console.WriteLine(responsePost.StatusCode);
199+
Console.WriteLine(responsePost.Content.ReadAsStringAsync().Result);
200+
Console.WriteLine("Emails added to Global Suppression Group. Press any key to continue.");
201+
Console.ReadKey();
202+
203+
// DELETE EMAILS FROM A SUPPRESSION GROUP
204+
Console.WriteLine("Deleting emails from Global Suppression Group, please wait.");
205+
HttpResponseMessage responseDelete1 = client.GlobalSuppressions.Delete("[email protected]").Result;
206+
Console.WriteLine(responseDelete1.StatusCode);
207+
HttpResponseMessage responseDelete2 = client.GlobalSuppressions.Delete("[email protected]").Result;
208+
Console.WriteLine(responseDelete2.StatusCode);
209+
HttpResponseMessage responseFinal = client.GlobalSuppressions.Get("[email protected]").Result;
210+
Console.WriteLine(responseFinal.StatusCode);
211+
Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result);
212+
HttpResponseMessage responseFinal2 = client.GlobalSuppressions.Get("[email protected]").Result;
213+
Console.WriteLine(responseFinal2.StatusCode);
214+
Console.WriteLine(responseFinal2.Content.ReadAsStringAsync().Result);
215+
Console.WriteLine("Emails removed from Global Suppression Group. Press any key to end.");
176216
Console.ReadKey();
177217
}
178218

SendGrid/SendGrid/Client.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class Client
1616
public APIKeys ApiKeys;
1717
public UnsubscribeGroups UnsubscribeGroups;
1818
public Suppressions Suppressions;
19+
public GlobalSuppressions GlobalSuppressions;
1920
public string Version;
2021
private Uri _baseUri;
2122
private const string MediaType = "application/json";
@@ -37,6 +38,7 @@ public Client(string apiKey, string baseUri = "https://api.sendgrid.com/")
3738
ApiKeys = new APIKeys(this);
3839
UnsubscribeGroups = new UnsubscribeGroups(this);
3940
Suppressions = new Suppressions(this);
41+
GlobalSuppressions = new GlobalSuppressions(this);
4042
}
4143

4244
/// <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.2")]
36-
[assembly: AssemblyFileVersion("6.3.2")]
35+
[assembly: AssemblyVersion("6.3.3")]
36+
[assembly: AssemblyFileVersion("6.3.3")]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.Net.Http;
2+
using System.Threading.Tasks;
3+
using Newtonsoft.Json.Linq;
4+
5+
namespace SendGrid.Resources
6+
{
7+
public class GlobalSuppressions
8+
{
9+
private string _endpoint;
10+
private Client _client;
11+
12+
/// <summary>
13+
/// Constructs the SendGrid Global Suppressions object.
14+
/// See https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/global_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 GlobalSuppressions(Client client, string endpoint = "v3/asm/suppressions/global")
19+
{
20+
_endpoint = endpoint;
21+
_client = client;
22+
}
23+
24+
/// <summary>
25+
/// Check if a recipient address is in the global suppressions group.
26+
/// </summary>
27+
/// <param name="email">email address to check</param>
28+
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/global_suppressions.html</returns>
29+
public async Task<HttpResponseMessage> Get(string email)
30+
{
31+
return await _client.Get(_endpoint + "/" + email);
32+
}
33+
34+
/// <summary>
35+
/// Add recipient addresses to the global suppression group.
36+
/// </summary>
37+
/// <param name="recipient_emails">Array of email addresses to add to the suppression group</param>
38+
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/global_suppressions.html</returns>
39+
public async Task<HttpResponseMessage> Post(string[] emails)
40+
{
41+
JArray receipient_emails = new JArray();
42+
foreach (string email in emails) { receipient_emails.Add(email); }
43+
var data = new JObject(new JProperty("recipient_emails", receipient_emails));
44+
return await _client.Post(_endpoint, data);
45+
}
46+
47+
/// <summary>
48+
/// Delete a recipient email from the global suppressions group.
49+
/// </summary>
50+
/// <param name="email">email address to be removed from the global suppressions group</param>
51+
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/global_suppressions.html</returns>
52+
public async Task<HttpResponseMessage> Delete(string email)
53+
{
54+
return await _client.Delete(_endpoint + "/" + email);
55+
}
56+
}
57+
}

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\GlobalSuppressions.cs" />
6768
<Compile Include="Resources\Suppressions.cs" />
6869
<Compile Include="Resources\UnsubscribeGroups.cs" />
6970
<Compile Include="Resources\APIKeys.cs" />

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.2")]
62-
[assembly: AssemblyFileVersion("6.3.2")]
61+
[assembly: AssemblyVersion("6.3.3")]
62+
[assembly: AssemblyFileVersion("6.3.3")]

SendGrid/UnitTest/UnitTest.cs

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

158-
[TestFixture]
159-
public class Suppressions
158+
}
159+
160+
[TestFixture]
161+
public class Suppressions
162+
{
163+
static string _baseUri = "https://api.sendgrid.com/";
164+
static string _apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
165+
public Client client = new Client(_apiKey, _baseUri);
166+
167+
[Test]
168+
public void SuppressionsIntegrationTest()
160169
{
161-
static string _baseUri = "https://api.sendgrid.com/";
162-
static string _apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
163-
public Client client = new Client(_apiKey, _baseUri);
170+
int unsubscribeGroupId = 69;
164171

165-
[Test]
166-
public void SuppressionsIntegrationTest()
167-
{
168-
int unsubscribeGroupId = 69;
172+
TestGet(unsubscribeGroupId);
173+
string[] emails = { "[email protected]", "[email protected]" };
174+
TestPost(unsubscribeGroupId, emails);
175+
TestDelete(unsubscribeGroupId, "[email protected]");
176+
TestDelete(unsubscribeGroupId, "[email protected]");
177+
}
169178

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-
}
179+
private void TestGet(int unsubscribeGroupId)
180+
{
181+
HttpResponseMessage response = client.Suppressions.Get(unsubscribeGroupId).Result;
182+
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
183+
string rawString = response.Content.ReadAsStringAsync().Result;
184+
dynamic jsonObject = JsonConvert.DeserializeObject(rawString);
185+
Assert.IsNotNull(jsonObject);
186+
}
176187

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-
}
188+
private void TestPost(int unsubscribeGroupId, string[] emails)
189+
{
190+
HttpResponseMessage response = client.Suppressions.Post(unsubscribeGroupId, emails).Result;
191+
Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
192+
string rawString = response.Content.ReadAsStringAsync().Result;
193+
dynamic jsonObject = JObject.Parse(rawString);
194+
string recipient_emails = jsonObject.recipient_emails.ToString();
195+
Assert.IsNotNull(recipient_emails);
196+
}
185197

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-
}
198+
private void TestDelete(int unsubscribeGroupId, string email)
199+
{
200+
HttpResponseMessage response = client.Suppressions.Delete(unsubscribeGroupId, email).Result;
201+
Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode);
202+
}
195203

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-
}
204+
}
205+
206+
[TestFixture]
207+
public class GlobalSuppressions
208+
{
209+
static string _baseUri = "https://api.sendgrid.com/";
210+
static string _apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
211+
public Client client = new Client(_apiKey, _baseUri);
212+
213+
[Test]
214+
public void SuppressionsIntegrationTest()
215+
{
216+
string email = "[email protected]";
217+
218+
TestGet(email);
219+
string[] emails = { "[email protected]", "[email protected]" };
220+
TestPost(emails);
221+
TestDelete("[email protected]");
222+
TestDelete("[email protected]");
223+
}
201224

225+
private void TestGet(string email)
226+
{
227+
HttpResponseMessage response = client.GlobalSuppressions.Get(email).Result;
228+
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
229+
string rawString = response.Content.ReadAsStringAsync().Result;
230+
dynamic jsonObject = JsonConvert.DeserializeObject(rawString);
231+
Assert.IsNotNull(jsonObject);
232+
}
233+
234+
private void TestPost(string[] emails)
235+
{
236+
HttpResponseMessage response = client.GlobalSuppressions.Post(emails).Result;
237+
Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
238+
string rawString = response.Content.ReadAsStringAsync().Result;
239+
dynamic jsonObject = JObject.Parse(rawString);
240+
string recipient_emails = jsonObject.recipient_emails.ToString();
241+
Assert.IsNotNull(recipient_emails);
242+
}
243+
244+
private void TestDelete(string email)
245+
{
246+
HttpResponseMessage response = client.GlobalSuppressions.Delete(email).Result;
247+
Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode);
202248
}
203249

204250
}

0 commit comments

Comments
 (0)