diff options
-rw-r--r-- | SendGrid/Example/Program.cs | 48 | ||||
-rw-r--r-- | SendGrid/SendGrid/Client.cs | 2 | ||||
-rw-r--r-- | SendGrid/SendGrid/Resources/GlobalSuppressions.cs | 57 | ||||
-rw-r--r-- | SendGrid/SendGrid/SendGrid.csproj | 1 | ||||
-rw-r--r-- | SendGrid/UnitTest/UnitTest.cs | 120 |
5 files changed, 188 insertions, 40 deletions
diff --git a/SendGrid/Example/Program.cs b/SendGrid/Example/Program.cs index 753f38c..9b7e359 100644 --- a/SendGrid/Example/Program.cs +++ b/SendGrid/Example/Program.cs @@ -10,6 +10,7 @@ namespace Example {
private static void Main()
{
+ /*
// Test sending email
var to = "example@example.com";
var from = "example@example.com";
@@ -19,6 +20,8 @@ namespace Example ApiKeys();
UnsubscribeGroups();
Suppressions();
+ */
+ GlobalSuppressions();
}
private static void SendAsync(SendGrid.SendGridMessage message)
@@ -95,7 +98,7 @@ namespace Example HttpResponseMessage responseFinal = client.ApiKeys.Get().Result;
Console.WriteLine(responseFinal.StatusCode);
Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result);
- Console.WriteLine("API Key Deleted, press any key to end");
+ Console.WriteLine("API Key Deleted, press any key to end.");
Console.ReadKey();
}
@@ -136,7 +139,7 @@ namespace Example HttpResponseMessage responseFinal = client.UnsubscribeGroups.Get().Result;
Console.WriteLine(responseFinal.StatusCode);
Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result);
- Console.WriteLine("Unsubscribe Group Deleted, press any key to end");
+ Console.WriteLine("Unsubscribe Group Deleted, press any key to end.");
Console.ReadKey();
}
@@ -172,7 +175,46 @@ namespace Example 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. Press any key to end");
+ Console.WriteLine("Emails removed from Suppression Group" + groupID.ToString() + "Deleted. Press any key to end.");
+ Console.ReadKey();
+ }
+
+ private static void GlobalSuppressions()
+ {
+ String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
+ var client = new SendGrid.Client(apiKey);
+
+ // GET SUPPRESSED ADDRESSES FOR A GIVEN GROUP
+ var email = "elmer.thomas+test_global@gmail.com";
+ HttpResponseMessage responseGetUnique = client.GlobalSuppressions.Get(email).Result;
+ Console.WriteLine(responseGetUnique.StatusCode);
+ Console.WriteLine(responseGetUnique.Content.ReadAsStringAsync().Result);
+ Console.WriteLine("Determines if the given email is listed on the Global Suppressions list. Press any key to continue.");
+ Console.ReadKey();
+
+ // ADD EMAILS TO A SUPPRESSION GROUP
+ string[] emails = { "example@example.com", "example2@example.com" };
+ HttpResponseMessage responsePost = client.GlobalSuppressions.Post(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 Global Suppression Group. Press any key to continue.");
+ Console.ReadKey();
+
+ // DELETE EMAILS FROM A SUPPRESSION GROUP
+ Console.WriteLine("Deleting emails from Global Suppression Group, please wait.");
+ HttpResponseMessage responseDelete1 = client.GlobalSuppressions.Delete("example@example.com").Result;
+ Console.WriteLine(responseDelete1.StatusCode);
+ HttpResponseMessage responseDelete2 = client.GlobalSuppressions.Delete("example2@example.com").Result;
+ Console.WriteLine(responseDelete2.StatusCode);
+ HttpResponseMessage responseFinal = client.GlobalSuppressions.Get("example@example.com").Result;
+ Console.WriteLine(responseFinal.StatusCode);
+ Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result);
+ HttpResponseMessage responseFinal2 = client.GlobalSuppressions.Get("example2@example.com").Result;
+ Console.WriteLine(responseFinal2.StatusCode);
+ Console.WriteLine(responseFinal2.Content.ReadAsStringAsync().Result);
+ Console.WriteLine("Emails removed from Global Suppression Group. Press any key to end.");
Console.ReadKey();
}
diff --git a/SendGrid/SendGrid/Client.cs b/SendGrid/SendGrid/Client.cs index fdcaf8b..31d49b5 100644 --- a/SendGrid/SendGrid/Client.cs +++ b/SendGrid/SendGrid/Client.cs @@ -16,6 +16,7 @@ namespace SendGrid public APIKeys ApiKeys; public UnsubscribeGroups UnsubscribeGroups; public Suppressions Suppressions; + public GlobalSuppressions GlobalSuppressions; public string Version; private Uri _baseUri; private const string MediaType = "application/json"; @@ -37,6 +38,7 @@ namespace SendGrid ApiKeys = new APIKeys(this); UnsubscribeGroups = new UnsubscribeGroups(this); Suppressions = new Suppressions(this); + GlobalSuppressions = new GlobalSuppressions(this); } /// <summary> diff --git a/SendGrid/SendGrid/Resources/GlobalSuppressions.cs b/SendGrid/SendGrid/Resources/GlobalSuppressions.cs new file mode 100644 index 0000000..36fcd18 --- /dev/null +++ b/SendGrid/SendGrid/Resources/GlobalSuppressions.cs @@ -0,0 +1,57 @@ +using System.Net.Http; +using System.Threading.Tasks; +using Newtonsoft.Json.Linq; + +namespace SendGrid.Resources +{ + public class GlobalSuppressions + { + private string _endpoint; + private Client _client; + + /// <summary> + /// Constructs the SendGrid Global Suppressions object. + /// See https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/global_suppressions.html + /// </summary> + /// <param name="client">SendGrid Web API v3 client</param> + /// <param name="endpoint">Resource endpoint, do not prepend slash</param> + public GlobalSuppressions(Client client, string endpoint = "v3/asm/suppressions/global") + { + _endpoint = endpoint; + _client = client; + } + + /// <summary> + /// Check if a recipient address is in the global suppressions group. + /// </summary> + /// <param name="email">email address to check</param> + /// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/global_suppressions.html</returns> + public async Task<HttpResponseMessage> Get(string email) + { + return await _client.Get(_endpoint + "/" + email); + } + + /// <summary> + /// Add recipient addresses to the global suppression group. + /// </summary> + /// <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/global_suppressions.html</returns> + public async Task<HttpResponseMessage> Post(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, data); + } + + /// <summary> + /// Delete a recipient email from the global suppressions group. + /// </summary> + /// <param name="email">email address to be removed from the global suppressions group</param> + /// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/global_suppressions.html</returns> + public async Task<HttpResponseMessage> Delete(string email) + { + return await _client.Delete(_endpoint + "/" + email); + } + } +}
\ No newline at end of file diff --git a/SendGrid/SendGrid/SendGrid.csproj b/SendGrid/SendGrid/SendGrid.csproj index 9a57b86..1ba1fb7 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\GlobalSuppressions.cs" /> <Compile Include="Resources\Suppressions.cs" /> <Compile Include="Resources\UnsubscribeGroups.cs" /> <Compile Include="Resources\APIKeys.cs" /> diff --git a/SendGrid/UnitTest/UnitTest.cs b/SendGrid/UnitTest/UnitTest.cs index eab3481..4575e3e 100644 --- a/SendGrid/UnitTest/UnitTest.cs +++ b/SendGrid/UnitTest/UnitTest.cs @@ -155,50 +155,96 @@ namespace UnitTest Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode); } - [TestFixture] - public class Suppressions + } + + [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() { - static string _baseUri = "https://api.sendgrid.com/"; - static string _apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY"); - public Client client = new Client(_apiKey, _baseUri); + int unsubscribeGroupId = 69; - [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"); + } - 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 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 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); + } - private void TestDelete(int unsubscribeGroupId, string email) - { - HttpResponseMessage response = client.Suppressions.Delete(unsubscribeGroupId, email).Result; - Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode); - } + } + + [TestFixture] + public class GlobalSuppressions + { + 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() + { + string email = "example3@example.com"; + + TestGet(email); + string[] emails = { "example1@example.com", "example2@example.com" }; + TestPost(emails); + TestDelete("example1@example.com"); + TestDelete("example2@example.com"); + } + private void TestGet(string email) + { + HttpResponseMessage response = client.GlobalSuppressions.Get(email).Result; + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); + string rawString = response.Content.ReadAsStringAsync().Result; + dynamic jsonObject = JsonConvert.DeserializeObject(rawString); + Assert.IsNotNull(jsonObject); + } + + private void TestPost(string[] emails) + { + HttpResponseMessage response = client.GlobalSuppressions.Post(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(string email) + { + HttpResponseMessage response = client.GlobalSuppressions.Delete(email).Result; + Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode); } } |