diff --git a/csharp/StatHat.cs b/csharp/StatHat.cs index 7d77c29..7394c3f 100644 --- a/csharp/StatHat.cs +++ b/csharp/StatHat.cs @@ -1,8 +1,7 @@ -using System; -using System.Net; -using System.IO; -using System.Text; +using System; using System.Collections.Generic; +using System.IO; +using System.Net; namespace StatHat { @@ -11,7 +10,6 @@ namespace StatHat public static class Post { - // =================== // How to use StatHat: // =================== @@ -68,15 +66,16 @@ public static class Post /// the number to increment public static void Counter(string key, string ukey, float count) { - Dictionary p = new Dictionary(); - p.Add("key", key); - p.Add("ukey", ukey); - p.Add("count", count.ToString()); - p.Add("vb", "1"); + var p = new Dictionary + { + { "key", key }, + { "ukey", ukey }, + { "count", count.ToString() }, + { "vb", "1" } + }; new FormPoster(Post.BaseUrl, "/c", p); } - /// /// Posts a counter increment to stathat over HTTP /// @@ -86,32 +85,37 @@ public static void Counter(string key, string ukey, float count) public static void Counter(string key, string ukey, int count) { Post.Counter(key, ukey, (float)count); - } + /// - /// Posts a value to stathat over HTTP + /// Posts a counter increment to stathat over HTTP /// /// the stat's posting key /// your user key - /// the number - public static void Value(string key, string ukey, float value) + /// the number to increment + /// the function you'd like called with the reply from stathat's server + public static void Counter(string key, string ukey, float count, ReplyDelegate replyDelegate) { - Dictionary p = new Dictionary(); - p.Add("key", key); - p.Add("ukey", ukey); - p.Add("value", value.ToString()); - p.Add("vb", "1"); - new FormPoster(Post.BaseUrl, "/v", p); + var p = new Dictionary + { + { "key", key }, + { "ukey", ukey }, + { "count", count.ToString() }, + { "vb", "1" } + }; + new FormPoster(Post.BaseUrl, "/c", p, replyDelegate); } + /// - /// Posts a value to stathat over HTTP + /// Posts a counter increment to stathat over HTTP /// /// the stat's posting key /// your user key - /// the number - public static void Value(string key, string ukey, int value) + /// the number to increment + /// the function you'd like called with the reply from stathat's server + public static void Counter(string key, string ukey, int count, ReplyDelegate replyDelegate) { - Post.Value(key, ukey, (float)value); + Post.Counter(key, ukey, (float)count, replyDelegate); } /// @@ -122,11 +126,13 @@ public static void Value(string key, string ukey, int value) /// the number to increment public static void EzCounter(string ezkey, string stat, float count) { - Dictionary p = new Dictionary(); - p.Add("ezkey", ezkey); - p.Add("stat", stat); - p.Add("count", count.ToString()); - p.Add("vb", "1"); + var p = new Dictionary + { + { "ezkey", ezkey }, + { "stat", stat }, + { "count", count.ToString() }, + { "vb", "1" } + }; new FormPoster(Post.BaseUrl, "/ez", p); } @@ -141,6 +147,37 @@ public static void EzCounter(string ezkey, string stat, int count) Post.EzCounter(ezkey, stat, (float)count); } + /// + /// Posts a counter increment to stathat over HTTP using ez API - the stat and/or you don't have to be pre-registered + /// + /// your ezkey (defaults to email address). If you already have a stathat account, use the one associated with it. + /// the name for your stat + /// the number to increment + /// the function you'd like called with the reply from stathat's server + public static void EzCounter(string ezkey, string stat, float count, ReplyDelegate replyDelegate) + { + var p = new Dictionary + { + { "ezkey", ezkey }, + { "stat", stat }, + { "count", count.ToString() }, + { "vb", "1" } + }; + new FormPoster(Post.BaseUrl, "/ez", p, replyDelegate); + } + + /// + /// Posts a counter increment to stathat over HTTP using ez API - the stat and/or you don't have to be pre-registered + /// + /// your ezkey (defaults to email address). If you already have a stathat account, use the one associated with it. + /// the name for your stat + /// the number to increment + /// the function you'd like called with the reply from stathat's server + public static void EzCounter(string ezkey, string stat, int count, ReplyDelegate replyDelegate) + { + Post.EzCounter(ezkey, stat, (float)count, replyDelegate); + } + /// /// Posts a counter increment to stathat over HTTP using ez API - the stat and/or you don't have to be pre-registered /// @@ -149,11 +186,13 @@ public static void EzCounter(string ezkey, string stat, int count) /// the number public static void EzValue(string ezkey, string stat, float value) { - Dictionary p = new Dictionary(); - p.Add("ezkey", ezkey); - p.Add("stat", stat); - p.Add("value", value.ToString()); - p.Add("vb", "1"); + var p = new Dictionary + { + { "ezkey", ezkey }, + { "stat", stat }, + { "value", value.ToString() }, + { "vb", "1" } + }; new FormPoster(Post.BaseUrl, "/ez", p); } @@ -165,200 +204,182 @@ public static void EzValue(string ezkey, string stat, float value) /// the number public static void EzValue(string ezkey, string stat, int value) { - Post.EzValue(ezkey, stat, (float) value); + Post.EzValue(ezkey, stat, (float)value); } /// - /// Posts a counter increment to stathat over HTTP + /// Posts a counter increment to stathat over HTTP using ez API - the stat and/or you don't have to be pre-registered /// - /// the stat's posting key - /// your user key - /// the number to increment + /// your ezkey (defaults to email address). If you already have a stathat account, use the one associated with it. + /// the name for your stat + /// the number /// the function you'd like called with the reply from stathat's server - public static void Counter(string key, string ukey, float count, ReplyDelegate replyDelegate) + public static void EzValue(string ezkey, string stat, float value, ReplyDelegate replyDelegate) { - Dictionary p = new Dictionary(); - p.Add("key", key); - p.Add("ukey", ukey); - p.Add("count", count.ToString()); - p.Add("vb", "1"); - new FormPoster(Post.BaseUrl, "/c", p, replyDelegate); + var p = new Dictionary + { + { "ezkey", ezkey }, + { "stat", stat }, + { "value", value.ToString() }, + { "vb", "1" } + }; + new FormPoster(Post.BaseUrl, "/ez", p, replyDelegate); } + /// - /// Posts a counter increment to stathat over HTTP + /// Posts a counter increment to stathat over HTTP using ez API - the stat and/or you don't have to be pre-registered /// - /// the stat's posting key - /// your user key - /// the number to increment + /// your ezkey (defaults to email address). If you already have a stathat account, use the one associated with it. + /// the name for your stat + /// the number /// the function you'd like called with the reply from stathat's server - public static void Counter(string key, string ukey, int count, ReplyDelegate replyDelegate) + public static void EzValue(string ezkey, string stat, int value, ReplyDelegate replyDelegate) { - Post.Counter(key, ukey, (float)count, replyDelegate); - + Post.EzValue(ezkey, stat, (float)value, replyDelegate); } + /// /// Posts a value to stathat over HTTP /// /// the stat's posting key /// your user key /// the number - /// the function you'd like called with the reply from stathat's server - public static void Value(string key, string ukey, float value, ReplyDelegate replyDelegate) + public static void Value(string key, string ukey, float value) { - Dictionary p = new Dictionary(); - p.Add("key", key); - p.Add("ukey", ukey); - p.Add("value", value.ToString()); - p.Add("vb", "1"); - new FormPoster(Post.BaseUrl, "/v", p, replyDelegate); + var p = new Dictionary + { + { "key", key }, + { "ukey", ukey }, + { "value", value.ToString() }, + { "vb", "1" } + }; + new FormPoster(Post.BaseUrl, "/v", p); } + /// /// Posts a value to stathat over HTTP /// /// the stat's posting key /// your user key /// the number - /// the function you'd like called with the reply from stathat's server - public static void Value(string key, string ukey, int value, ReplyDelegate replyDelegate) + public static void Value(string key, string ukey, int value) { - Post.Value(key, ukey, (float)value, replyDelegate); + Post.Value(key, ukey, (float)value); } /// - /// Posts a counter increment to stathat over HTTP using ez API - the stat and/or you don't have to be pre-registered + /// Posts a value to stathat over HTTP /// - /// your ezkey (defaults to email address). If you already have a stathat account, use the one associated with it. - /// the name for your stat - /// the number to increment + /// the stat's posting key + /// your user key + /// the number /// the function you'd like called with the reply from stathat's server - public static void EzCounter(string ezkey, string stat, float count, ReplyDelegate replyDelegate) + public static void Value(string key, string ukey, float value, ReplyDelegate replyDelegate) { - Dictionary p = new Dictionary(); - p.Add("ezkey", ezkey); - p.Add("stat", stat); - p.Add("count", count.ToString()); - p.Add("vb", "1"); - new FormPoster(Post.BaseUrl, "/ez", p, replyDelegate); + var p = new Dictionary + { + { "key", key }, + { "ukey", ukey }, + { "value", value.ToString() }, + { "vb", "1" } + }; + new FormPoster(Post.BaseUrl, "/v", p, replyDelegate); } /// - /// Posts a counter increment to stathat over HTTP using ez API - the stat and/or you don't have to be pre-registered + /// Posts a value to stathat over HTTP /// - /// your ezkey (defaults to email address). If you already have a stathat account, use the one associated with it. - /// the name for your stat - /// the number to increment + /// the stat's posting key + /// your user key + /// the number /// the function you'd like called with the reply from stathat's server - public static void EzCounter(string ezkey, string stat, int count, ReplyDelegate replyDelegate) + public static void Value(string key, string ukey, int value, ReplyDelegate replyDelegate) { - Post.EzCounter(ezkey, stat, (float)count, replyDelegate); + Post.Value(key, ukey, (float)value, replyDelegate); } - /// - /// Posts a counter increment to stathat over HTTP using ez API - the stat and/or you don't have to be pre-registered - /// - /// your ezkey (defaults to email address). If you already have a stathat account, use the one associated with it. - /// the name for your stat - /// the number - /// the function you'd like called with the reply from stathat's server - public static void EzValue(string ezkey, string stat, float value, ReplyDelegate replyDelegate) + private class FormPoster { - Dictionary p = new Dictionary(); - p.Add("ezkey", ezkey); - p.Add("stat", stat); - p.Add("value", value.ToString()); - p.Add("vb", "1"); - new FormPoster(Post.BaseUrl, "/ez", p, replyDelegate); - } + private readonly string _baseUrl; - /// - /// Posts a counter increment to stathat over HTTP using ez API - the stat and/or you don't have to be pre-registered - /// - /// your ezkey (defaults to email address). If you already have a stathat account, use the one associated with it. - /// the name for your stat - /// the number - /// the function you'd like called with the reply from stathat's server - public static void EzValue(string ezkey, string stat, int value, ReplyDelegate replyDelegate) - { - Post.EzValue(ezkey, stat, (float)value, replyDelegate); - } + private readonly Dictionary _parameters; - private class FormPoster - { - // Members - HttpWebRequest Request; - Dictionary Parameters; - ReplyDelegate Reply; - string RelUrl; - string BaseUrl; + private readonly string _relUrl; + private readonly ReplyDelegate _reply; + + // Members + private HttpWebRequest _request; // Methods public FormPoster(string base_url, string rel_url, Dictionary parameters, ReplyDelegate replyDelegate) { - this.BaseUrl = base_url; - this.Parameters = parameters; - this.Reply = replyDelegate; - this.RelUrl = rel_url; - this.PostForm(); + _baseUrl = base_url; + _parameters = parameters; + _reply = replyDelegate; + _relUrl = rel_url; + PostForm(); } + public FormPoster(string base_url, string rel_url, Dictionary parameters) { - this.BaseUrl = base_url; - this.Parameters = parameters; - this.Reply = new ReplyDelegate((rep) => { }); - this.RelUrl = rel_url; - this.PostForm(); + _baseUrl = base_url; + _parameters = parameters; + _reply = new ReplyDelegate((rep) => { }); + _relUrl = rel_url; + PostForm(); + } + + private string EncodeUriComponent(string s) + { + var res = s.Replace("&", "%26"); + res = res.Replace(" ", "%20"); + return res; } private void PostForm() { - this.Request = (HttpWebRequest)WebRequest.Create(this.BaseUrl + this.RelUrl); - Request.Method = "POST"; - Request.ContentType = "application/x-www-form-urlencoded"; - Request.BeginGetRequestStream(this.RequestCallback, Request); + _request = (HttpWebRequest)WebRequest.Create(_baseUrl + _relUrl); + _request.Method = "POST"; + _request.ContentType = "application/x-www-form-urlencoded"; + _request.BeginGetRequestStream(RequestCallback, _request); } + private void RequestCallback(IAsyncResult asyncResult) { try { - string postData = ""; - foreach (string key in this.Parameters.Keys) + var postData = ""; + foreach (var key in _parameters.Keys) { - postData += encodeUriComponent(key) + "=" + encodeUriComponent(this.Parameters[key]) + "&"; + postData += EncodeUriComponent(key) + "=" + EncodeUriComponent(_parameters[key]) + "&"; } - Stream newStream = Request.EndGetRequestStream(asyncResult); - StreamWriter streamWriter = new StreamWriter(newStream); + var newStream = _request.EndGetRequestStream(asyncResult); + var streamWriter = new StreamWriter(newStream); streamWriter.Write(postData); streamWriter.Close(); - this.Request.BeginGetResponse(this.ResponseCallback, this.Request); + _request.BeginGetResponse(ResponseCallback, _request); } catch (Exception e) { - this.Reply(e.Message); + _reply(e.Message); } finally { } } - private string encodeUriComponent(string s) - { - string res = s.Replace("&", "%26"); - res = res.Replace(" ","%20"); - return res; - } - private void ResponseCallback(IAsyncResult asyncResult) { try { - WebResponse response = this.Request.EndGetResponse(asyncResult); - Stream dataStream = response.GetResponseStream(); - StreamReader reader = new StreamReader(dataStream); - string result = reader.ReadToEnd(); - this.Reply(result); + var response = _request.EndGetResponse(asyncResult); + var dataStream = response.GetResponseStream(); + var reader = new StreamReader(dataStream); + var result = reader.ReadToEnd(); + _reply(result); } catch (Exception e) { - this.Reply(e.Message); + _reply(e.Message); } finally { } }