Skip to content

Commit 5391fbe

Browse files
Merge pull request #166 from sendgrid/asm_groups_get_post
Asm groups [GET, POST, DELETE]
2 parents 6d5b3a4 + 56af17b commit 5391fbe

File tree

11 files changed

+235
-10
lines changed

11 files changed

+235
-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.1] - 2015-12-10
5+
###Added
6+
- Implemented the unsubscribe groups /asm/groups endpoint [GET, POST, DELETE]
7+
48
## [6.3.0] - 2015-11-24
59
###Added
610
- Send emails using API Key

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,54 @@ ver apiKeyId = "<API Key ID>";
193193
HttpResponseMessage responseDelete = client.ApiKeys.Delete(apiKeyId).Result;
194194
```
195195

196+
## Unsubscribe Groups ##
197+
198+
Please refer to [our documentation](https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/groups.html) for further details.
199+
200+
Retrieve all suppression groups associated with the user. [GET]
201+
202+
```csharp
203+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
204+
var client = new SendGrid.Client(apiKey);
205+
// Leave off .Result for an asyncronous call
206+
HttpResponseMessage responseGet = client.ApiKeys.Get().Result;
207+
```
208+
209+
Get information on a single suppression group. [GET]
210+
211+
```csharp
212+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
213+
var client = new SendGrid.Client(apiKey);
214+
// Leave off .Result for an asyncronous call
215+
HttpResponseMessage responseGet = client.UnsubscribeGroups.Get().Result;
216+
```
217+
218+
Create a new suppression group. [POST]
219+
220+
There is a limit of 25 groups per user.
221+
222+
```csharp
223+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
224+
var client = new SendGrid.Client(apiKey);
225+
var unsubscribeGroupName = "CSharpTestUnsubscribeGroup";
226+
var unsubscribeGroupDescription = "CSharp test Unsubscribe Group description.";
227+
var unsubscribeGroupIsDefault = false;
228+
// Leave off .Result for an asyncronous call
229+
HttpResponseMessage responsePost = client.UnsubscribeGroups.Post(unsubscribeGroupName, unsubscribeGroupDescription, unsubscribeGroupIsDefault ).Result;
230+
```
231+
232+
Delete a suppression group. [DELETE]
233+
234+
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.
235+
236+
```csharp
237+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
238+
var client = new SendGrid.Client(apiKey);
239+
ver unsubscribeGroupId = "<UNSUBSCRIBE GROUP ID>";
240+
// Leave off .Result for an asyncronous call
241+
HttpResponseMessage responseDelete = client.UnsubscribeGroups.Delete(unsubscribeGroupId).Result;
242+
```
243+
196244
#How to: Testing
197245

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

SendGrid/Example/Program.cs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Net;
43
using System.Net.Http;
54
using System.Net.Mail;
65
using Newtonsoft.Json.Linq;
@@ -18,8 +17,9 @@ private static void Main()
1817
SendEmail(to, from, fromName);
1918
// Test viewing, creating, modifying and deleting API keys through our v3 Web API
2019
ApiKeys();
20+
UnsubscribeGroups();
2121
}
22-
22+
2323
private static void SendAsync(SendGrid.SendGridMessage message)
2424
{
2525
string apikey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
@@ -97,5 +97,46 @@ private static void ApiKeys()
9797
Console.WriteLine("API Key Deleted, press any key to end");
9898
Console.ReadKey();
9999
}
100+
101+
private static void UnsubscribeGroups()
102+
{
103+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
104+
var client = new SendGrid.Client(apiKey);
105+
106+
// GET UNSUBSCRIBE GROUPS
107+
HttpResponseMessage responseGet = client.UnsubscribeGroups.Get().Result;
108+
Console.WriteLine(responseGet.StatusCode);
109+
Console.WriteLine(responseGet.Content.ReadAsStringAsync().Result);
110+
Console.WriteLine("These are your current Unsubscribe Groups. Press any key to continue.");
111+
Console.ReadKey();
112+
113+
// GET A PARTICULAR UNSUBSCRIBE GROUP
114+
int unsubscribeGroupID = 69;
115+
HttpResponseMessage responseGetUnique = client.UnsubscribeGroups.Get(unsubscribeGroupID).Result;
116+
Console.WriteLine(responseGetUnique.StatusCode);
117+
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.ReadKey();
120+
121+
// POST UNSUBSCRIBE GROUP
122+
HttpResponseMessage responsePost = client.UnsubscribeGroups.Post("C Sharp Unsubscribes", "Testing the C Sharp Library", false).Result;
123+
var rawString = responsePost.Content.ReadAsStringAsync().Result;
124+
dynamic jsonObject = JObject.Parse(rawString);
125+
var unsubscribeGroupId = jsonObject.id.ToString();
126+
Console.WriteLine(responsePost.StatusCode);
127+
Console.WriteLine(responsePost.Content.ReadAsStringAsync().Result);
128+
Console.WriteLine("Unsubscribe Group created. Press any key to continue.");
129+
Console.ReadKey();
130+
131+
// DELETE UNSUBSCRIBE GROUP
132+
Console.WriteLine("Deleting Unsubscribe Group, please wait.");
133+
HttpResponseMessage responseDelete = client.UnsubscribeGroups.Delete(unsubscribeGroupId).Result;
134+
Console.WriteLine(responseDelete.StatusCode);
135+
HttpResponseMessage responseFinal = client.UnsubscribeGroups.Get().Result;
136+
Console.WriteLine(responseFinal.StatusCode);
137+
Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result);
138+
Console.WriteLine("Unsubscribe Group Deleted, press any key to end");
139+
Console.ReadKey();
140+
}
100141
}
101142
}

SendGrid/SendGrid/Client.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class Client
1414
{
1515
private string _apiKey;
1616
public APIKeys ApiKeys;
17+
public UnsubscribeGroups UnsubscribeGroups;
1718
public string Version;
1819
private Uri _baseUri;
1920
private const string MediaType = "application/json";
@@ -33,6 +34,7 @@ public Client(string apiKey, string baseUri = "https://api.sendgrid.com/")
3334
_apiKey = apiKey;
3435
Version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
3536
ApiKeys = new APIKeys(this);
37+
UnsubscribeGroups = new UnsubscribeGroups(this);
3638
}
3739

3840
/// <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.0")]
36-
[assembly: AssemblyFileVersion("6.3.0")]
35+
[assembly: AssemblyVersion("6.3.1")]
36+
[assembly: AssemblyFileVersion("6.3.1")]

SendGrid/SendGrid/Resources/APIKeys.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Net.Http;
2-
using System.Runtime.InteropServices;
32
using System.Threading.Tasks;
43
using Newtonsoft.Json.Linq;
54

@@ -53,7 +52,7 @@ public async Task<HttpResponseMessage> Delete(string apiKeyId)
5352
}
5453

5554
/// <summary>
56-
/// Delete a API key
55+
/// Patch a API key
5756
/// </summary>
5857
/// <param name="apiKeyId">ID of the API Key to rename</param>
5958
/// <param name="apiKeyName">New API Key name</param>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System.Net.Http;
2+
using System.Threading.Tasks;
3+
using Newtonsoft.Json.Linq;
4+
5+
namespace SendGrid.Resources
6+
{
7+
public class UnsubscribeGroups
8+
{
9+
private string _endpoint;
10+
private Client _client;
11+
12+
/// <summary>
13+
/// Constructs the SendGrid UnsubscribeGroups object.
14+
/// See https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/groups.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 UnsubscribeGroups(Client client, string endpoint = "v3/asm/groups")
19+
{
20+
_endpoint = endpoint;
21+
_client = client;
22+
}
23+
24+
/// <summary>
25+
/// Retrieve all suppression groups associated with the user.s
26+
/// </summary>
27+
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/groups.html</returns>
28+
public async Task<HttpResponseMessage> Get()
29+
{
30+
return await _client.Get(_endpoint);
31+
}
32+
33+
/// <summary>
34+
/// Retrieve all suppression groups associated with the user.s
35+
/// </summary>
36+
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/groups.html</returns>
37+
public async Task<HttpResponseMessage> Get(int unsubscribeGroupID)
38+
{
39+
string endpoint = _endpoint + "/" + unsubscribeGroupID;
40+
return await _client.Get(endpoint);
41+
}
42+
43+
/// <summary>
44+
/// Create a new suppression group.
45+
/// </summary>
46+
/// <param name="unsubscribeGroupName">The name of the new suppression group</param>
47+
/// <param name="unsubscribeGroupDescription">A description of the suppression group</param>
48+
/// <param name="unsubscribeGroupIsDefault">Default value is false</param>
49+
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/groups.html</returns>
50+
public async Task<HttpResponseMessage> Post(string unsubscribeGroupName,
51+
string unsubscribeGroupDescription,
52+
bool unsubscribeGroupIsDefault)
53+
{
54+
var data = new JObject {{"name", unsubscribeGroupName},
55+
{"description", unsubscribeGroupDescription},
56+
{"is_default", unsubscribeGroupIsDefault}};
57+
return await _client.Post(_endpoint, data);
58+
}
59+
60+
/// <summary>
61+
/// Delete a suppression group.
62+
/// </summary>
63+
/// <param name="unsubscribeGroupId">ID of the suppression group to delete</param>
64+
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/groups.html</returns>
65+
public async Task<HttpResponseMessage> Delete(string unsubscribeGroupId)
66+
{
67+
return await _client.Delete(_endpoint + "/" + unsubscribeGroupId);
68+
}
69+
}
70+
}

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\UnsubscribeGroups.cs" />
6768
<Compile Include="Resources\APIKeys.cs" />
6869
</ItemGroup>
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.0")]
62-
[assembly: AssemblyFileVersion("6.3.0")]
61+
[assembly: AssemblyVersion("6.3.1")]
62+
[assembly: AssemblyFileVersion("6.3.1")]

SendGrid/SendGridMail/Transport/Web.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
using System.Net.Http.Headers;
88
using System.Reflection;
99
using System.Threading.Tasks;
10-
using System.Xml;
11-
using Exceptions;
1210
using SendGrid.SmtpApi;
1311

1412
// ReSharper disable MemberCanBePrivate.Global

0 commit comments

Comments
 (0)