diff options
author | Tyler Bischel <tyler.bischel@sendgrid.com> | 2012-01-10 17:33:13 -0800 |
---|---|---|
committer | Tyler Bischel <tyler.bischel@sendgrid.com> | 2012-01-10 17:33:13 -0800 |
commit | b13f35419a29ace0ecaabff02552a0613aa8afe2 (patch) | |
tree | 83dba1149b6f90d53935186ee0a601d851a50647 | |
parent | 7251a831e634eda2f98885777468b9f5aad6cd00 (diff) | |
download | sendgrid-csharp-b13f35419a29ace0ecaabff02552a0613aa8afe2.zip sendgrid-csharp-b13f35419a29ace0ecaabff02552a0613aa8afe2.tar.gz sendgrid-csharp-b13f35419a29ace0ecaabff02552a0613aa8afe2.tar.bz2 |
example project sends email, tree node for filters created, json encoding done, other fixes
-rwxr-xr-x | SendGrid/Example/Program.cs | 13 | ||||
-rwxr-xr-x | SendGrid/SendGridMail/Header.cs | 64 | ||||
-rwxr-xr-x | SendGrid/SendGridMail/JsonUtils.cs | 23 | ||||
-rwxr-xr-x | SendGrid/SendGridMail/Mail.csproj | 3 | ||||
-rwxr-xr-x | SendGrid/SendGridMail/SendGrid.cs | 98 | ||||
-rwxr-xr-x | SendGrid/Tests/TestJsonUtils.cs | 22 | ||||
-rwxr-xr-x | SendGrid/Tests/TestTreeNode.cs | 57 | ||||
-rwxr-xr-x | SendGrid/Tests/Tests.csproj | 2 |
8 files changed, 227 insertions, 55 deletions
diff --git a/SendGrid/Example/Program.cs b/SendGrid/Example/Program.cs index abf67f0..3925174 100755 --- a/SendGrid/Example/Program.cs +++ b/SendGrid/Example/Program.cs @@ -1,8 +1,11 @@ using System;
using System.Collections.Generic;
using System.Linq;
+using System.Net;
using System.Net.Mail;
using System.Text;
+using SendGridMail;
+using SendGridMail.Transport;
namespace Example
{
@@ -10,7 +13,15 @@ namespace Example {
static void Main(string[] args)
{
-
+ var credentials = new NetworkCredential("cjbuchmann", "Gadget_15");
+ var transport = SMTP.GenerateInstance(credentials);
+ var header = new Header();
+ var message = new SendGrid(header);
+ message.AddTo("tyler.bischel@sendgrid.com");
+ message.From = new MailAddress("eric@sendgrid.com");
+ message.Text = "This is a test message.";
+ message.Subject = "hazaah!";
+ transport.Deliver(message);
}
}
}
diff --git a/SendGrid/SendGridMail/Header.cs b/SendGrid/SendGridMail/Header.cs index 2af4acd..6023214 100755 --- a/SendGrid/SendGridMail/Header.cs +++ b/SendGrid/SendGridMail/Header.cs @@ -2,7 +2,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
-using System.Text;
namespace SendGridMail
{
@@ -30,12 +29,12 @@ namespace SendGridMail public void Enable(string filter)
{
- throw new NotImplementedException();
+ AddFilterSetting(filter, new List<string>(){ "enable" }, "1");
}
public void Disable(string filter)
{
- throw new NotImplementedException();
+ AddFilterSetting(filter, new List<string>(){"enable"}, "0");
}
public void AddFilterSetting(string filter, IEnumerable<string> settings, string value)
@@ -50,7 +49,64 @@ namespace SendGridMail public String AsJson()
{
- throw new NotImplementedException();
+ return "";
+ }
+
+
+ internal class FilterNode
+ {
+ private Dictionary<String, FilterNode> _branches;
+ private String _leaf;
+
+ public FilterNode()
+ {
+ _branches = new Dictionary<string, FilterNode>();
+ }
+
+ public void AddSetting(List<String> keys, String value)
+ {
+ if (keys.Count == 0)
+ {
+ _leaf = value;
+ }
+ else
+ {
+ var key = keys[0];
+ if (!_branches.ContainsKey(key))
+ _branches[key] = new FilterNode();
+ var remainingKeys = keys.Skip(1).ToList();
+ _branches[key].AddSetting(remainingKeys, value);
+ }
+ }
+
+
+ public String GetSetting(params String[] keys)
+ {
+ return GetSetting(keys.ToList());
+ }
+
+ public String GetSetting(List<String> keys)
+ {
+ if (keys.Count == 0)
+ return _leaf;
+ var key = keys.First();
+ if(!_branches.ContainsKey(key))
+ throw new ArgumentException("Bad key path!");
+ var remainingKeys = keys.Skip(1).ToList();
+ return _branches[key].GetSetting(remainingKeys);
+ }
+
+ public String GetLeaf()
+ {
+ return _leaf;
+ }
+
+ public String ToJson()
+ {
+ if (_branches.Count > 0)
+ return "{" + String.Join(",", _branches.Keys.Select(k => '"' + k + '"' + ":" + _branches[k].ToJson())) + "}";
+ return JsonUtils.Serialize(_leaf);
+ }
}
}
}
diff --git a/SendGrid/SendGridMail/JsonUtils.cs b/SendGrid/SendGridMail/JsonUtils.cs new file mode 100755 index 0000000..d92b9dd --- /dev/null +++ b/SendGrid/SendGridMail/JsonUtils.cs @@ -0,0 +1,23 @@ +using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Runtime.Serialization.Json;
+using System.Text;
+
+namespace SendGridMail
+{
+ public class JsonUtils
+ {
+ public static string Serialize<T>(T obj)
+ {
+ var serializer = new DataContractJsonSerializer(obj.GetType());
+ using (var stream = new MemoryStream())
+ {
+ serializer.WriteObject(stream, obj);
+ var jsonData = Encoding.UTF8.GetString(stream.ToArray(), 0, (int)stream.Length);
+ return jsonData;
+ }
+ }
+ }
+}
diff --git a/SendGrid/SendGridMail/Mail.csproj b/SendGrid/SendGridMail/Mail.csproj index b5a9a9d..9272780 100755 --- a/SendGrid/SendGridMail/Mail.csproj +++ b/SendGrid/SendGridMail/Mail.csproj @@ -34,6 +34,8 @@ <Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net" />
+ <Reference Include="System.Runtime.Serialization" />
+ <Reference Include="System.ServiceModel.Web" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
@@ -50,6 +52,7 @@ <Compile Include="Transport\ITransport.cs" />
<Compile Include="Transport\REST.cs" />
<Compile Include="Transport\SMTP.cs" />
+ <Compile Include="JsonUtils.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
diff --git a/SendGrid/SendGridMail/SendGrid.cs b/SendGrid/SendGridMail/SendGrid.cs index 33f809b..f70cec5 100755 --- a/SendGrid/SendGridMail/SendGrid.cs +++ b/SendGrid/SendGridMail/SendGrid.cs @@ -12,21 +12,12 @@ namespace SendGridMail {
public class SendGrid : ISendGrid
{
- private IHeader header;
-
private Dictionary<String, String> _filters;
//apps list and settings
private const String ReText = @"<\%\s*\%>";
private const String ReHtml = @"<\%\s*[^\s]+\s*\%>";
- public SendGrid(IHeader header)
- {
- this.header = header;
-
- //initialize the filters, for use within the library
- this.InitializeFilters();
- }
public void InitializeFilters()
{
@@ -76,6 +67,8 @@ namespace SendGridMail From = from;
To = to;
+ Cc = cc;
+ Bcc = bcc;
_subs = new Dictionary<string, string>();
@@ -86,6 +79,15 @@ namespace SendGridMail Html = html;
}
+ public SendGrid(IHeader header)
+ {
+ message = new MailMessage();
+ Header = header;
+
+ //initialize the filters, for use within the library
+ this.InitializeFilters();
+ }
+
#region Properties
public MailAddress From
{
@@ -159,11 +161,7 @@ namespace SendGridMail }
}
- public IHeader Header
- {
- get { throw new NotImplementedException(); }
- set { throw new NotImplementedException(); }
- }
+ public IHeader Header { get; set; }
public String Html { get; set; }
public String Text { get; set; }
@@ -304,79 +302,79 @@ namespace SendGridMail #region SMTP API Functions
public void DisableGravatar()
{
- this.header.Disable(this._filters["Gravatar"]);
+ Header.Disable(this._filters["Gravatar"]);
}
public void DisableOpenTracking()
{
- this.header.Disable(this._filters["OpenTracking"]);
+ Header.Disable(this._filters["OpenTracking"]);
}
public void DisableClickTracking()
{
- this.header.Disable(this._filters["ClickTracking"]);
+ Header.Disable(this._filters["ClickTracking"]);
}
public void DisableSpamCheck()
{
- this.header.Disable(this._filters["SpamCheck"]);
+ Header.Disable(this._filters["SpamCheck"]);
}
public void DisableUnsubscribe()
{
- this.header.Disable(this._filters["Unsubscribe"]);
+ Header.Disable(this._filters["Unsubscribe"]);
}
public void DisableFooter()
{
- this.header.Disable(this._filters["Footer"]);
+ Header.Disable(this._filters["Footer"]);
}
public void DisableGoogleAnalytics()
{
- this.header.Disable(this._filters["GoogleAnalytics"]);
+ Header.Disable(this._filters["GoogleAnalytics"]);
}
public void DisableTemplate()
{
- this.header.Disable(this._filters["Template"]);
+ Header.Disable(this._filters["Template"]);
}
public void DisableBcc()
{
- this.header.Disable(this._filters["Bcc"]);
+ Header.Disable(this._filters["Bcc"]);
}
public void DisableBypassListManagement()
{
- this.header.Disable(this._filters["BypassListManagement"]);
+ Header.Disable(this._filters["BypassListManagement"]);
}
public void EnableGravatar()
{
- this.header.Enable(this._filters["Gravatar"]);
+ Header.Enable(this._filters["Gravatar"]);
}
public void EnableOpenTracking()
{
- this.header.Enable(this._filters["OpenTracking"]);
+ Header.Enable(this._filters["OpenTracking"]);
}
public void EnableClickTracking(string text = null)
{
var filter = this._filters["ClickTracking"];
- this.header.Enable(filter);
- this.header.AddFilterSetting(filter, new List<string>(){ "text" }, text);
+ Header.Enable(filter);
+ Header.AddFilterSetting(filter, new List<string>(){ "text" }, text);
}
public void EnableSpamCheck(int score = 5, string url = null)
{
var filter = this._filters["SpamCheck"];
- this.header.Enable(filter);
- this.header.AddFilterSetting(filter, new List<string>(){ "score" }, score.ToString(CultureInfo.InvariantCulture));
- this.header.AddFilterSetting(filter, new List<string>(){ "url" }, url);
+ Header.Enable(filter);
+ Header.AddFilterSetting(filter, new List<string>(){ "score" }, score.ToString(CultureInfo.InvariantCulture));
+ Header.AddFilterSetting(filter, new List<string>(){ "url" }, url);
}
public void EnableUnsubscribe(string text, string html, string replace, string url, string landing)
@@ -393,32 +391,32 @@ namespace SendGridMail throw new Exception("Missing substitution tag in html");
}
- this.header.Enable(filter);
- this.header.AddFilterSetting(filter, new List<string>(){ "text" }, text);
- this.header.AddFilterSetting(filter, new List<string>(){ "html" }, html);
- this.header.AddFilterSetting(filter, new List<string>(){ "replace"}, replace);
- this.header.AddFilterSetting(filter, new List<string>(){ "landing" }, landing);
+ Header.Enable(filter);
+ Header.AddFilterSetting(filter, new List<string>(){ "text" }, text);
+ Header.AddFilterSetting(filter, new List<string>(){ "html" }, html);
+ Header.AddFilterSetting(filter, new List<string>(){ "replace"}, replace);
+ Header.AddFilterSetting(filter, new List<string>(){ "landing" }, landing);
}
public void EnableFooter(string text = null, string html = null)
{
var filter = this._filters["Footer"];
- this.header.Enable(filter);
- this.header.AddFilterSetting(filter, new List<string>(){ "text" }, text);
- this.header.AddFilterSetting(filter, new List<string>(){ "html" }, html);
+ Header.Enable(filter);
+ Header.AddFilterSetting(filter, new List<string>(){ "text" }, text);
+ Header.AddFilterSetting(filter, new List<string>(){ "html" }, html);
}
public void EnableGoogleAnalytics(string source, string medium, string term, string content = null, string campaign = null)
{
var filter = this._filters["GoogleAnalytics"];
- this.header.Enable(filter);
- this.header.AddFilterSetting(filter, new List<string>(){ "source " }, source);
- this.header.AddFilterSetting(filter, new List<string>(){ "medium" }, medium);
- this.header.AddFilterSetting(filter, new List<string>(){ "term" }, term);
- this.header.AddFilterSetting(filter, new List<string>(){ "content" }, content);
- this.header.AddFilterSetting(filter, new List<string>(){ "compaign" }, campaign);
+ Header.Enable(filter);
+ Header.AddFilterSetting(filter, new List<string>(){ "source " }, source);
+ Header.AddFilterSetting(filter, new List<string>(){ "medium" }, medium);
+ Header.AddFilterSetting(filter, new List<string>(){ "term" }, term);
+ Header.AddFilterSetting(filter, new List<string>(){ "content" }, content);
+ Header.AddFilterSetting(filter, new List<string>(){ "compaign" }, campaign);
}
public void EnableTemplate(string html)
@@ -430,21 +428,21 @@ namespace SendGridMail throw new Exception("Missing substitution tag in html");
}
- this.header.Enable(filter);
- this.header.AddFilterSetting(filter, new List<string>(){ "html" }, html);
+ Header.Enable(filter);
+ Header.AddFilterSetting(filter, new List<string>(){ "html" }, html);
}
public void EnableBcc(string email)
{
var filter = this._filters["Bcc"];
- this.header.Enable(filter);
- this.header.AddFilterSetting(filter, new List<string>(){ "email" }, email);
+ Header.Enable(filter);
+ Header.AddFilterSetting(filter, new List<string>(){ "email" }, email);
}
public void EnableBypassListManagement()
{
- this.header.Enable(this._filters["BypassListManagement"]);
+ Header.Enable(this._filters["BypassListManagement"]);
}
#endregion
diff --git a/SendGrid/Tests/TestJsonUtils.cs b/SendGrid/Tests/TestJsonUtils.cs new file mode 100755 index 0000000..b06e153 --- /dev/null +++ b/SendGrid/Tests/TestJsonUtils.cs @@ -0,0 +1,22 @@ +using System.Text;
+using NUnit.Framework;
+using SendGridMail;
+
+namespace Tests
+{
+ [TestFixture]
+ public class TestJsonUtils
+ {
+ [Test]
+ public void TestSerialize()
+ {
+ Assert.AreEqual("1", JsonUtils.Serialize(1));
+ Assert.AreEqual("\"\\\"foo\\\"\"", JsonUtils.Serialize("\"foo\""));
+
+ var arg = Encoding.UTF8.GetString(Encoding.ASCII.GetBytes("добры дзень"));
+ var result = JsonUtils.Serialize(arg);
+ System.Console.WriteLine(arg + " => " + result);
+ Assert.AreEqual("", result);
+ }
+ }
+}
diff --git a/SendGrid/Tests/TestTreeNode.cs b/SendGrid/Tests/TestTreeNode.cs new file mode 100755 index 0000000..4b8a28f --- /dev/null +++ b/SendGrid/Tests/TestTreeNode.cs @@ -0,0 +1,57 @@ +using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using NUnit.Framework;
+using SendGridMail;
+
+namespace Tests
+{
+ [TestFixture]
+ public class TestTreeNode
+ {
+ [Test]
+ public void TestAddToTree()
+ {
+ var test = new Header.FilterNode();
+ test.AddSetting(new List<string>(), "foo");
+ Assert.AreEqual("foo", test.GetLeaf(), "Get the leaf of the first node");
+
+ test = new Header.FilterNode();
+ test.AddSetting(new List<string>() { "foo" }, "bar");
+ Assert.AreEqual("bar", test.GetSetting(new List<string>(){"foo"}), "Get the item in the first branch 'foo', make sure its set to 'bar'");
+
+ test = new Header.FilterNode();
+ test.AddSetting(new List<string>(){"foo"}, "bar");
+ Assert.AreEqual("bar", test.GetSetting("foo"), "tests the convienence get setting function that omits the lists stuff...");
+
+ test = new Header.FilterNode();
+ test.AddSetting(new List<string>() { "foo", "bar", "raz" }, "foobar");
+ Assert.AreEqual("foobar", test.GetSetting("foo", "bar", "raz"), "tests a tree that is multiple branches deep");
+
+
+ test = new Header.FilterNode();
+ test.AddSetting(new List<string>() { "foo", "bar", "raz" }, "foobar");
+ test.AddSetting(new List<string>() { "barfoo", "barbar", "barraz" }, "barfoobar");
+ Assert.AreEqual("foobar", test.GetSetting("foo", "bar", "raz"), "tests a tree that has multiple branches");
+ Assert.AreEqual("barfoobar", test.GetSetting("barfoo", "barbar", "barraz"), "tests the other branch");
+ }
+
+ [Test]
+ public void TestToJSON()
+ {
+ var test = new Header.FilterNode();
+ test.AddSetting(new List<string>() { "foo", "bar", "raz" }, "foobar");
+
+ var result = test.ToJson();
+ Assert.AreEqual("{\"foo\":{\"bar\":{\"raz\":\"foobar\"}}}", result);
+
+ test = new Header.FilterNode();
+ test.AddSetting(new List<string>() { "foo", "bar", "raz" }, "foobar");
+ test.AddSetting(new List<string>() { "barfoo", "barbar", "barraz" }, "barfoobar");
+
+ result = test.ToJson();
+ Assert.AreEqual("{\"foo\":{\"bar\":{\"raz\":\"foobar\"}},\"barfoo\":{\"barbar\":{\"barraz\":\"barfoobar\"}}}", result);
+ }
+ }
+}
diff --git a/SendGrid/Tests/Tests.csproj b/SendGrid/Tests/Tests.csproj index 86ce660..71b0db4 100755 --- a/SendGrid/Tests/Tests.csproj +++ b/SendGrid/Tests/Tests.csproj @@ -54,7 +54,9 @@ <ItemGroup>
<Compile Include="TestHeader.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="TestJsonUtils.cs" />
<Compile Include="TestSendgridMessageSetup.cs" />
+ <Compile Include="TestTreeNode.cs" />
<Compile Include="Transport\TestREST.cs" />
<Compile Include="Transport\TestSMTP.cs" />
</ItemGroup>
|