diff options
author | Eric Becking <eric.becking@sendgrid.com> | 2012-01-11 16:46:21 -0700 |
---|---|---|
committer | Eric Becking <eric.becking@sendgrid.com> | 2012-01-11 16:46:21 -0700 |
commit | 3e3285a98e09cb7563e70849de976dc15f1cfa57 (patch) | |
tree | da560ec886241a45dafb56ebcc35eb6226ec004e /SendGrid/SendGridMail | |
parent | 45c5d8c68adf922ebe3f9c9a22244f0e2a951cf5 (diff) | |
parent | 909a506ec9b244c69c1824e04c23247fc881dcb1 (diff) | |
download | sendgrid-csharp-3e3285a98e09cb7563e70849de976dc15f1cfa57.zip sendgrid-csharp-3e3285a98e09cb7563e70849de976dc15f1cfa57.tar.gz sendgrid-csharp-3e3285a98e09cb7563e70849de976dc15f1cfa57.tar.bz2 |
Merge branch 'us1882' of github.com:sendgrid/sendgrid-csharp into us1882
Diffstat (limited to 'SendGrid/SendGridMail')
-rwxr-xr-x | SendGrid/SendGridMail/Header.cs | 96 | ||||
-rwxr-xr-x | SendGrid/SendGridMail/IHeader.cs | 50 |
2 files changed, 127 insertions, 19 deletions
diff --git a/SendGrid/SendGridMail/Header.cs b/SendGrid/SendGridMail/Header.cs index 6023214..c1138d8 100755 --- a/SendGrid/SendGridMail/Header.cs +++ b/SendGrid/SendGridMail/Header.cs @@ -7,24 +7,33 @@ namespace SendGridMail {
public class Header : IHeader
{
- public void AddTo(IEnumerable<string> recipients)
+ private readonly HeaderSettingsNode _settings;
+
+ public Header()
{
- throw new NotImplementedException();
+ _settings = new HeaderSettingsNode();
}
public void AddSubVal(string tag, IEnumerable<string> substitutions)
{
- throw new NotImplementedException();
+ var keys = new List<String> {"data", "sub", tag};
+ _settings.AddArray(keys, substitutions);
}
public void AddUniqueIdentifier(IDictionary<string, string> identifiers)
{
- throw new NotImplementedException();
+ foreach (var key in identifiers.Keys)
+ {
+ var keys = new List<String> {"data", "unique_args", key};
+ var value = identifiers[key];
+ _settings.AddSetting(keys, value);
+ }
}
public void SetCategory(string category)
{
- throw new NotImplementedException();
+ var keys = new List<String> {"data", "category"};
+ _settings.AddSetting(keys, category);
}
public void Enable(string filter)
@@ -39,28 +48,50 @@ namespace SendGridMail public void AddFilterSetting(string filter, IEnumerable<string> settings, string value)
{
- throw new NotImplementedException();
+ var keys = new List<string>() { "data", "filters", filter, "settings" }.Concat(settings).ToList();
+ _settings.AddSetting(keys, value);
}
public void AddHeader(MailMessage mime)
{
- throw new NotImplementedException();
+ mime.Headers.Add("x-smtpapi", AsJson());
}
public String AsJson()
{
- return "";
+ if(_settings.IsEmpty()) return "";
+ return _settings.ToJson();
}
-
- internal class FilterNode
+ internal class HeaderSettingsNode
{
- private Dictionary<String, FilterNode> _branches;
+ private readonly Dictionary<String, HeaderSettingsNode> _branches;
+ private IEnumerable<String> _array;
private String _leaf;
- public FilterNode()
+ public HeaderSettingsNode()
{
- _branches = new Dictionary<string, FilterNode>();
+ _branches = new Dictionary<string, HeaderSettingsNode>();
+ }
+
+ public void AddArray(List<String> keys, IEnumerable<String> value)
+ {
+ if (keys.Count == 0)
+ {
+ _array = value;
+ }
+ else
+ {
+ if (_leaf != null || _array != null)
+ throw new ArgumentException("Attempt to overwrite setting");
+
+ var key = keys.First();
+ if (!_branches.ContainsKey(key))
+ _branches[key] = new HeaderSettingsNode();
+
+ var remainingKeys = keys.Skip(1).ToList();
+ _branches[key].AddArray(remainingKeys, value);
+ }
}
public void AddSetting(List<String> keys, String value)
@@ -71,15 +102,18 @@ namespace SendGridMail }
else
{
- var key = keys[0];
+ if(_leaf != null || _array != null)
+ throw new ArgumentException("Attempt to overwrite setting");
+
+ var key = keys.First();
if (!_branches.ContainsKey(key))
- _branches[key] = new FilterNode();
+ _branches[key] = new HeaderSettingsNode();
+
var remainingKeys = keys.Skip(1).ToList();
_branches[key].AddSetting(remainingKeys, value);
}
}
-
public String GetSetting(params String[] keys)
{
return GetSetting(keys.ToList());
@@ -96,6 +130,22 @@ namespace SendGridMail return _branches[key].GetSetting(remainingKeys);
}
+ public IEnumerable<String> GetArray(params String[] keys)
+ {
+ return GetArray(keys.ToList());
+ }
+
+ public IEnumerable<String> GetArray(List<String> keys)
+ {
+ if (keys.Count == 0)
+ return _array;
+ var key = keys.First();
+ if (!_branches.ContainsKey(key))
+ throw new ArgumentException("Bad key path!");
+ var remainingKeys = keys.Skip(1).ToList();
+ return _branches[key].GetArray(remainingKeys);
+ }
+
public String GetLeaf()
{
return _leaf;
@@ -104,8 +154,18 @@ namespace SendGridMail public String ToJson()
{
if (_branches.Count > 0)
- return "{" + String.Join(",", _branches.Keys.Select(k => '"' + k + '"' + ":" + _branches[k].ToJson())) + "}";
- return JsonUtils.Serialize(_leaf);
+ return "{" + String.Join(",", _branches.Keys.Select(k => JsonUtils.Serialize(k) + " : " + _branches[k].ToJson())) + "}";
+ if (_leaf != null)
+ return JsonUtils.Serialize(_leaf);
+ if (_array != null)
+ return "[" + String.Join(", ", _array.Select(i => JsonUtils.Serialize(i))) + "]";
+ return "{}";
+ }
+
+ public bool IsEmpty()
+ {
+ if (_leaf != null) return false;
+ return _branches == null || _branches.Keys.Count == 0;
}
}
}
diff --git a/SendGrid/SendGridMail/IHeader.cs b/SendGrid/SendGridMail/IHeader.cs index a9464ed..f55eadd 100755 --- a/SendGrid/SendGridMail/IHeader.cs +++ b/SendGrid/SendGridMail/IHeader.cs @@ -6,16 +6,64 @@ using System.Text; namespace SendGridMail
{
+ /// <summary>
+ /// Represents the additional functionality to add SendGrid specific mail headers
+ /// </summary>
public interface IHeader
{
- void AddTo(IEnumerable<String> recipients);
+ /// <summary>
+ /// This adds a substitution value to be used during the mail merge. Substitutions
+ /// will happen in order added, so calls to this should match calls to addTo in the mail message.
+ /// </summary>
+ /// <param name="tag">string to be replaced in the message</param>
+ /// <param name="substitutions">substitutions to be made, one per recipient</param>
void AddSubVal(String tag, IEnumerable<String> substitutions);
+
+ /// <summary>
+ /// This adds parameters and values that will be bassed back through SendGrid's
+ /// Event API if an event notification is triggered by this email.
+ /// </summary>
+ /// <param name="identifiers">parameter value pairs to be passed back on event notification</param>
void AddUniqueIdentifier(IDictionary<String, String> identifiers);
+
+ /// <summary>
+ /// This sets the category for this email. Statistics are stored on a per category
+ /// basis, so this can be useful for tracking on a per group basis.
+ /// </summary>
+ /// <param name="category">categories applied to the message</param>
void SetCategory(String category);
+
+ /// <summary>
+ /// Shortcut method for enabling a filter.
+ /// </summary>
+ /// <param name="filter">The name of the filter to enable</param>
void Enable(String filter);
+
+ /// <summary>
+ /// Shortcut method for disabling a filter.
+ /// </summary>
+ /// <param name="filter">The name of the filter to disable</param>
void Disable(String filter);
+
+ /// <summary>
+ /// Allows you to specify a filter setting. You can find a list of filters and settings here:
+ /// http://docs.sendgrid.com/documentation/api/web-api/filtersettings/
+ /// </summary>
+ /// <param name="filter">The name of the filter to set</param>
+ /// <param name="settings">The multipart name of the parameter being set</param>
+ /// <param name="value">The value that the settings name will be assigning</param>
void AddFilterSetting(String filter, IEnumerable<String> settings, String value);
+
+ /// <summary>
+ /// Attaches the SendGrid headers to the MIME.
+ /// </summary>
+ /// <param name="mime">the MIME to which we are attaching</param>
void AddHeader(MailMessage mime);
+
+ /// <summary>
+ /// Converts the filter settings into a JSON string.
+ /// </summary>
+ /// <returns>String representation of the SendGrid headers</returns>
String AsJson();
}
}
|