-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Personalization.DynamicTemplateData to be an object
- Loading branch information
1 parent
312e218
commit 3203526
Showing
8 changed files
with
161 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
@@ -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("[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; } | ||
} | ||
} | ||
} | ||
``` | ||
|
@@ -580,7 +612,10 @@ namespace Example | |
'dynamic_template_data': { | ||
'subject': 'Hi!', | ||
'name': 'Example User', | ||
'city': 'Birmingham' | ||
'location': { | ||
'city': 'Birmingham', | ||
'country': 'United Kingdom' | ||
} | ||
} | ||
} | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
tests/SendGrid.Tests/DynamicTemplateDataSerialisationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.