summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElmer Thomas <elmer@thinkingserious.com>2015-12-11 13:28:24 -0800
committerElmer Thomas <elmer@thinkingserious.com>2015-12-11 13:28:24 -0800
commit2ac9155968c5c6c8cfea138adc4c00524a5f07f6 (patch)
tree2fa2b4e6f3375ef4c8dc8e93ec93b87803d07e16
parent5391fbe59c8be7e23f4b89786bdca0063d4730d0 (diff)
downloadsendgrid-csharp-2ac9155968c5c6c8cfea138adc4c00524a5f07f6.zip
sendgrid-csharp-2ac9155968c5c6c8cfea138adc4c00524a5f07f6.tar.gz
sendgrid-csharp-2ac9155968c5c6c8cfea138adc4c00524a5f07f6.tar.bz2
Added Suppressions [GET, POST, DELETE]
-rw-r--r--SendGrid/SendGrid/Client.cs2
-rw-r--r--SendGrid/SendGrid/Resources/Suppressions.cs60
-rw-r--r--SendGrid/SendGrid/Resources/UnsubscribeGroups.cs10
-rw-r--r--SendGrid/SendGrid/SendGrid.csproj1
-rw-r--r--SendGrid/UnitTest/UnitTest.cs46
5 files changed, 114 insertions, 5 deletions
diff --git a/SendGrid/SendGrid/Client.cs b/SendGrid/SendGrid/Client.cs
index 0b5383e..fdcaf8b 100644
--- a/SendGrid/SendGrid/Client.cs
+++ b/SendGrid/SendGrid/Client.cs
@@ -15,6 +15,7 @@ namespace SendGrid
private string _apiKey;
public APIKeys ApiKeys;
public UnsubscribeGroups UnsubscribeGroups;
+ public Suppressions Suppressions;
public string Version;
private Uri _baseUri;
private const string MediaType = "application/json";
@@ -35,6 +36,7 @@ namespace SendGrid
Version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
ApiKeys = new APIKeys(this);
UnsubscribeGroups = new UnsubscribeGroups(this);
+ Suppressions = new Suppressions(this);
}
/// <summary>
diff --git a/SendGrid/SendGrid/Resources/Suppressions.cs b/SendGrid/SendGrid/Resources/Suppressions.cs
new file mode 100644
index 0000000..91bb38d
--- /dev/null
+++ b/SendGrid/SendGrid/Resources/Suppressions.cs
@@ -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);
+ }
+ }
+} \ No newline at end of file
diff --git a/SendGrid/SendGrid/Resources/UnsubscribeGroups.cs b/SendGrid/SendGrid/Resources/UnsubscribeGroups.cs
index 17d8ace..db8e4d2 100644
--- a/SendGrid/SendGrid/Resources/UnsubscribeGroups.cs
+++ b/SendGrid/SendGrid/Resources/UnsubscribeGroups.cs
@@ -22,7 +22,7 @@ namespace SendGrid.Resources
}
/// <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()
@@ -31,13 +31,13 @@ namespace SendGrid.Resources
}
/// <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>
diff --git a/SendGrid/SendGrid/SendGrid.csproj b/SendGrid/SendGrid/SendGrid.csproj
index 49ea158..9a57b86 100644
--- a/SendGrid/SendGrid/SendGrid.csproj
+++ b/SendGrid/SendGrid/SendGrid.csproj
@@ -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>
diff --git a/SendGrid/UnitTest/UnitTest.cs b/SendGrid/UnitTest/UnitTest.cs
index 853d59e..eab3481 100644
--- a/SendGrid/UnitTest/UnitTest.cs
+++ b/SendGrid/UnitTest/UnitTest.cs
@@ -155,5 +155,51 @@ namespace UnitTest
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 = { "example@example.com", "example2@example.com" };
+ TestPost(unsubscribeGroupId, emails);
+ TestDelete(unsubscribeGroupId, "example@example.com");
+ TestDelete(unsubscribeGroupId, "example2@example.com");
+ }
+
+ 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);
+ }
+
+ }
+
}
}