From a7e9022866bc437c8d30606371fd9e62c5e014cc Mon Sep 17 00:00:00 2001 From: Carl Hartshorn Date: Thu, 16 Aug 2018 12:16:31 +0100 Subject: [PATCH 1/6] Add Dynamic Template Support --- USE_CASES.md | 139 +++++++++++- src/SendGrid/Helpers/Mail/MailHelper.cs | 111 ++++++++- .../Helpers/Mail/Model/Personalization.cs | 6 + src/SendGrid/Helpers/Mail/SendGridMessage.cs | 108 +++++++++ .../Helpers/Mail/MailHelperTests.cs | 105 +++++++++ tests/SendGrid.Tests/Integration.cs | 213 ++++++++++++++++-- tests/SendGrid.Tests/SendGrid.Tests.csproj | 62 ++--- 7 files changed, 696 insertions(+), 48 deletions(-) create mode 100644 tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs diff --git a/USE_CASES.md b/USE_CASES.md index 06b50e8eb..b183a8c64 100644 --- a/USE_CASES.md +++ b/USE_CASES.md @@ -7,7 +7,8 @@ This documentation provides examples for specific use cases. Please [open an iss * [Email - Send a Single Email to Multiple Recipients](#singleemailmultiplerecipients) * [Email - Send a Single Email to a Single Recipient](#singleemailsinglerecipient) * [Email - Send Multiple Emails to Multiple Recipients](#multipleemailsmultiplerecipients) -* [Email - Transactional Templates](#transactional-templates) +* [Email - Dynamic Transactional Templates](#dynamic-transactional-templates) +* [Email - _Legacy_ Transactional Templates](#transactional-templates) * [Transient Fault Handling](#transient-faults) * [How to Setup a Domain Whitelabel](#domain-whitelabel) * [How to View Email Statistics](#email-stats) @@ -467,8 +468,142 @@ namespace Example } ``` + +# Dynamic Transactional Templates + +For this example, we assume you have created a [dynamic transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/Create_and_edit_dynamic_transactional_templates.html). +Following is the template content we used for testing. + +Template ID (replace with your own): + +```text +d-d42b0eea09964d1ab957c18986c01828 +``` + +Email Subject: + +```text +Dynamic Subject: {{subject}} +``` + +Template Body: + +```html + + + + + +Hello {{name}}, +

+I'm glad you are trying out the dynamic template feature! +

+I hope you are having a great day in {{city}} :) +

+ + +``` + +## With Mail Helper Class + +```csharp +using SendGrid; +using SendGrid.Helpers.Mail; +using System.Threading.Tasks; +using System; + +namespace Example +{ + internal class Example + { + private static void Main() + { + Execute().Wait(); + } + + static async Task Execute() + { + var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY"); + var client = new SendGridClient(apiKey); + var msg = new SendGridMessage(); + msg.SetFrom(new EmailAddress("test@example.com", "Example User")); + msg.AddTo(new EmailAddress("test@example.com", "Example User")); + msg.SetTemplateId("d-d42b0eea09964d1ab957c18986c01828"); + msg.AddDynamicTemplateDataValue("subject", "Hi!"); + msg.AddDynamicTemplateDataValue("name", "Example User"); + msg.AddDynamicTemplateDataValue("city", "Birmingham"); + var response = await client.SendEmailAsync(msg); + Console.WriteLine(response.StatusCode); + Console.WriteLine(response.Headers.ToString()); + Console.WriteLine("\n\nPress any key to exit."); + Console.ReadLine(); + } + } +} +``` + +Methods also exist on `MailHelper` to create dynamic template emails: +* `CreateSingleDynamicTemplateEmail` +* `CreateSingleDynamicTemplateEmailToMultipleRecipients` +* `CreateMultipleDynamicTemplateEmailsToMultipleRecipients` + +## Without Mail Helper Class + +```csharp +using Newtonsoft.Json; +using System.Threading.Tasks; +using System; +using SendGrid; + +namespace Example +{ + internal class Example + { + private static void Main() + { + Execute().Wait(); + } + + static async Task Execute() + { + var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY"); + var client = new SendGridClient(apiKey); + + string data = @"{ + 'personalizations': [ + { + 'to': [ + { + 'email': 'test@example.com' + } + ], + 'dynamic_template_data': { + 'subject': 'Hi!', + 'name': 'Example User', + 'city': 'Birmingham' + } + } + ], + 'from': { + 'email': 'test@example.com' + }, + 'template_id': 'd-d42b0eea09964d1ab957c18986c01828' + }"; + var json = JsonConvert.DeserializeObject(data); + var response = await client.RequestAsync(method: SendGridClient.Method.POST, + requestBody: json.ToString(), + urlPath: "mail/send"); + Console.WriteLine(response.StatusCode); + Console.WriteLine(response.Headers.ToString()); + Console.WriteLine("\n\nPress any key to exit."); + Console.ReadLine(); + } + } +} +``` + -# Transactional Templates +# _Legacy_ Transactional Templates For this example, we assume you have created a [transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html). Following is the template content we used for testing. diff --git a/src/SendGrid/Helpers/Mail/MailHelper.cs b/src/SendGrid/Helpers/Mail/MailHelper.cs index 85a0eaf09..8e230747e 100644 --- a/src/SendGrid/Helpers/Mail/MailHelper.cs +++ b/src/SendGrid/Helpers/Mail/MailHelper.cs @@ -4,7 +4,8 @@ // namespace SendGrid.Helpers.Mail -{ +{ + using System; using System.Collections.Generic; using System.Text.RegularExpressions; @@ -52,6 +53,38 @@ public static SendGridMessage CreateSingleEmail( return msg; } + /// + /// Send a single dynamic template email + /// + /// An email object that may contain the recipient’s name, but must always contain the sender’s email. + /// An email object that may contain the recipient’s name, but must always contain the recipient’s email. + /// The ID of the template. + /// The data with which to populate the dynamic template. + /// A SendGridMessage object. + public static SendGridMessage CreateSingleDynamicTemplateEmail( + EmailAddress from, + EmailAddress to, + string templateId, + Dictionary dynamicTemplateData) + { + if (string.IsNullOrWhiteSpace(templateId)) + { + throw new ArgumentException($"{nameof(templateId)} is required when creating a dynamic template email.", nameof(templateId)); + } + + var msg = new SendGridMessage(); + msg.SetFrom(from); + msg.AddTo(to); + msg.TemplateId = templateId; + + if (dynamicTemplateData != null) + { + msg.AddDynamicTemplateDataValues(dynamicTemplateData); + } + + return msg; + } + /// /// Send a single simple email to multiple recipients /// @@ -89,6 +122,44 @@ public static SendGridMessage CreateSingleEmailToMultipleRecipients( return msg; } + /// + /// Send a single simple email to multiple recipients + /// + /// An email object that may contain the recipient’s name, but must always contain the sender’s email. + /// A list of email objects that may contain the recipient’s name, but must always contain the recipient’s email. + /// The ID of the template. + /// The data with which to populate the dynamic template. + /// A SendGridMessage object. + public static SendGridMessage CreateSingleDynamicTemplateEmailToMultipleRecipients( + EmailAddress from, + List tos, + string templateId, + Dictionary dynamicTemplateData) + { + if (string.IsNullOrWhiteSpace(templateId)) + { + throw new ArgumentException($"{nameof(templateId)} is required when creating a dynamic template email.", nameof(templateId)); + } + + var msg = new SendGridMessage(); + msg.SetFrom(from); + msg.TemplateId = templateId; + + var setDynamicTemplateDataValues = dynamicTemplateData != null; + + for (var i = 0; i < tos.Count; i++) + { + msg.AddTo(tos[i], i); + + if (setDynamicTemplateDataValues) + { + msg.AddDynamicTemplateDataValues(dynamicTemplateData, i); + } + } + + return msg; + } + /// /// Send multiple emails to multiple recipients. /// @@ -129,6 +200,44 @@ public static SendGridMessage CreateMultipleEmailsToMultipleRecipients( return msg; } + /// + /// Send multiple emails to multiple recipients. + /// + /// An email object that may contain the recipient’s name, but must always contain the sender’s email. + /// A list of email objects that may contain the recipient’s name, but must always contain the recipient’s email. + /// The ID of the template. + /// The data with which to populate the dynamic template. + /// A SendGridMessage object. + public static SendGridMessage CreateMultipleDynamicTemplateEmailsToMultipleRecipients( + EmailAddress from, + List tos, + string templateId, + List> dynamicTemplateData) + { + if (string.IsNullOrWhiteSpace(templateId)) + { + throw new ArgumentException($"{nameof(templateId)} is required when creating a dynamic template email.", nameof(templateId)); + } + + var msg = new SendGridMessage(); + msg.SetFrom(from); + msg.TemplateId = templateId; + + var setDynamicTemplateDataValues = dynamicTemplateData != null; + + for (var i = 0; i < tos.Count; i++) + { + msg.AddTo(tos[i], i); + + if (setDynamicTemplateDataValues) + { + msg.AddDynamicTemplateDataValues(dynamicTemplateData[i], i); + } + } + + return msg; + } + /// /// Uncomplex conversion of a "]]> to EmailAddress /// diff --git a/src/SendGrid/Helpers/Mail/Model/Personalization.cs b/src/SendGrid/Helpers/Mail/Model/Personalization.cs index 6eae03840..7d7b79222 100644 --- a/src/SendGrid/Helpers/Mail/Model/Personalization.cs +++ b/src/SendGrid/Helpers/Mail/Model/Personalization.cs @@ -63,5 +63,11 @@ public class Personalization /// [JsonProperty(PropertyName = "send_at")] public long? SendAt { get; set; } + + /// + /// Gets or sets the dynamic template data object following the pattern "dynamic template data key":"dynamic template data value". All are assumed to be strings. These key value pairs will apply to the content of your dynamic template email, in addition to the subject and reply-to parameters. + /// + [JsonProperty(PropertyName = "dynamic_template_data", IsReference = false)] + public Dictionary DynamicTemplateData { get; set; } } } diff --git a/src/SendGrid/Helpers/Mail/SendGridMessage.cs b/src/SendGrid/Helpers/Mail/SendGridMessage.cs index a40935ec4..9b45016f6 100644 --- a/src/SendGrid/Helpers/Mail/SendGridMessage.cs +++ b/src/SendGrid/Helpers/Mail/SendGridMessage.cs @@ -750,6 +750,114 @@ public void AddSubstitutions(Dictionary substitutions, int perso return; } + /// + /// Add a dynamic template data value to the email. + /// + /// The dynamic template data key. + /// The substitution value. + /// Specify the index of the Personalization object where you want to add the substitution. + /// A personalization object to append to the message. + public void AddDynamicTemplateDataValue(string dynamicTemplateDataKey, string dynamicTemplateDataValue, int personalizationIndex = 0, Personalization personalization = null) + { + if (personalization != null) + { + personalization.DynamicTemplateData.Add(dynamicTemplateDataKey, dynamicTemplateDataValue); + if (this.Personalizations == null) + { + this.Personalizations = new List(); + this.Personalizations.Add(personalization); + } + else + { + this.Personalizations.Add(personalization); + } + + return; + } + + if (this.Personalizations != null) + { + if (this.Personalizations[personalizationIndex] == null) + { + var p = new Personalization(); + this.Personalizations.Insert(personalizationIndex, p); + } + + if (this.Personalizations[personalizationIndex].DynamicTemplateData == null) + { + this.Personalizations[personalizationIndex].DynamicTemplateData = new Dictionary(); + } + + this.Personalizations[personalizationIndex].DynamicTemplateData.Add(dynamicTemplateDataKey, dynamicTemplateDataValue); + return; + } + + this.Personalizations = new List() + { + new Personalization() + { + DynamicTemplateData = new Dictionary() + { + { dynamicTemplateDataKey, dynamicTemplateDataValue } + } + } + }; + return; + } + + /// + /// Add dynamic template data to the email. + /// + /// A collection of Dynamic Template Data key value pairs. + /// Specify the index of the Personalization object where you want to add the substitutions. + /// A personalization object to append to the message. + public void AddDynamicTemplateDataValues(Dictionary dynamicTemplateData, int personalizationIndex = 0, Personalization personalization = null) + { + if (personalization != null) + { + personalization.DynamicTemplateData = (personalization.DynamicTemplateData != null) + ? personalization.DynamicTemplateData.Union(dynamicTemplateData).ToDictionary(pair => pair.Key, pair => pair.Value) : dynamicTemplateData; + if (this.Personalizations == null) + { + this.Personalizations = new List(); + this.Personalizations.Add(personalization); + } + else + { + this.Personalizations.Add(personalization); + } + + return; + } + + if (this.Personalizations != null) + { + if (this.Personalizations[personalizationIndex] == null) + { + var p = new Personalization(); + this.Personalizations.Insert(personalizationIndex, p); + } + + if (this.Personalizations[personalizationIndex].DynamicTemplateData == null) + { + this.Personalizations[personalizationIndex].DynamicTemplateData = new Dictionary(); + } + + this.Personalizations[personalizationIndex].DynamicTemplateData = (this.Personalizations[personalizationIndex].DynamicTemplateData != null) + ? this.Personalizations[personalizationIndex].DynamicTemplateData.Union(dynamicTemplateData).ToDictionary(pair => pair.Key, pair => pair.Value) : dynamicTemplateData; + return; + } + + this.Personalizations = new List() + { + new Personalization() + { + DynamicTemplateData = dynamicTemplateData + } + }; + return; + } + /// /// Add a custom argument to the email. /// diff --git a/tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs b/tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs new file mode 100644 index 000000000..588615fff --- /dev/null +++ b/tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs @@ -0,0 +1,105 @@ +using SendGrid.Helpers.Mail; +using System.Collections.Generic; +using System.Linq; +using Xunit; + +namespace SendGrid.Tests.Helpers.Mail +{ + public class MailHelperTests + { + [Theory] + [InlineData("Name Of A Person+", "send@grid.com", "Name Of A Person+ < send@grid.com > ")] + [InlineData("", "send@grid.com", " send@grid.com ")] + [InlineData(null, "notAValidEmail", "notAValidEmail")] + public void TestStringToEmail(string expectedName, string expectedEmail, string rf2822Email) + { + var address = MailHelper.StringToEmailAddress(rf2822Email); + Assert.Equal(expectedEmail, address.Email); + Assert.Equal(expectedName, address.Name); + } + + [Fact] + public void TestCreateSingleDynamicTemplateEmail() + { + var from = new EmailAddress("from@email.com", "FromName"); + var to = new EmailAddress("to@email.com"); + var templateId = "d-template1"; + var dynamicTemplateData = new Dictionary + { + { "key1", "value1" } + }; + + var sendGridMessage = MailHelper.CreateSingleDynamicTemplateEmail( + from, + to, + templateId, + dynamicTemplateData); + + Assert.Equal(from, sendGridMessage.From); + Assert.Equal(to, sendGridMessage.Personalizations.Single().Tos.Single()); + Assert.Equal(templateId, sendGridMessage.TemplateId); + Assert.Equal(dynamicTemplateData, sendGridMessage.Personalizations.Single().DynamicTemplateData); + } + + [Fact] + public void TestCreateSingleDynamicTemplateEmailToMultipleRecipients() + { + var from = new EmailAddress("from@email.com", "FromName"); + var tos = new List + { + new EmailAddress("to1@email.com"), + new EmailAddress("to2@email.com") + }; + + var templateId = "d-template2"; + var dynamicTemplateData = new Dictionary + { + { "key1", "value1" } + }; + + var sendGridMessage = MailHelper.CreateSingleDynamicTemplateEmailToMultipleRecipients( + from, + tos, + templateId, + dynamicTemplateData); + + Assert.Equal(from, sendGridMessage.From); + Assert.Equal(tos[0], sendGridMessage.Personalizations.ElementAt(0).Tos.Single()); + Assert.Equal(tos[1], sendGridMessage.Personalizations.ElementAt(1).Tos.Single()); + Assert.Equal(templateId, sendGridMessage.TemplateId); + Assert.Equal(dynamicTemplateData, sendGridMessage.Personalizations.ElementAt(0).DynamicTemplateData); + Assert.Equal(dynamicTemplateData, sendGridMessage.Personalizations.ElementAt(1).DynamicTemplateData); + } + + [Fact] + public void TestCreateMultipleDynamicTemplateEmailsToMultipleRecipients() + { + var from = new EmailAddress("from@email.com", "FromName"); + var tos = new List + { + new EmailAddress("to1@email.com"), + new EmailAddress("to2@email.com") + }; + + var templateId = "d-template2"; + var dynamicTemplateData = new List> + { + new Dictionary { { "key1", "value1" } }, + new Dictionary { { "key2", "value2" } } + }; + + var sendGridMessage = MailHelper.CreateMultipleDynamicTemplateEmailsToMultipleRecipients( + from, + tos, + templateId, + dynamicTemplateData); + + Assert.Equal(from, sendGridMessage.From); + Assert.Equal(tos[0], sendGridMessage.Personalizations.ElementAt(0).Tos.Single()); + Assert.Equal(tos[1], sendGridMessage.Personalizations.ElementAt(1).Tos.Single()); + Assert.Equal(templateId, sendGridMessage.TemplateId); + Assert.Equal(dynamicTemplateData[0], sendGridMessage.Personalizations.ElementAt(0).DynamicTemplateData); + Assert.Equal(dynamicTemplateData[1], sendGridMessage.Personalizations.ElementAt(1).DynamicTemplateData); + } + } +} diff --git a/tests/SendGrid.Tests/Integration.cs b/tests/SendGrid.Tests/Integration.cs index 89056b2af..31909eb9f 100644 --- a/tests/SendGrid.Tests/Integration.cs +++ b/tests/SendGrid.Tests/Integration.cs @@ -1656,6 +1656,205 @@ public void TestAddSubstitutions() }; msg.AddSubstitutions(substitutions); Assert.True(msg.Serialize() == "{\"personalizations\":[{\"substitutions\":{\"-sub28-\":\"Substituted Value 28\",\"-sub29-\":\"Substituted Value 29\",\"-sub32-\":\"Substituted Value 32\",\"-sub33-\":\"Substituted Value 33\"}},{\"substitutions\":{\"-sub30-\":\"Substituted Value 30\",\"-sub31-\":\"Substituted Value 31\"}}]}"); + } + + [Fact] + public void TestAddDynamicTemplateDataValue() + { + // Personalization not passed in, Personalization does not exist + var msg = new SendGridMessage(); + msg.AddDynamicTemplateDataValue("key1", "Dynamic Template Data Value 1"); + Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key1\":\"Dynamic Template Data Value 1\"}}]}"); + + // Personalization passed in, no Personalizations + msg = new SendGridMessage(); + var personalization = new Personalization() + { + DynamicTemplateData = new Dictionary() + { + { "key2", "Dynamic Template Data Value 2" } + } + }; + msg.AddDynamicTemplateDataValue("key3", "Dynamic Template Data Value 3", 0, personalization); + Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key2\":\"Dynamic Template Data Value 2\",\"key3\":\"Dynamic Template Data Value 3\"}}]}"); + + // Personalization passed in, Personalization exists + msg = new SendGridMessage + { + Personalizations = new List() + { + new Personalization() + { + DynamicTemplateData = new Dictionary() + { + {"key4", "Dynamic Template Data Value 4"} + } + } + } + }; + personalization = new Personalization() + { + DynamicTemplateData = new Dictionary() + { + { "key5", "Dynamic Template Data Value 5" } + } + }; + msg.AddDynamicTemplateDataValue("key6", "Dynamic Template Data Value 6", 1, personalization); + Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key4\":\"Dynamic Template Data Value 4\"}},{\"dynamic_template_data\":{\"key5\":\"Dynamic Template Data Value 5\",\"key6\":\"Dynamic Template Data Value 6\"}}]}"); + + // Personalization not passed in Personalization exists + msg = new SendGridMessage + { + Personalizations = new List() + { + new Personalization() + { + DynamicTemplateData = new Dictionary() + { + {"key7", "Dynamic Template Data Value 7"} + } + } + } + }; + msg.AddDynamicTemplateDataValue("key8", "Dynamic Template Data Value 8"); + Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key7\":\"Dynamic Template Data Value 7\",\"key8\":\"Dynamic Template Data Value 8\"}}]}"); + + // Personalization not passed in Personalizations exists + msg = new SendGridMessage + { + Personalizations = new List() + { + new Personalization() + { + DynamicTemplateData = new Dictionary() + { + {"key9", "Dynamic Template Data Value 9"} + } + } + } + }; + personalization = new Personalization() + { + DynamicTemplateData = new Dictionary() + { + { "key10", "Dynamic Template Data Value 10" } + } + }; + msg.Personalizations.Add(personalization); + msg.AddDynamicTemplateDataValue("key11", "Dynamic Template Data Value 11"); + Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key9\":\"Dynamic Template Data Value 9\",\"key11\":\"Dynamic Template Data Value 11\"}},{\"dynamic_template_data\":{\"key10\":\"Dynamic Template Data Value 10\"}}]}"); + } + + [Fact] + public void TestAddDynamicTemplateDataValues() + { + // Personalization not passed in, Personalization does not exist + var msg = new SendGridMessage(); + var dynamicTemplateDataValues = new Dictionary + { + {"key12", "Dynamic Template Data Value 12"}, + {"key13", "Dynamic Template Data Value 13"} + }; + msg.AddDynamicTemplateDataValues(dynamicTemplateDataValues); + Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key12\":\"Dynamic Template Data Value 12\",\"key13\":\"Dynamic Template Data Value 13\"}}]}"); + + // Personalization passed in, no Personalizations + msg = new SendGridMessage(); + dynamicTemplateDataValues = new Dictionary + { + {"key14", "Dynamic Template Data Value 14"}, + {"key15", "Dynamic Template Data Value 15"} + }; + var personalization = new Personalization() + { + DynamicTemplateData = dynamicTemplateDataValues + }; + dynamicTemplateDataValues = new Dictionary + { + {"key16", "Dynamic Template Data Value 16"}, + {"key17", "Dynamic Template Data Value 17"} + }; + msg.AddDynamicTemplateDataValues(dynamicTemplateDataValues, 0, personalization); + Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key14\":\"Dynamic Template Data Value 14\",\"key15\":\"Dynamic Template Data Value 15\",\"key16\":\"Dynamic Template Data Value 16\",\"key17\":\"Dynamic Template Data Value 17\"}}]}"); + + // Personalization passed in, Personalization exists + msg = new SendGridMessage(); + dynamicTemplateDataValues = new Dictionary + { + {"key18", "Dynamic Template Data Value 18"}, + {"key19", "Dynamic Template Data Value 19"} + }; + msg.Personalizations = new List() { + new Personalization() { + DynamicTemplateData = dynamicTemplateDataValues + } + }; + dynamicTemplateDataValues = new Dictionary + { + {"key20", "Dynamic Template Data Value 20"}, + {"key21", "Dynamic Template Data Value 21"} + }; + personalization = new Personalization() + { + DynamicTemplateData = dynamicTemplateDataValues + }; + dynamicTemplateDataValues = new Dictionary + { + {"key22", "Dynamic Template Data Value 22"}, + {"key23", "Dynamic Template Data Value 23"} + }; + msg.AddDynamicTemplateDataValues(dynamicTemplateDataValues, 1, personalization); + Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key18\":\"Dynamic Template Data Value 18\",\"key19\":\"Dynamic Template Data Value 19\"}},{\"dynamic_template_data\":{\"key20\":\"Dynamic Template Data Value 20\",\"key21\":\"Dynamic Template Data Value 21\",\"key22\":\"Dynamic Template Data Value 22\",\"key23\":\"Dynamic Template Data Value 23\"}}]}"); + + // Personalization not passed in Personalization exists + msg = new SendGridMessage(); + dynamicTemplateDataValues = new Dictionary + { + {"key24", "Dynamic Template Data Value 24"}, + {"key25", "Dynamic Template Data Value 25"} + }; + msg.Personalizations = new List() { + new Personalization() { + DynamicTemplateData = dynamicTemplateDataValues + } + }; + dynamicTemplateDataValues = new Dictionary + { + {"key26", "Dynamic Template Data Value 26"}, + {"key27", "Dynamic Template Data Value 27"} + }; + msg.AddDynamicTemplateDataValues(dynamicTemplateDataValues); + Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key24\":\"Dynamic Template Data Value 24\",\"key25\":\"Dynamic Template Data Value 25\",\"key26\":\"Dynamic Template Data Value 26\",\"key27\":\"Dynamic Template Data Value 27\"}}]}"); + + // Personalization not passed in Personalizations exists + msg = new SendGridMessage(); + dynamicTemplateDataValues = new Dictionary + { + {"key28", "Dynamic Template Data Value 28"}, + {"key29", "Dynamic Template Data Value 29"} + }; + msg.Personalizations = new List() { + new Personalization() { + DynamicTemplateData = dynamicTemplateDataValues + } + }; + dynamicTemplateDataValues = new Dictionary + { + {"key30", "Dynamic Template Data Value 30"}, + {"key31", "Dynamic Template Data Value 31"} + }; + personalization = new Personalization() + { + DynamicTemplateData = dynamicTemplateDataValues + }; + msg.Personalizations.Add(personalization); + dynamicTemplateDataValues = new Dictionary + { + {"key32", "Dynamic Template Data Value 32"}, + {"key33", "Dynamic Template Data Value 33"} + }; + msg.AddDynamicTemplateDataValues(dynamicTemplateDataValues); + Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key28\":\"Dynamic Template Data Value 28\",\"key29\":\"Dynamic Template Data Value 29\",\"key32\":\"Dynamic Template Data Value 32\",\"key33\":\"Dynamic Template Data Value 33\"}},{\"dynamic_template_data\":{\"key30\":\"Dynamic Template Data Value 30\",\"key31\":\"Dynamic Template Data Value 31\"}}]}"); } [Fact] @@ -6075,18 +6274,4 @@ public override HttpResponseMessage Send(HttpRequestMessage request) throw new TimeoutException(exceptionMessage); } } - - public class MailHelperTests - { - [Theory] - [InlineData("Name Of A Person+", "send@grid.com", "Name Of A Person+ < send@grid.com > ")] - [InlineData("", "send@grid.com", " send@grid.com ")] - [InlineData(null, "notAValidEmail", "notAValidEmail")] - public void TestStringToEmail(string expectedName, string expectedEmail, string rf2822Email) - { - var address = MailHelper.StringToEmailAddress(rf2822Email); - Assert.Equal(expectedEmail, address.Email); - Assert.Equal(expectedName, address.Name); - } - } } diff --git a/tests/SendGrid.Tests/SendGrid.Tests.csproj b/tests/SendGrid.Tests/SendGrid.Tests.csproj index bb33bee10..6ba268cc7 100644 --- a/tests/SendGrid.Tests/SendGrid.Tests.csproj +++ b/tests/SendGrid.Tests/SendGrid.Tests.csproj @@ -1,31 +1,31 @@ - - - - netcoreapp1.0 - SendGrid.Tests - SendGrid.Tests - true - 1.0.4 - false - false - false - - - - - - - - - - - - - - - - - - - - + + + + netcoreapp1.0 + SendGrid.Tests + SendGrid.Tests + true + 1.0.4 + false + false + false + + + + + + + + + + + + + + + + + + + + From 312e218172d9f0a0622f24158ea4bd1d28bc2b0b Mon Sep 17 00:00:00 2001 From: Carl Hartshorn Date: Tue, 21 Aug 2018 15:11:01 +0100 Subject: [PATCH 2/6] Update Personalization.DynamicTemplateData to Dictionary --- src/SendGrid/Helpers/Mail/MailHelper.cs | 6 ++-- .../Helpers/Mail/Model/Personalization.cs | 2 +- src/SendGrid/Helpers/Mail/SendGridMessage.cs | 8 ++--- .../Helpers/Mail/MailHelperTests.cs | 10 +++--- tests/SendGrid.Tests/Integration.cs | 34 +++++++++---------- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/SendGrid/Helpers/Mail/MailHelper.cs b/src/SendGrid/Helpers/Mail/MailHelper.cs index 8e230747e..552f09e35 100644 --- a/src/SendGrid/Helpers/Mail/MailHelper.cs +++ b/src/SendGrid/Helpers/Mail/MailHelper.cs @@ -65,7 +65,7 @@ public static SendGridMessage CreateSingleDynamicTemplateEmail( EmailAddress from, EmailAddress to, string templateId, - Dictionary dynamicTemplateData) + Dictionary dynamicTemplateData) { if (string.IsNullOrWhiteSpace(templateId)) { @@ -134,7 +134,7 @@ public static SendGridMessage CreateSingleDynamicTemplateEmailToMultipleRecipien EmailAddress from, List tos, string templateId, - Dictionary dynamicTemplateData) + Dictionary dynamicTemplateData) { if (string.IsNullOrWhiteSpace(templateId)) { @@ -212,7 +212,7 @@ public static SendGridMessage CreateMultipleDynamicTemplateEmailsToMultipleRecip EmailAddress from, List tos, string templateId, - List> dynamicTemplateData) + List> dynamicTemplateData) { if (string.IsNullOrWhiteSpace(templateId)) { diff --git a/src/SendGrid/Helpers/Mail/Model/Personalization.cs b/src/SendGrid/Helpers/Mail/Model/Personalization.cs index 7d7b79222..20aecb01f 100644 --- a/src/SendGrid/Helpers/Mail/Model/Personalization.cs +++ b/src/SendGrid/Helpers/Mail/Model/Personalization.cs @@ -68,6 +68,6 @@ public class Personalization /// Gets or sets the dynamic template data object following the pattern "dynamic template data key":"dynamic template data value". All are assumed to be strings. These key value pairs will apply to the content of your dynamic template email, in addition to the subject and reply-to parameters. /// [JsonProperty(PropertyName = "dynamic_template_data", IsReference = false)] - public Dictionary DynamicTemplateData { get; set; } + public Dictionary DynamicTemplateData { get; set; } } } diff --git a/src/SendGrid/Helpers/Mail/SendGridMessage.cs b/src/SendGrid/Helpers/Mail/SendGridMessage.cs index 9b45016f6..f7e9ee161 100644 --- a/src/SendGrid/Helpers/Mail/SendGridMessage.cs +++ b/src/SendGrid/Helpers/Mail/SendGridMessage.cs @@ -785,7 +785,7 @@ public void AddDynamicTemplateDataValue(string dynamicTemplateDataKey, string dy if (this.Personalizations[personalizationIndex].DynamicTemplateData == null) { - this.Personalizations[personalizationIndex].DynamicTemplateData = new Dictionary(); + this.Personalizations[personalizationIndex].DynamicTemplateData = new Dictionary(); } this.Personalizations[personalizationIndex].DynamicTemplateData.Add(dynamicTemplateDataKey, dynamicTemplateDataValue); @@ -796,7 +796,7 @@ public void AddDynamicTemplateDataValue(string dynamicTemplateDataKey, string dy { new Personalization() { - DynamicTemplateData = new Dictionary() + DynamicTemplateData = new Dictionary() { { dynamicTemplateDataKey, dynamicTemplateDataValue } } @@ -811,7 +811,7 @@ public void AddDynamicTemplateDataValue(string dynamicTemplateDataKey, string dy /// A collection of Dynamic Template Data key value pairs. /// Specify the index of the Personalization object where you want to add the substitutions. /// A personalization object to append to the message. - public void AddDynamicTemplateDataValues(Dictionary dynamicTemplateData, int personalizationIndex = 0, Personalization personalization = null) + public void AddDynamicTemplateDataValues(Dictionary dynamicTemplateData, int personalizationIndex = 0, Personalization personalization = null) { if (personalization != null) { @@ -840,7 +840,7 @@ public void AddDynamicTemplateDataValues(Dictionary dynamicTempl if (this.Personalizations[personalizationIndex].DynamicTemplateData == null) { - this.Personalizations[personalizationIndex].DynamicTemplateData = new Dictionary(); + this.Personalizations[personalizationIndex].DynamicTemplateData = new Dictionary(); } this.Personalizations[personalizationIndex].DynamicTemplateData = (this.Personalizations[personalizationIndex].DynamicTemplateData != null) diff --git a/tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs b/tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs index 588615fff..daf322ef0 100644 --- a/tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs +++ b/tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs @@ -24,7 +24,7 @@ public void TestCreateSingleDynamicTemplateEmail() var from = new EmailAddress("from@email.com", "FromName"); var to = new EmailAddress("to@email.com"); var templateId = "d-template1"; - var dynamicTemplateData = new Dictionary + var dynamicTemplateData = new Dictionary { { "key1", "value1" } }; @@ -52,7 +52,7 @@ public void TestCreateSingleDynamicTemplateEmailToMultipleRecipients() }; var templateId = "d-template2"; - var dynamicTemplateData = new Dictionary + var dynamicTemplateData = new Dictionary { { "key1", "value1" } }; @@ -82,10 +82,10 @@ public void TestCreateMultipleDynamicTemplateEmailsToMultipleRecipients() }; var templateId = "d-template2"; - var dynamicTemplateData = new List> + var dynamicTemplateData = new List> { - new Dictionary { { "key1", "value1" } }, - new Dictionary { { "key2", "value2" } } + new Dictionary { { "key1", "value1" } }, + new Dictionary { { "key2", "value2" } } }; var sendGridMessage = MailHelper.CreateMultipleDynamicTemplateEmailsToMultipleRecipients( diff --git a/tests/SendGrid.Tests/Integration.cs b/tests/SendGrid.Tests/Integration.cs index 31909eb9f..255315fa5 100644 --- a/tests/SendGrid.Tests/Integration.cs +++ b/tests/SendGrid.Tests/Integration.cs @@ -1670,7 +1670,7 @@ public void TestAddDynamicTemplateDataValue() msg = new SendGridMessage(); var personalization = new Personalization() { - DynamicTemplateData = new Dictionary() + DynamicTemplateData = new Dictionary() { { "key2", "Dynamic Template Data Value 2" } } @@ -1685,7 +1685,7 @@ public void TestAddDynamicTemplateDataValue() { new Personalization() { - DynamicTemplateData = new Dictionary() + DynamicTemplateData = new Dictionary() { {"key4", "Dynamic Template Data Value 4"} } @@ -1694,7 +1694,7 @@ public void TestAddDynamicTemplateDataValue() }; personalization = new Personalization() { - DynamicTemplateData = new Dictionary() + DynamicTemplateData = new Dictionary() { { "key5", "Dynamic Template Data Value 5" } } @@ -1709,7 +1709,7 @@ public void TestAddDynamicTemplateDataValue() { new Personalization() { - DynamicTemplateData = new Dictionary() + DynamicTemplateData = new Dictionary() { {"key7", "Dynamic Template Data Value 7"} } @@ -1726,7 +1726,7 @@ public void TestAddDynamicTemplateDataValue() { new Personalization() { - DynamicTemplateData = new Dictionary() + DynamicTemplateData = new Dictionary() { {"key9", "Dynamic Template Data Value 9"} } @@ -1735,7 +1735,7 @@ public void TestAddDynamicTemplateDataValue() }; personalization = new Personalization() { - DynamicTemplateData = new Dictionary() + DynamicTemplateData = new Dictionary() { { "key10", "Dynamic Template Data Value 10" } } @@ -1750,7 +1750,7 @@ public void TestAddDynamicTemplateDataValues() { // Personalization not passed in, Personalization does not exist var msg = new SendGridMessage(); - var dynamicTemplateDataValues = new Dictionary + var dynamicTemplateDataValues = new Dictionary { {"key12", "Dynamic Template Data Value 12"}, {"key13", "Dynamic Template Data Value 13"} @@ -1760,7 +1760,7 @@ public void TestAddDynamicTemplateDataValues() // Personalization passed in, no Personalizations msg = new SendGridMessage(); - dynamicTemplateDataValues = new Dictionary + dynamicTemplateDataValues = new Dictionary { {"key14", "Dynamic Template Data Value 14"}, {"key15", "Dynamic Template Data Value 15"} @@ -1769,7 +1769,7 @@ public void TestAddDynamicTemplateDataValues() { DynamicTemplateData = dynamicTemplateDataValues }; - dynamicTemplateDataValues = new Dictionary + dynamicTemplateDataValues = new Dictionary { {"key16", "Dynamic Template Data Value 16"}, {"key17", "Dynamic Template Data Value 17"} @@ -1779,7 +1779,7 @@ public void TestAddDynamicTemplateDataValues() // Personalization passed in, Personalization exists msg = new SendGridMessage(); - dynamicTemplateDataValues = new Dictionary + dynamicTemplateDataValues = new Dictionary { {"key18", "Dynamic Template Data Value 18"}, {"key19", "Dynamic Template Data Value 19"} @@ -1789,7 +1789,7 @@ public void TestAddDynamicTemplateDataValues() DynamicTemplateData = dynamicTemplateDataValues } }; - dynamicTemplateDataValues = new Dictionary + dynamicTemplateDataValues = new Dictionary { {"key20", "Dynamic Template Data Value 20"}, {"key21", "Dynamic Template Data Value 21"} @@ -1798,7 +1798,7 @@ public void TestAddDynamicTemplateDataValues() { DynamicTemplateData = dynamicTemplateDataValues }; - dynamicTemplateDataValues = new Dictionary + dynamicTemplateDataValues = new Dictionary { {"key22", "Dynamic Template Data Value 22"}, {"key23", "Dynamic Template Data Value 23"} @@ -1808,7 +1808,7 @@ public void TestAddDynamicTemplateDataValues() // Personalization not passed in Personalization exists msg = new SendGridMessage(); - dynamicTemplateDataValues = new Dictionary + dynamicTemplateDataValues = new Dictionary { {"key24", "Dynamic Template Data Value 24"}, {"key25", "Dynamic Template Data Value 25"} @@ -1818,7 +1818,7 @@ public void TestAddDynamicTemplateDataValues() DynamicTemplateData = dynamicTemplateDataValues } }; - dynamicTemplateDataValues = new Dictionary + dynamicTemplateDataValues = new Dictionary { {"key26", "Dynamic Template Data Value 26"}, {"key27", "Dynamic Template Data Value 27"} @@ -1828,7 +1828,7 @@ public void TestAddDynamicTemplateDataValues() // Personalization not passed in Personalizations exists msg = new SendGridMessage(); - dynamicTemplateDataValues = new Dictionary + dynamicTemplateDataValues = new Dictionary { {"key28", "Dynamic Template Data Value 28"}, {"key29", "Dynamic Template Data Value 29"} @@ -1838,7 +1838,7 @@ public void TestAddDynamicTemplateDataValues() DynamicTemplateData = dynamicTemplateDataValues } }; - dynamicTemplateDataValues = new Dictionary + dynamicTemplateDataValues = new Dictionary { {"key30", "Dynamic Template Data Value 30"}, {"key31", "Dynamic Template Data Value 31"} @@ -1848,7 +1848,7 @@ public void TestAddDynamicTemplateDataValues() DynamicTemplateData = dynamicTemplateDataValues }; msg.Personalizations.Add(personalization); - dynamicTemplateDataValues = new Dictionary + dynamicTemplateDataValues = new Dictionary { {"key32", "Dynamic Template Data Value 32"}, {"key33", "Dynamic Template Data Value 33"} From 7eafad1a5af281ea24298f6dd9032dbcad74be8e Mon Sep 17 00:00:00 2001 From: Carl Hartshorn Date: Tue, 21 Aug 2018 16:09:04 +0100 Subject: [PATCH 3/6] Update Personalization.DynamicTemplateData to be an object --- USE_CASES.md | 45 +++- src/SendGrid/Helpers/Mail/MailHelper.cs | 12 +- .../Helpers/Mail/Model/Personalization.cs | 2 +- src/SendGrid/Helpers/Mail/SendGridMessage.cs | 65 +----- .../DynamicTemplateDataSerialisationTests.cs | 40 ++++ .../Helpers/Mail/MailHelperTests.cs | 6 +- tests/SendGrid.Tests/Integration.cs | 202 ++++++------------ 7 files changed, 160 insertions(+), 212 deletions(-) create mode 100644 tests/SendGrid.Tests/DynamicTemplateDataSerialisationTests.cs diff --git a/USE_CASES.md b/USE_CASES.md index b183a8c64..4752d2d51 100644 --- a/USE_CASES.md +++ b/USE_CASES.md @@ -498,7 +498,7 @@ Hello {{name}},

I'm glad you are trying out the dynamic template feature!

-I hope you are having a great day in {{city}} :) +I hope you are having a great day in {{location.city}} :)

@@ -507,6 +507,7 @@ I hope you are having a great day in {{city}} :) ## With Mail Helper Class ```csharp +using Newtonsoft.Json; using SendGrid; using SendGrid.Helpers.Mail; using System.Threading.Tasks; @@ -529,15 +530,46 @@ namespace Example msg.SetFrom(new EmailAddress("test@example.com", "Example User")); msg.AddTo(new EmailAddress("test@example.com", "Example User")); msg.SetTemplateId("d-d42b0eea09964d1ab957c18986c01828"); - msg.AddDynamicTemplateDataValue("subject", "Hi!"); - msg.AddDynamicTemplateDataValue("name", "Example User"); - msg.AddDynamicTemplateDataValue("city", "Birmingham"); + + var dynamicTemplateData = new ExampleDynamicTemplateData + { + Subject = "Hi!", + Name = "Example User", + Location = new Location + { + City = "Birmingham", + Country = "United Kingdom" + } + }; + + msg.SetDynamicTemplateData(dynamicTemplateData); var response = await client.SendEmailAsync(msg); Console.WriteLine(response.StatusCode); Console.WriteLine(response.Headers.ToString()); Console.WriteLine("\n\nPress any key to exit."); Console.ReadLine(); } + + private class ExampleDynamicTemplateData + { + [JsonProperty("subject")] + public string Subject { get; set; } + + [JsonProperty("name")] + public string Name { get; set; } + + [JsonProperty("location")] + public Location Location { get; set; } + } + + private class Location + { + [JsonProperty("city")] + public string City { get; set; } + + [JsonProperty("country")] + public string Country { get; set; } + } } } ``` @@ -580,7 +612,10 @@ namespace Example 'dynamic_template_data': { 'subject': 'Hi!', 'name': 'Example User', - 'city': 'Birmingham' + 'location': { + 'city': 'Birmingham', + 'country': 'United Kingdom' + } } } ], diff --git a/src/SendGrid/Helpers/Mail/MailHelper.cs b/src/SendGrid/Helpers/Mail/MailHelper.cs index 552f09e35..0e525cce6 100644 --- a/src/SendGrid/Helpers/Mail/MailHelper.cs +++ b/src/SendGrid/Helpers/Mail/MailHelper.cs @@ -65,7 +65,7 @@ public static SendGridMessage CreateSingleDynamicTemplateEmail( EmailAddress from, EmailAddress to, string templateId, - Dictionary dynamicTemplateData) + object dynamicTemplateData) { if (string.IsNullOrWhiteSpace(templateId)) { @@ -79,7 +79,7 @@ public static SendGridMessage CreateSingleDynamicTemplateEmail( if (dynamicTemplateData != null) { - msg.AddDynamicTemplateDataValues(dynamicTemplateData); + msg.SetDynamicTemplateData(dynamicTemplateData); } return msg; @@ -134,7 +134,7 @@ public static SendGridMessage CreateSingleDynamicTemplateEmailToMultipleRecipien EmailAddress from, List tos, string templateId, - Dictionary dynamicTemplateData) + object dynamicTemplateData) { if (string.IsNullOrWhiteSpace(templateId)) { @@ -153,7 +153,7 @@ public static SendGridMessage CreateSingleDynamicTemplateEmailToMultipleRecipien if (setDynamicTemplateDataValues) { - msg.AddDynamicTemplateDataValues(dynamicTemplateData, i); + msg.SetDynamicTemplateData(dynamicTemplateData, i); } } @@ -212,7 +212,7 @@ public static SendGridMessage CreateMultipleDynamicTemplateEmailsToMultipleRecip EmailAddress from, List tos, string templateId, - List> dynamicTemplateData) + List dynamicTemplateData) { if (string.IsNullOrWhiteSpace(templateId)) { @@ -231,7 +231,7 @@ public static SendGridMessage CreateMultipleDynamicTemplateEmailsToMultipleRecip if (setDynamicTemplateDataValues) { - msg.AddDynamicTemplateDataValues(dynamicTemplateData[i], i); + msg.SetDynamicTemplateData(dynamicTemplateData[i], i); } } diff --git a/src/SendGrid/Helpers/Mail/Model/Personalization.cs b/src/SendGrid/Helpers/Mail/Model/Personalization.cs index 20aecb01f..5a03205d7 100644 --- a/src/SendGrid/Helpers/Mail/Model/Personalization.cs +++ b/src/SendGrid/Helpers/Mail/Model/Personalization.cs @@ -68,6 +68,6 @@ public class Personalization /// Gets or sets the dynamic template data object following the pattern "dynamic template data key":"dynamic template data value". All are assumed to be strings. These key value pairs will apply to the content of your dynamic template email, in addition to the subject and reply-to parameters. /// [JsonProperty(PropertyName = "dynamic_template_data", IsReference = false)] - public Dictionary DynamicTemplateData { get; set; } + public object DynamicTemplateData { get; set; } } } diff --git a/src/SendGrid/Helpers/Mail/SendGridMessage.cs b/src/SendGrid/Helpers/Mail/SendGridMessage.cs index f7e9ee161..1f0b53141 100644 --- a/src/SendGrid/Helpers/Mail/SendGridMessage.cs +++ b/src/SendGrid/Helpers/Mail/SendGridMessage.cs @@ -750,73 +750,17 @@ public void AddSubstitutions(Dictionary substitutions, int perso return; } - /// - /// Add a dynamic template data value to the email. - /// - /// The dynamic template data key. - /// The substitution value. - /// Specify the index of the Personalization object where you want to add the substitution. - /// A personalization object to append to the message. - public void AddDynamicTemplateDataValue(string dynamicTemplateDataKey, string dynamicTemplateDataValue, int personalizationIndex = 0, Personalization personalization = null) - { - if (personalization != null) - { - personalization.DynamicTemplateData.Add(dynamicTemplateDataKey, dynamicTemplateDataValue); - if (this.Personalizations == null) - { - this.Personalizations = new List(); - this.Personalizations.Add(personalization); - } - else - { - this.Personalizations.Add(personalization); - } - - return; - } - - if (this.Personalizations != null) - { - if (this.Personalizations[personalizationIndex] == null) - { - var p = new Personalization(); - this.Personalizations.Insert(personalizationIndex, p); - } - - if (this.Personalizations[personalizationIndex].DynamicTemplateData == null) - { - this.Personalizations[personalizationIndex].DynamicTemplateData = new Dictionary(); - } - - this.Personalizations[personalizationIndex].DynamicTemplateData.Add(dynamicTemplateDataKey, dynamicTemplateDataValue); - return; - } - - this.Personalizations = new List() - { - new Personalization() - { - DynamicTemplateData = new Dictionary() - { - { dynamicTemplateDataKey, dynamicTemplateDataValue } - } - } - }; - return; - } - /// /// Add dynamic template data to the email. /// - /// A collection of Dynamic Template Data key value pairs. + /// A Dynamic Template Data object. /// Specify the index of the Personalization object where you want to add the substitutions. /// A personalization object to append to the message. - public void AddDynamicTemplateDataValues(Dictionary dynamicTemplateData, int personalizationIndex = 0, Personalization personalization = null) + public void SetDynamicTemplateData(object dynamicTemplateData, int personalizationIndex = 0, Personalization personalization = null) { if (personalization != null) { - personalization.DynamicTemplateData = (personalization.DynamicTemplateData != null) - ? personalization.DynamicTemplateData.Union(dynamicTemplateData).ToDictionary(pair => pair.Key, pair => pair.Value) : dynamicTemplateData; + personalization.DynamicTemplateData = dynamicTemplateData; if (this.Personalizations == null) { this.Personalizations = new List(); @@ -843,8 +787,7 @@ public void AddDynamicTemplateDataValues(Dictionary dynamicTempl this.Personalizations[personalizationIndex].DynamicTemplateData = new Dictionary(); } - this.Personalizations[personalizationIndex].DynamicTemplateData = (this.Personalizations[personalizationIndex].DynamicTemplateData != null) - ? this.Personalizations[personalizationIndex].DynamicTemplateData.Union(dynamicTemplateData).ToDictionary(pair => pair.Key, pair => pair.Value) : dynamicTemplateData; + this.Personalizations[personalizationIndex].DynamicTemplateData = dynamicTemplateData; return; } diff --git a/tests/SendGrid.Tests/DynamicTemplateDataSerialisationTests.cs b/tests/SendGrid.Tests/DynamicTemplateDataSerialisationTests.cs new file mode 100644 index 000000000..6f9924506 --- /dev/null +++ b/tests/SendGrid.Tests/DynamicTemplateDataSerialisationTests.cs @@ -0,0 +1,40 @@ +using Newtonsoft.Json; +using SendGrid.Helpers.Mail; +using Xunit; + +namespace SendGrid.Tests +{ + public class DynamicTemplateDataSerialisationTests + { + [Fact] + public void TestSetDynamicTemplateDataWorksWithSpecifiedJsonPropertyNames() + { + var msg = new SendGridMessage(); + + var dynamicTemplateData = new TestDynamicTemplateData + { + MyCamelCaseProperty = "camelCase", + MyKebabCaseProperty = "kebab-case", + MyPascalCaseProperty = "PascalCase", + MySnakeCaseProperty = "snake_case", + }; + + msg.SetDynamicTemplateData(dynamicTemplateData); + Assert.Equal("{\"personalizations\":[{\"dynamic_template_data\":{\"myCamelCaseProperty\":\"camelCase\",\"my-kebab-case-property\":\"kebab-case\",\"MyPascalCaseProperty\":\"PascalCase\",\"my_snake_case_property\":\"snake_case\"}}]}", msg.Serialize()); + } + + private class TestDynamicTemplateData + { + [JsonProperty("myCamelCaseProperty")] + public string MyCamelCaseProperty { get; set; } + + [JsonProperty("my-kebab-case-property")] + public string MyKebabCaseProperty { get; set; } + + public string MyPascalCaseProperty { get; set; } + + [JsonProperty("my_snake_case_property")] + public string MySnakeCaseProperty { get; set; } + } + } +} diff --git a/tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs b/tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs index daf322ef0..fb5a7d81f 100644 --- a/tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs +++ b/tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs @@ -82,10 +82,10 @@ public void TestCreateMultipleDynamicTemplateEmailsToMultipleRecipients() }; var templateId = "d-template2"; - var dynamicTemplateData = new List> + var dynamicTemplateData = new List { - new Dictionary { { "key1", "value1" } }, - new Dictionary { { "key2", "value2" } } + new { key1 = "value1" }, + new { key2 = "value2" } }; var sendGridMessage = MailHelper.CreateMultipleDynamicTemplateEmailsToMultipleRecipients( diff --git a/tests/SendGrid.Tests/Integration.cs b/tests/SendGrid.Tests/Integration.cs index 255315fa5..746859e57 100644 --- a/tests/SendGrid.Tests/Integration.cs +++ b/tests/SendGrid.Tests/Integration.cs @@ -1656,205 +1656,135 @@ public void TestAddSubstitutions() }; msg.AddSubstitutions(substitutions); Assert.True(msg.Serialize() == "{\"personalizations\":[{\"substitutions\":{\"-sub28-\":\"Substituted Value 28\",\"-sub29-\":\"Substituted Value 29\",\"-sub32-\":\"Substituted Value 32\",\"-sub33-\":\"Substituted Value 33\"}},{\"substitutions\":{\"-sub30-\":\"Substituted Value 30\",\"-sub31-\":\"Substituted Value 31\"}}]}"); - } - - [Fact] - public void TestAddDynamicTemplateDataValue() - { - // Personalization not passed in, Personalization does not exist - var msg = new SendGridMessage(); - msg.AddDynamicTemplateDataValue("key1", "Dynamic Template Data Value 1"); - Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key1\":\"Dynamic Template Data Value 1\"}}]}"); - - // Personalization passed in, no Personalizations - msg = new SendGridMessage(); - var personalization = new Personalization() - { - DynamicTemplateData = new Dictionary() - { - { "key2", "Dynamic Template Data Value 2" } - } - }; - msg.AddDynamicTemplateDataValue("key3", "Dynamic Template Data Value 3", 0, personalization); - Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key2\":\"Dynamic Template Data Value 2\",\"key3\":\"Dynamic Template Data Value 3\"}}]}"); - - // Personalization passed in, Personalization exists - msg = new SendGridMessage - { - Personalizations = new List() - { - new Personalization() - { - DynamicTemplateData = new Dictionary() - { - {"key4", "Dynamic Template Data Value 4"} - } - } - } - }; - personalization = new Personalization() - { - DynamicTemplateData = new Dictionary() - { - { "key5", "Dynamic Template Data Value 5" } - } - }; - msg.AddDynamicTemplateDataValue("key6", "Dynamic Template Data Value 6", 1, personalization); - Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key4\":\"Dynamic Template Data Value 4\"}},{\"dynamic_template_data\":{\"key5\":\"Dynamic Template Data Value 5\",\"key6\":\"Dynamic Template Data Value 6\"}}]}"); - - // Personalization not passed in Personalization exists - msg = new SendGridMessage - { - Personalizations = new List() - { - new Personalization() - { - DynamicTemplateData = new Dictionary() - { - {"key7", "Dynamic Template Data Value 7"} - } - } - } - }; - msg.AddDynamicTemplateDataValue("key8", "Dynamic Template Data Value 8"); - Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key7\":\"Dynamic Template Data Value 7\",\"key8\":\"Dynamic Template Data Value 8\"}}]}"); - - // Personalization not passed in Personalizations exists - msg = new SendGridMessage - { - Personalizations = new List() - { - new Personalization() - { - DynamicTemplateData = new Dictionary() - { - {"key9", "Dynamic Template Data Value 9"} - } - } - } - }; - personalization = new Personalization() - { - DynamicTemplateData = new Dictionary() - { - { "key10", "Dynamic Template Data Value 10" } - } - }; - msg.Personalizations.Add(personalization); - msg.AddDynamicTemplateDataValue("key11", "Dynamic Template Data Value 11"); - Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key9\":\"Dynamic Template Data Value 9\",\"key11\":\"Dynamic Template Data Value 11\"}},{\"dynamic_template_data\":{\"key10\":\"Dynamic Template Data Value 10\"}}]}"); } [Fact] - public void TestAddDynamicTemplateDataValues() + public void TestSetDynamicTemplateData() { // Personalization not passed in, Personalization does not exist var msg = new SendGridMessage(); - var dynamicTemplateDataValues = new Dictionary + var dynamicTemplateData1 = new { - {"key12", "Dynamic Template Data Value 12"}, - {"key13", "Dynamic Template Data Value 13"} + key12 = "Dynamic Template Data Value 12", + key13 = "Dynamic Template Data Value 13" }; - msg.AddDynamicTemplateDataValues(dynamicTemplateDataValues); + msg.SetDynamicTemplateData(dynamicTemplateData1); Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key12\":\"Dynamic Template Data Value 12\",\"key13\":\"Dynamic Template Data Value 13\"}}]}"); // Personalization passed in, no Personalizations msg = new SendGridMessage(); - dynamicTemplateDataValues = new Dictionary + var dynamicTemplateData2 = new { - {"key14", "Dynamic Template Data Value 14"}, - {"key15", "Dynamic Template Data Value 15"} + key14 = "Dynamic Template Data Value 14", + key15 = "Dynamic Template Data Value 15" }; var personalization = new Personalization() { - DynamicTemplateData = dynamicTemplateDataValues + DynamicTemplateData = dynamicTemplateData2 }; - dynamicTemplateDataValues = new Dictionary + var dynamicTemplateData3 = new { - {"key16", "Dynamic Template Data Value 16"}, - {"key17", "Dynamic Template Data Value 17"} + key16 = "Dynamic Template Data Value 16", + key17 = "Dynamic Template Data Value 17" }; - msg.AddDynamicTemplateDataValues(dynamicTemplateDataValues, 0, personalization); - Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key14\":\"Dynamic Template Data Value 14\",\"key15\":\"Dynamic Template Data Value 15\",\"key16\":\"Dynamic Template Data Value 16\",\"key17\":\"Dynamic Template Data Value 17\"}}]}"); + msg.SetDynamicTemplateData(dynamicTemplateData3, 0, personalization); + Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key16\":\"Dynamic Template Data Value 16\",\"key17\":\"Dynamic Template Data Value 17\"}}]}"); // Personalization passed in, Personalization exists msg = new SendGridMessage(); - dynamicTemplateDataValues = new Dictionary + var dynamicTemplateData4 = new { - {"key18", "Dynamic Template Data Value 18"}, - {"key19", "Dynamic Template Data Value 19"} + key18 = "Dynamic Template Data Value 18", + key19 = "Dynamic Template Data Value 19" }; msg.Personalizations = new List() { new Personalization() { - DynamicTemplateData = dynamicTemplateDataValues + DynamicTemplateData = dynamicTemplateData4 } }; - dynamicTemplateDataValues = new Dictionary + var dynamicTemplateData5 = new { - {"key20", "Dynamic Template Data Value 20"}, - {"key21", "Dynamic Template Data Value 21"} + key20 = "Dynamic Template Data Value 20", + key21 = "Dynamic Template Data Value 21" }; personalization = new Personalization() { - DynamicTemplateData = dynamicTemplateDataValues + DynamicTemplateData = dynamicTemplateData5 }; - dynamicTemplateDataValues = new Dictionary + var dynamicTemplateData6 = new { - {"key22", "Dynamic Template Data Value 22"}, - {"key23", "Dynamic Template Data Value 23"} + key22 = "Dynamic Template Data Value 22", + key23 = "Dynamic Template Data Value 23" }; - msg.AddDynamicTemplateDataValues(dynamicTemplateDataValues, 1, personalization); - Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key18\":\"Dynamic Template Data Value 18\",\"key19\":\"Dynamic Template Data Value 19\"}},{\"dynamic_template_data\":{\"key20\":\"Dynamic Template Data Value 20\",\"key21\":\"Dynamic Template Data Value 21\",\"key22\":\"Dynamic Template Data Value 22\",\"key23\":\"Dynamic Template Data Value 23\"}}]}"); + msg.SetDynamicTemplateData(dynamicTemplateData6, 1, personalization); + Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key18\":\"Dynamic Template Data Value 18\",\"key19\":\"Dynamic Template Data Value 19\"}},{\"dynamic_template_data\":{\"key22\":\"Dynamic Template Data Value 22\",\"key23\":\"Dynamic Template Data Value 23\"}}]}"); // Personalization not passed in Personalization exists msg = new SendGridMessage(); - dynamicTemplateDataValues = new Dictionary + var dynamicTemplateData7 = new { - {"key24", "Dynamic Template Data Value 24"}, - {"key25", "Dynamic Template Data Value 25"} + key24 = "Dynamic Template Data Value 24", + key25 = "Dynamic Template Data Value 25" }; msg.Personalizations = new List() { new Personalization() { - DynamicTemplateData = dynamicTemplateDataValues + DynamicTemplateData = dynamicTemplateData7 } }; - dynamicTemplateDataValues = new Dictionary + var dynamicTemplateData8 = new { - {"key26", "Dynamic Template Data Value 26"}, - {"key27", "Dynamic Template Data Value 27"} + key26 = "Dynamic Template Data Value 26", + key27 = "Dynamic Template Data Value 27" }; - msg.AddDynamicTemplateDataValues(dynamicTemplateDataValues); - Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key24\":\"Dynamic Template Data Value 24\",\"key25\":\"Dynamic Template Data Value 25\",\"key26\":\"Dynamic Template Data Value 26\",\"key27\":\"Dynamic Template Data Value 27\"}}]}"); + msg.SetDynamicTemplateData(dynamicTemplateData8); + Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key26\":\"Dynamic Template Data Value 26\",\"key27\":\"Dynamic Template Data Value 27\"}}]}"); // Personalization not passed in Personalizations exists msg = new SendGridMessage(); - dynamicTemplateDataValues = new Dictionary + var dynamicTemplateData9 = new { - {"key28", "Dynamic Template Data Value 28"}, - {"key29", "Dynamic Template Data Value 29"} + key28 = "Dynamic Template Data Value 28", + key29 = "Dynamic Template Data Value 29" }; msg.Personalizations = new List() { new Personalization() { - DynamicTemplateData = dynamicTemplateDataValues + DynamicTemplateData = dynamicTemplateData9 } }; - dynamicTemplateDataValues = new Dictionary + var dynamicTemplateData10 = new { - {"key30", "Dynamic Template Data Value 30"}, - {"key31", "Dynamic Template Data Value 31"} + key30 = "Dynamic Template Data Value 30", + key31 = "Dynamic Template Data Value 31" }; personalization = new Personalization() { - DynamicTemplateData = dynamicTemplateDataValues + DynamicTemplateData = dynamicTemplateData10 }; msg.Personalizations.Add(personalization); - dynamicTemplateDataValues = new Dictionary + var dynamicTemplateData11 = new { - {"key32", "Dynamic Template Data Value 32"}, - {"key33", "Dynamic Template Data Value 33"} + key32 = "Dynamic Template Data Value 32", + key33 = "Dynamic Template Data Value 33" + }; + msg.SetDynamicTemplateData(dynamicTemplateData11); + Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key32\":\"Dynamic Template Data Value 32\",\"key33\":\"Dynamic Template Data Value 33\"}},{\"dynamic_template_data\":{\"key30\":\"Dynamic Template Data Value 30\",\"key31\":\"Dynamic Template Data Value 31\"}}]}"); + + // Complex dynamic template data + msg = new SendGridMessage(); + var dynamicTemplateData12 = new + { + array = new List + { + "Dynamic Template Data Array Value 1", + "Dynamic Template Data Array Value 2" + }, + innerObject = new + { + innerObjectKey1 = "Dynamic Template Data Deep Object Value 1" + } }; - msg.AddDynamicTemplateDataValues(dynamicTemplateDataValues); - Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key28\":\"Dynamic Template Data Value 28\",\"key29\":\"Dynamic Template Data Value 29\",\"key32\":\"Dynamic Template Data Value 32\",\"key33\":\"Dynamic Template Data Value 33\"}},{\"dynamic_template_data\":{\"key30\":\"Dynamic Template Data Value 30\",\"key31\":\"Dynamic Template Data Value 31\"}}]}"); + msg.SetDynamicTemplateData(dynamicTemplateData12); + Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"array\":[\"Dynamic Template Data Array Value 1\",\"Dynamic Template Data Array Value 2\"],\"innerObject\":{\"innerObjectKey1\":\"Dynamic Template Data Deep Object Value 1\"}}}]}"); } [Fact] From dc345b34af287b41cc4bebf8a5522fccb0af290d Mon Sep 17 00:00:00 2001 From: Elmer Thomas Date: Wed, 5 Sep 2018 12:20:44 -0700 Subject: [PATCH 4/6] Naming conventions --- USE_CASES.md | 52 +++++++++------ src/SendGrid/Helpers/Mail/MailHelper.cs | 12 ++-- .../Helpers/Mail/Model/Personalization.cs | 4 +- src/SendGrid/Helpers/Mail/SendGridMessage.cs | 14 ++-- .../Helpers/Mail/MailHelperTests.cs | 64 +++++++++---------- tests/SendGrid.Tests/Integration.cs | 40 ++++++------ ...s.cs => TemplateDataSerialisationTests.cs} | 30 ++++----- 7 files changed, 113 insertions(+), 103 deletions(-) rename tests/SendGrid.Tests/{DynamicTemplateDataSerialisationTests.cs => TemplateDataSerialisationTests.cs} (76%) diff --git a/USE_CASES.md b/USE_CASES.md index cf910100e..8dd8906cd 100644 --- a/USE_CASES.md +++ b/USE_CASES.md @@ -2,17 +2,27 @@ This documentation provides examples for specific use cases. Please [open an iss # Table of Contents -* [Email - Attachments](#attachments) -* [Email - Kitchen Sink - an example with all settings used](#kitchensink) -* [Email - Send a Single Email to Multiple Recipients](#singleemailmultiplerecipients) -* [Email - Send a Single Email to a Single Recipient](#singleemailsinglerecipient) -* [Email - Send Multiple Emails to Multiple Recipients](#multipleemailsmultiplerecipients) -* [Email - Dynamic Transactional Templates](#dynamic-transactional-templates) -* [Email - _Legacy_ Transactional Templates](#transactional-templates) -* [Transient Fault Handling](#transient-faults) -* [How to Setup a Domain Whitelabel](#domain-whitelabel) -* [How to View Email Statistics](#email-stats) -* [How to transform HTML to plain text](#html-to-plain-text) +- [Table of Contents](#table-of-contents) +- [Attachments](#attachments) +- [Kitchen Sink - an example with all settings used](#kitchen-sink---an-example-with-all-settings-used) +- [Send a Single Email to Multiple Recipients](#send-a-single-email-to-multiple-recipients) +- [Send a Single Email to a Single Recipient](#send-a-single-email-to-a-single-recipient) +- [Send Multiple Emails to Multiple Recipients](#send-multiple-emails-to-multiple-recipients) +- [Transactional Templates](#transactional-templates) + - [With Mail Helper Class](#with-mail-helper-class) + - [Without Mail Helper Class](#without-mail-helper-class) +- [_Legacy_ Transactional Templates](#legacy-transactional-templates) + - [Legacy Template With Mail Helper Class](#legacy-template-with-mail-helper-class) + - [Legacy Template Without Mail Helper Class](#legacy-template-without-mail-helper-class) +- [Transient Fault Handling](#transient-fault-handling) + - [RetryCount](#retrycount) + - [MinimumBackOff](#minimumbackoff) + - [MaximumBackOff](#maximumbackoff) + - [DeltaBackOff](#deltabackoff) + - [Examples](#examples) +- [How to Setup a Domain Whitelabel](#how-to-setup-a-domain-whitelabel) +- [How to View Email Statistics](#how-to-view-email-statistics) +- [How to transform HTML to plain text](#how-to-transform-html-to-plain-text) # Attachments @@ -469,10 +479,10 @@ namespace Example } ``` - -# Dynamic Transactional Templates + +# Transactional Templates -For this example, we assume you have created a [dynamic transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/Create_and_edit_dynamic_transactional_templates.html). +For this example, we assume you have created a [transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/Create_and_edit_dynamic_transactional_templates.html). Following is the template content we used for testing. Template ID (replace with your own): @@ -576,9 +586,9 @@ namespace Example ``` Methods also exist on `MailHelper` to create dynamic template emails: -* `CreateSingleDynamicTemplateEmail` -* `CreateSingleDynamicTemplateEmailToMultipleRecipients` -* `CreateMultipleDynamicTemplateEmailsToMultipleRecipients` +* `CreateSingleTemplateEmail` +* `CreateSingleTemplateEmailToMultipleRecipients` +* `CreateMultipleTemplateEmailsToMultipleRecipients` ## Without Mail Helper Class @@ -638,10 +648,10 @@ namespace Example } ``` - + # _Legacy_ Transactional Templates -For this example, we assume you have created a [transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html). Following is the template content we used for testing. +For this example, we assume you have created a [legacy transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html). Following is the template content we used for testing. Template ID (replace with your own): @@ -675,7 +685,7 @@ I hope you are having a great day in -city- :) ``` -## With Mail Helper Class +## Legacy Template With Mail Helper Class ```csharp using SendGrid; @@ -714,7 +724,7 @@ namespace Example } ``` -## Without Mail Helper Class +## Legacy Template Without Mail Helper Class ```csharp using Newtonsoft.Json; diff --git a/src/SendGrid/Helpers/Mail/MailHelper.cs b/src/SendGrid/Helpers/Mail/MailHelper.cs index a750ca733..ec4ec158c 100644 --- a/src/SendGrid/Helpers/Mail/MailHelper.cs +++ b/src/SendGrid/Helpers/Mail/MailHelper.cs @@ -61,7 +61,7 @@ public static SendGridMessage CreateSingleEmail( /// The ID of the template. /// The data with which to populate the dynamic template. /// A SendGridMessage object. - public static SendGridMessage CreateSingleDynamicTemplateEmail( + public static SendGridMessage CreateSingleTemplateEmail( EmailAddress from, EmailAddress to, string templateId, @@ -79,7 +79,7 @@ public static SendGridMessage CreateSingleDynamicTemplateEmail( if (dynamicTemplateData != null) { - msg.SetDynamicTemplateData(dynamicTemplateData); + msg.SetTemplateData(dynamicTemplateData); } return msg; @@ -130,7 +130,7 @@ public static SendGridMessage CreateSingleEmailToMultipleRecipients( /// The ID of the template. /// The data with which to populate the dynamic template. /// A SendGridMessage object. - public static SendGridMessage CreateSingleDynamicTemplateEmailToMultipleRecipients( + public static SendGridMessage CreateSingleTemplateEmailToMultipleRecipients( EmailAddress from, List tos, string templateId, @@ -153,7 +153,7 @@ public static SendGridMessage CreateSingleDynamicTemplateEmailToMultipleRecipien if (setDynamicTemplateDataValues) { - msg.SetDynamicTemplateData(dynamicTemplateData, i); + msg.SetTemplateData(dynamicTemplateData, i); } } @@ -208,7 +208,7 @@ public static SendGridMessage CreateMultipleEmailsToMultipleRecipients( /// The ID of the template. /// The data with which to populate the dynamic template. /// A SendGridMessage object. - public static SendGridMessage CreateMultipleDynamicTemplateEmailsToMultipleRecipients( + public static SendGridMessage CreateMultipleTemplateEmailsToMultipleRecipients( EmailAddress from, List tos, string templateId, @@ -231,7 +231,7 @@ public static SendGridMessage CreateMultipleDynamicTemplateEmailsToMultipleRecip if (setDynamicTemplateDataValues) { - msg.SetDynamicTemplateData(dynamicTemplateData[i], i); + msg.SetTemplateData(dynamicTemplateData[i], i); } } diff --git a/src/SendGrid/Helpers/Mail/Model/Personalization.cs b/src/SendGrid/Helpers/Mail/Model/Personalization.cs index 8b9ac0bcb..f4c1f659a 100644 --- a/src/SendGrid/Helpers/Mail/Model/Personalization.cs +++ b/src/SendGrid/Helpers/Mail/Model/Personalization.cs @@ -65,9 +65,9 @@ public class Personalization public long? SendAt { get; set; } /// - /// Gets or sets the dynamic template data object following the pattern "dynamic template data key":"dynamic template data value". All are assumed to be strings. These key value pairs will apply to the content of your dynamic template email, in addition to the subject and reply-to parameters. + /// Gets or sets the template data object following the pattern "template data key":"template data value". All are assumed to be strings. These key value pairs will apply to the content of your template email, in addition to the subject and reply-to parameters. /// [JsonProperty(PropertyName = "dynamic_template_data", IsReference = false)] - public object DynamicTemplateData { get; set; } + public object TemplateData { get; set; } } } diff --git a/src/SendGrid/Helpers/Mail/SendGridMessage.cs b/src/SendGrid/Helpers/Mail/SendGridMessage.cs index 5f0ffa4f4..3b43aff41 100644 --- a/src/SendGrid/Helpers/Mail/SendGridMessage.cs +++ b/src/SendGrid/Helpers/Mail/SendGridMessage.cs @@ -759,14 +759,14 @@ public void AddSubstitutions(Dictionary substitutions, int perso /// /// Add dynamic template data to the email. /// - /// A Dynamic Template Data object. + /// A Template Data object. /// Specify the index of the Personalization object where you want to add the substitutions. /// A personalization object to append to the message. - public void SetDynamicTemplateData(object dynamicTemplateData, int personalizationIndex = 0, Personalization personalization = null) + public void SetTemplateData(object dynamicTemplateData, int personalizationIndex = 0, Personalization personalization = null) { if (personalization != null) { - personalization.DynamicTemplateData = dynamicTemplateData; + personalization.TemplateData = dynamicTemplateData; if (this.Personalizations == null) { this.Personalizations = new List(); @@ -788,12 +788,12 @@ public void SetDynamicTemplateData(object dynamicTemplateData, int personalizati this.Personalizations.Insert(personalizationIndex, p); } - if (this.Personalizations[personalizationIndex].DynamicTemplateData == null) + if (this.Personalizations[personalizationIndex].TemplateData == null) { - this.Personalizations[personalizationIndex].DynamicTemplateData = new Dictionary(); + this.Personalizations[personalizationIndex].TemplateData = new Dictionary(); } - this.Personalizations[personalizationIndex].DynamicTemplateData = dynamicTemplateData; + this.Personalizations[personalizationIndex].TemplateData = dynamicTemplateData; return; } @@ -801,7 +801,7 @@ public void SetDynamicTemplateData(object dynamicTemplateData, int personalizati { new Personalization() { - DynamicTemplateData = dynamicTemplateData + TemplateData = dynamicTemplateData } }; return; diff --git a/tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs b/tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs index fb5a7d81f..ea9a4a1da 100644 --- a/tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs +++ b/tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs @@ -4,22 +4,22 @@ using Xunit; namespace SendGrid.Tests.Helpers.Mail -{ - public class MailHelperTests - { - [Theory] - [InlineData("Name Of A Person+", "send@grid.com", "Name Of A Person+ < send@grid.com > ")] - [InlineData("", "send@grid.com", " send@grid.com ")] - [InlineData(null, "notAValidEmail", "notAValidEmail")] - public void TestStringToEmail(string expectedName, string expectedEmail, string rf2822Email) - { - var address = MailHelper.StringToEmailAddress(rf2822Email); - Assert.Equal(expectedEmail, address.Email); - Assert.Equal(expectedName, address.Name); - } - - [Fact] - public void TestCreateSingleDynamicTemplateEmail() +{ + public class MailHelperTests + { + [Theory] + [InlineData("Name Of A Person+", "send@grid.com", "Name Of A Person+ < send@grid.com > ")] + [InlineData("", "send@grid.com", " send@grid.com ")] + [InlineData(null, "notAValidEmail", "notAValidEmail")] + public void TestStringToEmail(string expectedName, string expectedEmail, string rf2822Email) + { + var address = MailHelper.StringToEmailAddress(rf2822Email); + Assert.Equal(expectedEmail, address.Email); + Assert.Equal(expectedName, address.Name); + } + + [Fact] + public void TestCreateSingleTemplateEmail() { var from = new EmailAddress("from@email.com", "FromName"); var to = new EmailAddress("to@email.com"); @@ -29,7 +29,7 @@ public void TestCreateSingleDynamicTemplateEmail() { "key1", "value1" } }; - var sendGridMessage = MailHelper.CreateSingleDynamicTemplateEmail( + var sendGridMessage = MailHelper.CreateSingleTemplateEmail( from, to, templateId, @@ -38,11 +38,11 @@ public void TestCreateSingleDynamicTemplateEmail() Assert.Equal(from, sendGridMessage.From); Assert.Equal(to, sendGridMessage.Personalizations.Single().Tos.Single()); Assert.Equal(templateId, sendGridMessage.TemplateId); - Assert.Equal(dynamicTemplateData, sendGridMessage.Personalizations.Single().DynamicTemplateData); - } - - [Fact] - public void TestCreateSingleDynamicTemplateEmailToMultipleRecipients() + Assert.Equal(dynamicTemplateData, sendGridMessage.Personalizations.Single().TemplateData); + } + + [Fact] + public void TestCreateSingleTemplateEmailToMultipleRecipients() { var from = new EmailAddress("from@email.com", "FromName"); var tos = new List @@ -67,12 +67,12 @@ public void TestCreateSingleDynamicTemplateEmailToMultipleRecipients() Assert.Equal(tos[0], sendGridMessage.Personalizations.ElementAt(0).Tos.Single()); Assert.Equal(tos[1], sendGridMessage.Personalizations.ElementAt(1).Tos.Single()); Assert.Equal(templateId, sendGridMessage.TemplateId); - Assert.Equal(dynamicTemplateData, sendGridMessage.Personalizations.ElementAt(0).DynamicTemplateData); - Assert.Equal(dynamicTemplateData, sendGridMessage.Personalizations.ElementAt(1).DynamicTemplateData); - } - - [Fact] - public void TestCreateMultipleDynamicTemplateEmailsToMultipleRecipients() + Assert.Equal(dynamicTemplateData, sendGridMessage.Personalizations.ElementAt(0).TemplateData); + Assert.Equal(dynamicTemplateData, sendGridMessage.Personalizations.ElementAt(1).TemplateData); + } + + [Fact] + public void TestCreateMultipleTemplateEmailsToMultipleRecipients() { var from = new EmailAddress("from@email.com", "FromName"); var tos = new List @@ -88,7 +88,7 @@ public void TestCreateMultipleDynamicTemplateEmailsToMultipleRecipients() new { key2 = "value2" } }; - var sendGridMessage = MailHelper.CreateMultipleDynamicTemplateEmailsToMultipleRecipients( + var sendGridMessage = MailHelper.CreateMultipleTemplateEmailsToMultipleRecipients( from, tos, templateId, @@ -98,8 +98,8 @@ public void TestCreateMultipleDynamicTemplateEmailsToMultipleRecipients() Assert.Equal(tos[0], sendGridMessage.Personalizations.ElementAt(0).Tos.Single()); Assert.Equal(tos[1], sendGridMessage.Personalizations.ElementAt(1).Tos.Single()); Assert.Equal(templateId, sendGridMessage.TemplateId); - Assert.Equal(dynamicTemplateData[0], sendGridMessage.Personalizations.ElementAt(0).DynamicTemplateData); - Assert.Equal(dynamicTemplateData[1], sendGridMessage.Personalizations.ElementAt(1).DynamicTemplateData); - } + Assert.Equal(dynamicTemplateData[0], sendGridMessage.Personalizations.ElementAt(0).TemplateData); + Assert.Equal(dynamicTemplateData[1], sendGridMessage.Personalizations.ElementAt(1).TemplateData); + } } } diff --git a/tests/SendGrid.Tests/Integration.cs b/tests/SendGrid.Tests/Integration.cs index 903bccf57..7a972dd4d 100644 --- a/tests/SendGrid.Tests/Integration.cs +++ b/tests/SendGrid.Tests/Integration.cs @@ -1659,7 +1659,7 @@ public void TestAddSubstitutions() } [Fact] - public void TestSetDynamicTemplateData() + public void TestSetTemplateData() { // Personalization not passed in, Personalization does not exist var msg = new SendGridMessage(); @@ -1668,7 +1668,7 @@ public void TestSetDynamicTemplateData() key12 = "Dynamic Template Data Value 12", key13 = "Dynamic Template Data Value 13" }; - msg.SetDynamicTemplateData(dynamicTemplateData1); + msg.SetTemplateData(dynamicTemplateData1); Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key12\":\"Dynamic Template Data Value 12\",\"key13\":\"Dynamic Template Data Value 13\"}}]}"); // Personalization passed in, no Personalizations @@ -1680,14 +1680,14 @@ public void TestSetDynamicTemplateData() }; var personalization = new Personalization() { - DynamicTemplateData = dynamicTemplateData2 + TemplateData = dynamicTemplateData2 }; var dynamicTemplateData3 = new { key16 = "Dynamic Template Data Value 16", key17 = "Dynamic Template Data Value 17" }; - msg.SetDynamicTemplateData(dynamicTemplateData3, 0, personalization); + msg.SetTemplateData(dynamicTemplateData3, 0, personalization); Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key16\":\"Dynamic Template Data Value 16\",\"key17\":\"Dynamic Template Data Value 17\"}}]}"); // Personalization passed in, Personalization exists @@ -1699,7 +1699,7 @@ public void TestSetDynamicTemplateData() }; msg.Personalizations = new List() { new Personalization() { - DynamicTemplateData = dynamicTemplateData4 + TemplateData = dynamicTemplateData4 } }; var dynamicTemplateData5 = new @@ -1709,14 +1709,14 @@ public void TestSetDynamicTemplateData() }; personalization = new Personalization() { - DynamicTemplateData = dynamicTemplateData5 + TemplateData = dynamicTemplateData5 }; var dynamicTemplateData6 = new { key22 = "Dynamic Template Data Value 22", key23 = "Dynamic Template Data Value 23" }; - msg.SetDynamicTemplateData(dynamicTemplateData6, 1, personalization); + msg.SetTemplateData(dynamicTemplateData6, 1, personalization); Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key18\":\"Dynamic Template Data Value 18\",\"key19\":\"Dynamic Template Data Value 19\"}},{\"dynamic_template_data\":{\"key22\":\"Dynamic Template Data Value 22\",\"key23\":\"Dynamic Template Data Value 23\"}}]}"); // Personalization not passed in Personalization exists @@ -1728,7 +1728,7 @@ public void TestSetDynamicTemplateData() }; msg.Personalizations = new List() { new Personalization() { - DynamicTemplateData = dynamicTemplateData7 + TemplateData = dynamicTemplateData7 } }; var dynamicTemplateData8 = new @@ -1736,7 +1736,7 @@ public void TestSetDynamicTemplateData() key26 = "Dynamic Template Data Value 26", key27 = "Dynamic Template Data Value 27" }; - msg.SetDynamicTemplateData(dynamicTemplateData8); + msg.SetTemplateData(dynamicTemplateData8); Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key26\":\"Dynamic Template Data Value 26\",\"key27\":\"Dynamic Template Data Value 27\"}}]}"); // Personalization not passed in Personalizations exists @@ -1748,7 +1748,7 @@ public void TestSetDynamicTemplateData() }; msg.Personalizations = new List() { new Personalization() { - DynamicTemplateData = dynamicTemplateData9 + TemplateData = dynamicTemplateData9 } }; var dynamicTemplateData10 = new @@ -1758,7 +1758,7 @@ public void TestSetDynamicTemplateData() }; personalization = new Personalization() { - DynamicTemplateData = dynamicTemplateData10 + TemplateData = dynamicTemplateData10 }; msg.Personalizations.Add(personalization); var dynamicTemplateData11 = new @@ -1766,24 +1766,24 @@ public void TestSetDynamicTemplateData() key32 = "Dynamic Template Data Value 32", key33 = "Dynamic Template Data Value 33" }; - msg.SetDynamicTemplateData(dynamicTemplateData11); + msg.SetTemplateData(dynamicTemplateData11); Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"key32\":\"Dynamic Template Data Value 32\",\"key33\":\"Dynamic Template Data Value 33\"}},{\"dynamic_template_data\":{\"key30\":\"Dynamic Template Data Value 30\",\"key31\":\"Dynamic Template Data Value 31\"}}]}"); // Complex dynamic template data msg = new SendGridMessage(); var dynamicTemplateData12 = new { - array = new List - { - "Dynamic Template Data Array Value 1", - "Dynamic Template Data Array Value 2" + array = new List + { + "Dynamic Template Data Array Value 1", + "Dynamic Template Data Array Value 2" }, - innerObject = new - { - innerObjectKey1 = "Dynamic Template Data Deep Object Value 1" + innerObject = new + { + innerObjectKey1 = "Dynamic Template Data Deep Object Value 1" } }; - msg.SetDynamicTemplateData(dynamicTemplateData12); + msg.SetTemplateData(dynamicTemplateData12); Assert.True(msg.Serialize() == "{\"personalizations\":[{\"dynamic_template_data\":{\"array\":[\"Dynamic Template Data Array Value 1\",\"Dynamic Template Data Array Value 2\"],\"innerObject\":{\"innerObjectKey1\":\"Dynamic Template Data Deep Object Value 1\"}}}]}"); } diff --git a/tests/SendGrid.Tests/DynamicTemplateDataSerialisationTests.cs b/tests/SendGrid.Tests/TemplateDataSerialisationTests.cs similarity index 76% rename from tests/SendGrid.Tests/DynamicTemplateDataSerialisationTests.cs rename to tests/SendGrid.Tests/TemplateDataSerialisationTests.cs index 6f9924506..83137c4d1 100644 --- a/tests/SendGrid.Tests/DynamicTemplateDataSerialisationTests.cs +++ b/tests/SendGrid.Tests/TemplateDataSerialisationTests.cs @@ -4,26 +4,26 @@ namespace SendGrid.Tests { - public class DynamicTemplateDataSerialisationTests - { - [Fact] - public void TestSetDynamicTemplateDataWorksWithSpecifiedJsonPropertyNames() + public class TemplateDataSerialisationTests + { + [Fact] + public void TestSetTemplateDataWorksWithSpecifiedJsonPropertyNames() { var msg = new SendGridMessage(); - var dynamicTemplateData = new TestDynamicTemplateData - { - MyCamelCaseProperty = "camelCase", - MyKebabCaseProperty = "kebab-case", - MyPascalCaseProperty = "PascalCase", - MySnakeCaseProperty = "snake_case", - }; - - msg.SetDynamicTemplateData(dynamicTemplateData); - Assert.Equal("{\"personalizations\":[{\"dynamic_template_data\":{\"myCamelCaseProperty\":\"camelCase\",\"my-kebab-case-property\":\"kebab-case\",\"MyPascalCaseProperty\":\"PascalCase\",\"my_snake_case_property\":\"snake_case\"}}]}", msg.Serialize()); + var dynamicTemplateData = new TestTemplateData + { + MyCamelCaseProperty = "camelCase", + MyKebabCaseProperty = "kebab-case", + MyPascalCaseProperty = "PascalCase", + MySnakeCaseProperty = "snake_case", + }; + + msg.SetTemplateData(dynamicTemplateData); + Assert.Equal("{\"personalizations\":[{\"dynamic_template_data\":{\"myCamelCaseProperty\":\"camelCase\",\"my-kebab-case-property\":\"kebab-case\",\"MyPascalCaseProperty\":\"PascalCase\",\"my_snake_case_property\":\"snake_case\"}}]}", msg.Serialize()); } - private class TestDynamicTemplateData + private class TestTemplateData { [JsonProperty("myCamelCaseProperty")] public string MyCamelCaseProperty { get; set; } From 1ae579c16e76aebafe46b60a55351bee03bfc546 Mon Sep 17 00:00:00 2001 From: Elmer Thomas Date: Wed, 5 Sep 2018 12:36:56 -0700 Subject: [PATCH 5/6] Typo --- tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs b/tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs index ea9a4a1da..7f616180b 100644 --- a/tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs +++ b/tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs @@ -57,7 +57,7 @@ public void TestCreateSingleTemplateEmailToMultipleRecipients() { "key1", "value1" } }; - var sendGridMessage = MailHelper.CreateSingleDynamicTemplateEmailToMultipleRecipients( + var sendGridMessage = MailHelper.CreateSingleTemplateEmailToMultipleRecipients( from, tos, templateId, From 36ee1b9c89050c519a57be8875010d1575e91c21 Mon Sep 17 00:00:00 2001 From: Elmer Thomas Date: Wed, 5 Sep 2018 13:59:22 -0700 Subject: [PATCH 6/6] Update README to reflect template naming convention --- USE_CASES.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/USE_CASES.md b/USE_CASES.md index 8dd8906cd..bb46bd01f 100644 --- a/USE_CASES.md +++ b/USE_CASES.md @@ -542,7 +542,7 @@ namespace Example msg.AddTo(new EmailAddress("test@example.com", "Example User")); msg.SetTemplateId("d-d42b0eea09964d1ab957c18986c01828"); - var dynamicTemplateData = new ExampleDynamicTemplateData + var dynamicTemplateData = new ExampleTemplateData { Subject = "Hi!", Name = "Example User", @@ -553,7 +553,7 @@ namespace Example } }; - msg.SetDynamicTemplateData(dynamicTemplateData); + msg.SetTemplateData(dynamicTemplateData); var response = await client.SendEmailAsync(msg); Console.WriteLine(response.StatusCode); Console.WriteLine(response.Headers.ToString()); @@ -561,7 +561,7 @@ namespace Example Console.ReadLine(); } - private class ExampleDynamicTemplateData + private class ExampleTemplateData { [JsonProperty("subject")] public string Subject { get; set; } @@ -885,4 +885,4 @@ namespace Example { } } -``` \ No newline at end of file +```