summaryrefslogtreecommitdiffstats
path: root/SendGrid/SendGridMail/Header.cs
diff options
context:
space:
mode:
Diffstat (limited to 'SendGrid/SendGridMail/Header.cs')
-rwxr-xr-xSendGrid/SendGridMail/Header.cs96
1 files changed, 78 insertions, 18 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;
}
}
}