Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strongly typed #211

Closed
wants to merge 58 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
ed82d9f
Add an overloaded RequestAsync to accept JArray and also refactor it …
Jericho Feb 1, 2016
eb5a119
Reformat
Jericho Feb 1, 2016
0b66b5c
Strongly typed GlobalStats
Jericho Feb 2, 2016
40fadc9
Strongly typed GlobalSuppressions
Jericho Feb 3, 2016
4659022
Strongly typed UnsubscribeGroups
Jericho Feb 3, 2016
602a9b4
Strongly typed 'Suppression'
Jericho Feb 4, 2016
fb10162
Add overload Client.Post method to accept a JArray
Jericho Feb 5, 2016
35880a9
Strongly typed Bounces
Jericho Feb 15, 2016
8e5f0d4
Strongly typed custom fields
Jericho Feb 15, 2016
b4e1f67
Display the underlying error message when exception occurs
Jericho Feb 15, 2016
5e6b31c
Added missing reference to system serialization
Jericho Feb 16, 2016
68da11f
Strongly typed Lists
Jericho Feb 16, 2016
3a01e79
Added strongly typed Custom Fields
Jericho Feb 16, 2016
ede9da5
Merge pull request #1 from sendgrid/master
Jericho Feb 17, 2016
be134ae
Add an overloaded RequestAsync to accept JArray and also refactor it …
Jericho Feb 1, 2016
7dcfd55
Reformat
Jericho Feb 1, 2016
b9ffdea
Strongly typed GlobalStats
Jericho Feb 2, 2016
017895a
Strongly typed GlobalSuppressions
Jericho Feb 3, 2016
356dc2f
Strongly typed UnsubscribeGroups
Jericho Feb 3, 2016
3a22e63
Strongly typed 'Suppression'
Jericho Feb 4, 2016
3310daa
Add overload Client.Post method to accept a JArray
Jericho Feb 5, 2016
b15c283
Strongly typed Bounces
Jericho Feb 15, 2016
431ab71
Strongly typed custom fields
Jericho Feb 15, 2016
de04c00
Display the underlying error message when exception occurs
Jericho Feb 15, 2016
0285525
Added missing reference to system serialization
Jericho Feb 16, 2016
8c18087
Strongly typed Lists
Jericho Feb 16, 2016
0f32188
Added strongly typed Custom Fields
Jericho Feb 16, 2016
adc84b8
Merge branch 'strongly_typed' of https://github.com/Jericho/sendgrid-…
Jericho Feb 17, 2016
7134d77
Strongly typed Segments
Jericho Feb 17, 2016
158d23b
Replace 'new JArray(...)' with JArray.FromObject(...)'
Jericho Feb 17, 2016
a41801b
Stringly typed Contacts
Jericho Feb 20, 2016
3e776ea
Strongly typed Templates and Template Versions
Jericho Feb 25, 2016
b4d66aa
Add cancellation token to async methods
Jericho Feb 29, 2016
b5ccd02
Configure the 'Example' project to be built
Jericho Mar 1, 2016
23964af
Add 3 missing files
Jericho Mar 1, 2016
8f3deb4
Merge branch 'strongly_typed' of https://github.com/Jericho/sendgrid-…
Jericho Mar 1, 2016
a59cd03
Get rid of Visual Studio warning: CS4014 Because this call is not awa…
Jericho Mar 1, 2016
d443120
Strongly typed Categories and User
Jericho Mar 1, 2016
595b03a
Merge branch 'strongly_typed' of https://github.com/Jericho/sendgrid-…
Jericho Mar 1, 2016
29dad31
Strongly typed Categories and User
Jericho Mar 1, 2016
9b3b281
Merge branch 'strongly_typed' of https://github.com/Jericho/sendgrid-…
Jericho Mar 1, 2016
b3426fb
ConfigureAwait(false)
Jericho Mar 4, 2016
60bc811
Strongly typed Api Keys
Jericho Mar 18, 2016
f853d70
Add .editorconfig file
Jericho Mar 18, 2016
edd4ca2
Revert changes I did not intend to commit
Jericho Mar 18, 2016
a015d29
Strongly typed campaigns
Jericho Mar 23, 2016
6e00d13
Fix unit tests
Jericho Mar 24, 2016
18d6856
Implement IDisposable interface
Jericho Apr 10, 2016
0448307
Get rid of the 'The method '...' does not need to use async/await" wa…
Jericho May 10, 2016
83cce39
Fix parameters in XML documentation that do not match the name of the…
Jericho May 10, 2016
6fe8609
Fix bug: Segments.GetRecipientsAsync ignores the segmentId
Jericho May 10, 2016
7eeeb4a
Remove redundant empty parentheses
Jericho May 10, 2016
4be377d
Convert fields to readonly
Jericho May 10, 2016
2461764
Remove redundant comma in array initializer
Jericho May 10, 2016
ec39dfa
Fix the SendGridMessage constructor that ignored the 'header' parameter
Jericho May 10, 2016
8d011fe
Merge branch 'strongly_typed' of https://github.com/Jericho/sendgrid-…
Jericho May 10, 2016
6876a8a
Restore Apikeys.cs which was overwritten in the last merge
Jericho May 10, 2016
3fa56fb
Misc
Jericho May 10, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Strongly typed 'Suppression'
  • Loading branch information
Jericho committed Feb 4, 2016
commit 602a9b4751cb8e5b413ec10c4c7136dc9be04c94
75 changes: 33 additions & 42 deletions SendGrid/Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private static void Main()
UseProxy = useFiddler
}
);

// Test sending email
var to = "example@example.com";
var from = "example@example.com";
Expand All @@ -40,7 +40,6 @@ private static void Main()
// Test viewing, creating, modifying and deleting API keys through our v3 Web API
ApiKeys(httpClient);
UnsubscribeGroups(httpClient);
Suppressions(httpClient);
GlobalSuppressions(httpClient);
GlobalStats(httpClient);
}
Expand Down Expand Up @@ -132,20 +131,48 @@ private static void UnsubscribeGroups(HttpClient httpClient)

// CREATE A NEW SUPPRESSION GROUP
var newGroup = client.UnsubscribeGroups.CreateAsync("New group", "This is a new group for testing purposes", false).Result;
Console.WriteLine("Unique ID of the new suppresion group: {0}", newGroup.Id);
Console.WriteLine("Unique ID of the new unsubscribe group: {0}", newGroup.Id);

// UPDATE A SUPPRESSION GROUP
var updatedGroup = client.UnsubscribeGroups.UpdateAsync(newGroup.Id, "This is the updated name").Result;
Console.WriteLine("Suppresion group {0} updated", updatedGroup.Id);
Console.WriteLine("Unsubscribe group {0} updated", updatedGroup.Id);

// GET UNSUBSCRIBE GROUPS
var groups = client.UnsubscribeGroups.GetAllAsync().Result;
Console.WriteLine("There are {0} suppresion groups", groups.Length);
Console.WriteLine("There are {0} unsubscribe groups", groups.Length);

// GET A PARTICULAR UNSUBSCRIBE GROUP
var group = client.UnsubscribeGroups.GetAsync(newGroup.Id).Result;
Console.WriteLine("Retrieved unsubscribe group {0}: {1}", group.Id, group.Name);

// ADD A FEW ADDRESSES TO UNSUBSCRIBE GROUP
client.Suppressions.AddAddressToUnsubscribeGroupAsync(group.Id, "test1@example.com").Wait();
Console.WriteLine("Added test1@example.com to unsubscribe group {0}", group.Id);
client.Suppressions.AddAddressToUnsubscribeGroupAsync(group.Id, "test2@example.com").Wait();
Console.WriteLine("Added test2@example.com to unsubscribe group {0}", group.Id);

// GET THE ADDRESSES IN A GROUP
var unsubscribedAddresses = client.Suppressions.GetUnsubscribedAddressesAsync(group.Id).Result;
Console.WriteLine("There are {0} unsubscribed addresses in group {1}", unsubscribedAddresses.Length, group.Id);

// REMOVE ALL ADDRESSES FROM UNSUBSCRIBE GROUP
foreach (var address in unsubscribedAddresses)
{
client.Suppressions.RemoveAddressFromSuppressionGroupAsync(group.Id, address).Wait();
Console.WriteLine("{0} removed from unsubscribe group {1}", address, group.Id);
}

// MAKE SURE THERE ARE NO ADDRESSES IN THE GROUP
unsubscribedAddresses = client.Suppressions.GetUnsubscribedAddressesAsync(group.Id).Result;
if (unsubscribedAddresses.Length == 0)
{
Console.WriteLine("As expected, there are no more addresses in group {0}", group.Id);
}
else
{
Console.WriteLine("We expected the group {1} to be empty but instead we found {0} unsubscribed addresses.", unsubscribedAddresses.Length, group.Id);
}

// DELETE UNSUBSCRIBE GROUP
client.UnsubscribeGroups.DeleteAsync(newGroup.Id).Wait();
Console.WriteLine("Suppression group {0} deleted", newGroup.Id);
Expand All @@ -154,42 +181,6 @@ private static void UnsubscribeGroups(HttpClient httpClient)
Console.ReadKey();
}

private static void Suppressions(HttpClient httpClient)
{
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 = { "example@example.com", "example2@example.com" };
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() + ".\n\nPress 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, "example@example.com").Result;
Console.WriteLine(responseDelete1.StatusCode);
HttpResponseMessage responseDelete2 = client.Suppressions.Delete(groupID, "example2@example.com").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.\n\nPress any key to end.");
Console.ReadKey();
}

private static void GlobalSuppressions(HttpClient httpClient)
{
Console.WriteLine("\n***** GLOBAL SUPPRESSION *****");
Expand Down
47 changes: 34 additions & 13 deletions SendGrid/SendGrid/Resources/Suppressions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Net.Http;
using Newtonsoft.Json.Linq;
using SendGrid.Utilities;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace SendGrid.Resources
{
Expand All @@ -26,9 +28,27 @@ public Suppressions(Client client, string endpoint = "v3/asm/groups")
/// </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)
public async Task<string[]> GetUnsubscribedAddressesAsync(int groupId)
{
return await _client.Get(_endpoint + "/" + groupId.ToString() + "/suppressions");
var response = await _client.Get(string.Format("{0}/{1}/suppressions", _endpoint, groupId));
response.EnsureSuccess();

var responseContent = await response.Content.ReadAsStringAsync();
var suppressedAddresses = JArray.Parse(responseContent).ToObject<string[]>();
return suppressedAddresses;
}

/// <summary>
/// Add recipient address 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="email">Email address 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 AddAddressToUnsubscribeGroupAsync(int groupId, string email)
{
await AddAddressToUnsubscribeGroupAsync(groupId, new[] { email });
}

/// <summary>
Expand All @@ -37,24 +57,25 @@ public async Task<HttpResponseMessage> Get(int groupId)
/// 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>
/// <param name="emails">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)
public async Task AddAddressToUnsubscribeGroupAsync(int groupId, IEnumerable<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);
var data = new JObject(new JProperty("recipient_emails", new JArray(emails.ToArray())));
var response = await _client.Post(string.Format("{0}/{1}/suppressions", _endpoint, groupId), data);
response.EnsureSuccess();
}

/// <summary>
/// Delete a suppression group.
/// Delete a recipient email from the suppressions list for a group.
/// </summary>
/// <param name="groupId">ID of the suppression group to delete</param>
/// <param name="email">Email address to remove from the suppression group</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)
public async Task RemoveAddressFromSuppressionGroupAsync(int groupId, string email)
{
return await _client.Delete(_endpoint + "/" + groupId.ToString() + "/suppressions/" + email);
var response = await _client.Delete(string.Format("{0}/{1}/suppressions/{2}", _endpoint, groupId, email));
response.EnsureSuccess();
}
}
}