Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

code cleanup and bug fixes

  • Loading branch information...
commit a165b7046a6d7ea886101ca8abcfcbb7081d6661 1 parent 9027025
Billy Coover authored
39 test.twilio.clr/StoredProceduresTest.cs
View
@@ -1,25 +1,12 @@
-using System;
-using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
using twilio.clr;
namespace test.twilio.clr
{
- [TestClass()]
+ [TestClass]
public class StoredProceduresTest
{
- private TestContext testContextInstance;
-
- public TestContext TestContext
- {
- get
- {
- return testContextInstance;
- }
- set
- {
- testContextInstance = value;
- }
- }
+ public TestContext TestContext { get; set; }
#region Additional test attributes
//
@@ -51,19 +38,19 @@ public TestContext TestContext
//
#endregion
- [TestMethod()]
+ [TestMethod]
public void SendSMSMessageTest()
{
- string apiVersion = "2010-04-01";
- string accountSid = "fake_sid";
- string authToken = "fake_auth_token";
- string from = "fake_from";
- string to = "fake_to";
- string body = "fake_body";
- string sid = String.Empty; //sid for new text message
- string sidExpected = "SM90c6fc909d8504d45ecdb3a3d5b3556e"; // sid from sample xml http://www.twilio.com/docs/api/rest/sending-smsdocumentation
+ const string apiVersion = "2010-04-01";
+ const string accountSid = "fake_sid";
+ const string authToken = "fake_auth_token";
+ const string @from = "fake_from";
+ const string to = "fake_to";
+ const string body = "fake_body";
+ string sid; //sid for new text message
+ const string sidExpected = "SM90c6fc909d8504d45ecdb3a3d5b3556e"; // sid from sample xml http://www.twilio.com/docs/api/rest/sending-smsdocumentation
- StoredProcedures procedures = new StoredProcedures(new Account_Mock());
+ var procedures = new StoredProcedures(new AccountMock());
procedures.SendSMSMessage(apiVersion, accountSid, authToken, from, to, body, out sid);
Assert.AreEqual(sidExpected, sid);
}
212 twilio.clr/Account.cs
View
@@ -1,159 +1,149 @@
using System;
using System.Collections;
using System.IO;
+using System.Linq;
using System.Net;
using System.Text;
-public class Account : IAccount
+namespace twilio.clr
{
- const string TWILIO_API_URL = "https://api.twilio.com";
-
- private string id;
- private string token;
-
- public Account(string id, string token)
+ public class Account : IAccount
{
- this.id = id;
- this.token = token;
- }
+ const string TwilioApiUrl = "https://api.twilio.com";
- private string _download(string uri, Hashtable vars)
- {
- // 1. format query string
- if (vars != null)
+ private readonly string _id;
+ private readonly string _token;
+
+ public Account(string id, string token)
{
- string query = "";
- foreach (DictionaryEntry d in vars)
- query += "&" + d.Key.ToString() + "=" + d.Value.ToString();
- if (query.Length > 0)
- uri = uri + "?" + query.Substring(1);
+ _id = id;
+ _token = token;
}
- // 2. setup basic authenication
- string authstring = Convert.ToBase64String(
- Encoding.ASCII.GetBytes(String.Format("{0}:{1}",
- this.id, this.token)));
-
- // 3. perform GET using WebClient
- WebClient client = new WebClient();
- client.Headers.Add("Authorization",
- String.Format("Basic {0}", authstring));
- byte[] resp = client.DownloadData(uri);
-
- return Encoding.ASCII.GetString(resp);
- }
-
- private string _upload(string uri, string method, Hashtable vars)
- {
- // 1. format body data
- string data = "";
- if (vars != null)
+ private string Download(string uri, Hashtable vars)
{
- foreach (DictionaryEntry d in vars)
+ // 1. format query string
+ if (vars != null)
{
- data += d.Key.ToString() + "=" + d.Value.ToString() + "&";
+ var query = vars.Cast<DictionaryEntry>().Aggregate("", (current, d) => current + ("&" + d.Key.ToString() + "=" + d.Value.ToString()));
+ if (query.Length > 0)
+ uri = uri + "?" + query.Substring(1);
}
- }
+ // 2. setup basic authenication
+ var authstring = Convert.ToBase64String(
+ Encoding.ASCII.GetBytes(String.Format("{0}:{1}",
+ _id, _token)));
- // 2. setup basic authenication
- string authstring = Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", this.id, this.token)));
+ // 3. perform GET using WebClient
+ var client = new WebClient();
+ client.Headers.Add("Authorization",
+ String.Format("Basic {0}", authstring));
+ var resp = client.DownloadData(uri);
- // 3. perform POST/PUT/DELETE using WebClient
- ServicePointManager.Expect100Continue = false;
- Byte[] postbytes = Encoding.ASCII.GetBytes(data);
- WebClient client = new WebClient();
-
- client.Headers.Add("Authorization", String.Format("Basic {0}", authstring));
- client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
+ return Encoding.ASCII.GetString(resp);
+ }
- byte[] resp = client.UploadData(uri, method, postbytes);
+ private string Upload(string uri, string method, Hashtable vars)
+ {
+ // 1. format body data
+ var data = "";
+ if (vars != null)
+ {
+ data = vars.Cast<DictionaryEntry>().Aggregate(data, (current, d) => current + (d.Key.ToString() + "=" + d.Value.ToString() + "&"));
+ }
- return Encoding.ASCII.GetString(resp);
- }
+ // 2. setup basic authenication
+ var authstring = Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", _id, _token)));
- private string _build_uri(string path)
- {
- if (path[0] == '/')
- return TWILIO_API_URL + path;
- else
- return TWILIO_API_URL + "/" + path;
- }
+ // 3. perform POST/PUT/DELETE using WebClient
+ ServicePointManager.Expect100Continue = false;
+ var postbytes = Encoding.ASCII.GetBytes(data);
+ var client = new WebClient();
- public string request(string path, string method, Hashtable vars)
- {
- string response = null;
+ client.Headers.Add("Authorization", String.Format("Basic {0}", authstring));
+ client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
- if (path == null || path.Length <= 0)
- throw (new ArgumentException("Invalid path parameter"));
+ var resp = client.UploadData(uri, method, postbytes);
- method = method.ToUpper();
- if (method == null || (method != "GET" && method != "POST" && method != "PUT" && method != "DELETE"))
- {
- throw (new ArgumentException("Invalid method parameter"));
+ return Encoding.ASCII.GetString(resp);
}
- if (method != "GET" && vars.Count <= 0)
+ private string _build_uri(string path)
{
- throw (new ArgumentException("No vars parameters"));
+ if (path[0] == '/')
+ return TwilioApiUrl + path;
+ return TwilioApiUrl + "/" + path;
}
- string url = _build_uri(path);
- try
+ public string Request(string path, string method, Hashtable vars)
{
- if (method == "GET")
- {
- response = _download(url, vars);
- }
- else
+ string response;
+
+ if (path == null || path.Length <= 0)
+ throw (new ArgumentException("Invalid path parameter"));
+
+ method = method.ToUpper();
+ if (method == null || (method != "GET" && method != "POST" && method != "PUT" && method != "DELETE"))
{
- response = _upload(url, method, vars);
+ throw (new ArgumentException("Invalid method parameter"));
}
- }
- catch (WebException e)
- {
- string message = e.Message;
- switch (e.Status)
+ if (method != "GET" && vars.Count <= 0)
{
- case WebExceptionStatus.TrustFailure:
- message = "You do not trust the people who issued the certificate being used by twiliorest.dll.";
- break;
+ throw (new ArgumentException("No vars parameters"));
}
- string var_list = "";
- foreach (DictionaryEntry d in vars)
+ var url = _build_uri(path);
+ try
{
- var_list += "&" + d.Key.ToString() + "=" + d.Value.ToString();
+ response = method == "GET" ? Download(url, vars) : Upload(url, method, vars);
}
-
-
- string response_str = "";
- using (StreamReader sr = new StreamReader(e.Response.GetResponseStream()))
+ catch (WebException e)
{
- string line;
- while ((line = sr.ReadLine()) != null)
+ var message = e.Message;
+
+ switch (e.Status)
{
- response_str += line;
+ case WebExceptionStatus.TrustFailure:
+ message = "You do not trust the people who issued the certificate being used by twiliorest.dll.";
+ break;
}
- }
+ var varList = vars.Cast<DictionaryEntry>().Aggregate("", (current, d) => current + ("&" + d.Key.ToString() + "=" + d.Value.ToString()));
- message = String.Format("TwilioRestException occurred in the request you sent: \n{0}\n\tURLL: {1}\n\tMETHOD:{2}\n\tVARS:{3}\n\tRESPONSE:{4}",
- message,
- url,
- method,
- var_list,
- response_str);
- throw new TwilioRestException(message, e);
- }
+ var responseStr = "";
+ Stream responseStream = null;
+ if (e.Response != null)
+ responseStream = e.Response.GetResponseStream();
+ if (responseStream != null)
+ using (var sr = new StreamReader(responseStream))
+ {
+ string line;
+ while ((line = sr.ReadLine()) != null)
+ {
+ responseStr += line;
+ }
+ }
- return response;
- }
- public string request(string path, string method)
- {
- return this.request(path, method, null);
+ message = String.Format("TwilioRestException occurred in the request you sent: \n{0}\n\tURLL: {1}\n\tMETHOD:{2}\n\tVARS:{3}\n\tRESPONSE:{4}",
+ message,
+ url,
+ method,
+ varList,
+ responseStr);
+
+ throw new TwilioRestException(message, e);
+ }
+
+ return response;
+ }
+
+ public string Request(string path, string method)
+ {
+ return Request(path, method, null);
+ }
}
}
6 twilio.clr/Account_Mock.cs
View
@@ -2,14 +2,14 @@
namespace twilio.clr
{
- public class Account_Mock : IAccount
+ public class AccountMock : IAccount
{
- public string request(string path, string method)
+ public string Request(string path, string method)
{
throw new NotImplementedException();
}
- public string request(string path, string method, System.Collections.Hashtable vars)
+ public string Request(string path, string method, System.Collections.Hashtable vars)
{
if (path.Contains("") && method == "POST")
{
11 twilio.clr/IAccount.cs
View
@@ -1,7 +1,8 @@
-using System;
-
-public interface IAccount
+namespace twilio.clr
{
- string request(string path, string method);
- string request(string path, string method, System.Collections.Hashtable vars);
+ public interface IAccount
+ {
+ string Request(string path, string method);
+ string Request(string path, string method, System.Collections.Hashtable vars);
+ }
}
66 twilio.clr/PhoneAutomation.cs
View
@@ -4,51 +4,47 @@
using System.Xml;
using Microsoft.SqlServer.Server;
-public partial class StoredProcedures
+namespace twilio.clr
{
- const String IsoCountryCode = "US";
- const String AccountSid = "fake_sid";
- const String AuthToken = "fake_auth_token";
-
- private IAccount account;
-
- public StoredProcedures()
- : this(new Account(AccountSid, AuthToken))
+ public class StoredProcedures
{
- }
+ const String AccountSid = "fake_sid";
+ const String AuthToken = "fake_auth_token";
- public StoredProcedures(IAccount account) {
- this.account = account;
- }
+ private readonly IAccount _account;
- [SqlProcedure]
- //public static void SendSMSMessage(String apiVersion, String accountSid, String authToken, String from, String to, String body, out String sid)
- public void SendSMSMessage(String apiVersion, String accountSid, String authToken, String from, String to, String body, out String sid)
- {
- if (String.IsNullOrEmpty(apiVersion) || String.IsNullOrEmpty(authToken)) {
- throw new ArgumentNullException("api version and auth token required");
+ public StoredProcedures()
+ : this(new Account(AccountSid, AuthToken))
+ {
}
- if (String.IsNullOrEmpty(from) || String.IsNullOrEmpty(to) || String.IsNullOrEmpty(body)) {
- throw new ArgumentNullException("missing from, to, or body");
+
+ public StoredProcedures(IAccount account) {
+ _account = account;
}
- Hashtable parameters = new Hashtable();
- parameters.Add("From", from);
- parameters.Add("To", to);
- parameters.Add("Body", body);
+ [SqlProcedure]
+ //public static void SendSMSMessage(String apiVersion, String accountSid, String authToken, String from, String to, String body, out String sid)
+ public void SendSMSMessage(String apiVersion, String accountSid, String authToken, String from, String to, String body, out String sid)
+ {
+ if (String.IsNullOrEmpty(apiVersion) || String.IsNullOrEmpty(authToken) || String.IsNullOrEmpty(from) || String.IsNullOrEmpty(to) || String.IsNullOrEmpty(body)) {
+ throw new ArgumentNullException();
+ }
- //Havent figured out how to use unit testing with static methods so for now, just toggle the comments for the method signature
- //and the account instantiation below
+ var parameters = new Hashtable {{"From", from}, {"To", to}, {"Body", body}};
- //var account = new Account(accountSid, authToken);
- var twilioResponse = account.request(String.Format("/{0}/Accounts/{1}/SMS/Messages", apiVersion, accountSid), "POST", parameters);
+ //Havent figured out how to use unit testing with static methods so for now, just toggle the comments for the method signature
+ //and the account instantiation below
- using (StringReader stream = new StringReader(twilioResponse))
- {
- XmlDocument doc = new XmlDocument();
- doc.Load(stream);
- XmlNodeList nodes = doc.GetElementsByTagName("Sid");
- sid = nodes[0].InnerText;
+ //var account = new Account(accountSid, authToken);
+ var twilioResponse = _account.Request(String.Format("/{0}/Accounts/{1}/SMS/Messages", apiVersion, accountSid), "POST", parameters);
+
+ using (var stream = new StringReader(twilioResponse))
+ {
+ var doc = new XmlDocument();
+ doc.Load(stream);
+ var nodes = doc.GetElementsByTagName("Sid");
+ sid = nodes[0].InnerText;
+ }
}
}
}
42 twilio.clr/TwilioRestException.cs
View
@@ -1,33 +1,35 @@
using System;
-[Serializable()]
-public class TwilioRestException : System.Exception
+namespace twilio.clr
{
-
- public TwilioRestException()
- : base()
+ [Serializable]
+ public class TwilioRestException : Exception
{
- }
+ public TwilioRestException()
+ {
- public TwilioRestException(string message)
- : base(message)
- {
+ }
- }
+ public TwilioRestException(string message)
+ : base(message)
+ {
- public TwilioRestException(string message,
- System.Exception innerException)
- : base(message, innerException)
- {
+ }
- }
+ public TwilioRestException(string message,
+ Exception innerException)
+ : base(message, innerException)
+ {
- protected TwilioRestException(System.Runtime.Serialization.SerializationInfo info,
- System.Runtime.Serialization.StreamingContext context)
- : base(info, context)
- {
+ }
- }
+ protected TwilioRestException(System.Runtime.Serialization.SerializationInfo info,
+ System.Runtime.Serialization.StreamingContext context)
+ : base(info, context)
+ {
+ }
+
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.