Skip to content

Commit

Permalink
Update Personalization.DynamicTemplateData to be an object
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-underwood committed Aug 21, 2018
1 parent 312e218 commit 3203526
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 214 deletions.
45 changes: 40 additions & 5 deletions USE_CASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ Hello {{name}},
<br /><br/>
I'm glad you are trying out the dynamic template feature!
<br /><br/>
I hope you are having a great day in {{city}} :)
I hope you are having a great day in {{location.city}} :)
<br /><br/>
</body>
</html>
Expand All @@ -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;
Expand All @@ -529,15 +530,46 @@ namespace Example
msg.SetFrom(new EmailAddress("[email protected]", "Example User"));
msg.AddTo(new EmailAddress("[email protected]", "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; }
}
}
}
```
Expand Down Expand Up @@ -580,7 +612,10 @@ namespace Example
'dynamic_template_data': {
'subject': 'Hi!',
'name': 'Example User',
'city': 'Birmingham'
'location': {
'city': 'Birmingham',
'country': 'United Kingdom'
}
}
}
],
Expand Down
12 changes: 6 additions & 6 deletions src/SendGrid/Helpers/Mail/MailHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static SendGridMessage CreateSingleDynamicTemplateEmail(
EmailAddress from,
EmailAddress to,
string templateId,
Dictionary<string, object> dynamicTemplateData)
object dynamicTemplateData)
{
if (string.IsNullOrWhiteSpace(templateId))
{
Expand All @@ -79,7 +79,7 @@ public static SendGridMessage CreateSingleDynamicTemplateEmail(

if (dynamicTemplateData != null)
{
msg.AddDynamicTemplateDataValues(dynamicTemplateData);
msg.SetDynamicTemplateData(dynamicTemplateData);
}

return msg;
Expand Down Expand Up @@ -134,7 +134,7 @@ public static SendGridMessage CreateSingleDynamicTemplateEmailToMultipleRecipien
EmailAddress from,
List<EmailAddress> tos,
string templateId,
Dictionary<string, object> dynamicTemplateData)
object dynamicTemplateData)
{
if (string.IsNullOrWhiteSpace(templateId))
{
Expand All @@ -153,7 +153,7 @@ public static SendGridMessage CreateSingleDynamicTemplateEmailToMultipleRecipien

if (setDynamicTemplateDataValues)
{
msg.AddDynamicTemplateDataValues(dynamicTemplateData, i);
msg.SetDynamicTemplateData(dynamicTemplateData, i);
}
}

Expand Down Expand Up @@ -212,7 +212,7 @@ public static SendGridMessage CreateMultipleDynamicTemplateEmailsToMultipleRecip
EmailAddress from,
List<EmailAddress> tos,
string templateId,
List<Dictionary<string, object>> dynamicTemplateData)
List<object> dynamicTemplateData)
{
if (string.IsNullOrWhiteSpace(templateId))
{
Expand All @@ -231,7 +231,7 @@ public static SendGridMessage CreateMultipleDynamicTemplateEmailsToMultipleRecip

if (setDynamicTemplateDataValues)
{
msg.AddDynamicTemplateDataValues(dynamicTemplateData[i], i);
msg.SetDynamicTemplateData(dynamicTemplateData[i], i);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/SendGrid/Helpers/Mail/Model/Personalization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
[JsonProperty(PropertyName = "dynamic_template_data", IsReference = false)]
public Dictionary<string, object> DynamicTemplateData { get; set; }
public object DynamicTemplateData { get; set; }
}
}
65 changes: 4 additions & 61 deletions src/SendGrid/Helpers/Mail/SendGridMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -750,73 +750,17 @@ public void AddSubstitutions(Dictionary<string, string> substitutions, int perso
return;
}

/// <summary>
/// Add a dynamic template data value to the email.
/// </summary>
/// <param name="dynamicTemplateDataKey">The dynamic template data key.</param>
/// <param name="dynamicTemplateDataValue">The substitution value.</param>
/// <param name="personalizationIndex">Specify the index of the Personalization object where you want to add the substitution.</param>
/// <param name="personalization">A personalization object to append to the message.</param>
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<Personalization>();
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<string, object>();
}

this.Personalizations[personalizationIndex].DynamicTemplateData.Add(dynamicTemplateDataKey, dynamicTemplateDataValue);
return;
}

this.Personalizations = new List<Personalization>()
{
new Personalization()
{
DynamicTemplateData = new Dictionary<string, object>()
{
{ dynamicTemplateDataKey, dynamicTemplateDataValue }
}
}
};
return;
}

/// <summary>
/// Add dynamic template data to the email.
/// </summary>
/// <param name="dynamicTemplateData">A collection of Dynamic Template Data key value pairs.</param>
/// <param name="dynamicTemplateData">A Dynamic Template Data object.</param>
/// <param name="personalizationIndex">Specify the index of the Personalization object where you want to add the substitutions.</param>
/// <param name="personalization">A personalization object to append to the message.</param>
public void AddDynamicTemplateDataValues(Dictionary<string, object> 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<Personalization>();
Expand All @@ -843,8 +787,7 @@ public void AddDynamicTemplateDataValues(Dictionary<string, object> dynamicTempl
this.Personalizations[personalizationIndex].DynamicTemplateData = new Dictionary<string, object>();
}

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;
}

Expand Down
40 changes: 40 additions & 0 deletions tests/SendGrid.Tests/DynamicTemplateDataSerialisationTests.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
}
6 changes: 3 additions & 3 deletions tests/SendGrid.Tests/Helpers/Mail/MailHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ public void TestCreateMultipleDynamicTemplateEmailsToMultipleRecipients()
};

var templateId = "d-template2";
var dynamicTemplateData = new List<Dictionary<string, object>>
var dynamicTemplateData = new List<object>
{
new Dictionary<string, object> { { "key1", "value1" } },
new Dictionary<string, object> { { "key2", "value2" } }
new { key1 = "value1" },
new { key2 = "value2" }
};

var sendGridMessage = MailHelper.CreateMultipleDynamicTemplateEmailsToMultipleRecipients(
Expand Down
Loading

0 comments on commit 3203526

Please sign in to comment.