From 4b694c0c3130e9d5807a0b4b6bc15d60057b5bff Mon Sep 17 00:00:00 2001 From: high Date: Mon, 5 Sep 2011 14:15:46 -0400 Subject: [PATCH] Moved classes out of Rest.cs into their own files. --- TShockAPI/Rest/Rest.cs | 105 ---------------------------------- TShockAPI/Rest/RestCommand.cs | 47 +++++++++++++++ TShockAPI/Rest/RestObject.cs | 61 ++++++++++++++++++++ TShockAPI/Rest/RestVerbs.cs | 40 +++++++++++++ TShockAPI/TShockAPI.csproj | 5 +- 5 files changed, 152 insertions(+), 106 deletions(-) create mode 100644 TShockAPI/Rest/RestCommand.cs create mode 100644 TShockAPI/Rest/RestObject.cs create mode 100644 TShockAPI/Rest/RestVerbs.cs diff --git a/TShockAPI/Rest/Rest.cs b/TShockAPI/Rest/Rest.cs index 1a91cf88..aa668f01 100644 --- a/TShockAPI/Rest/Rest.cs +++ b/TShockAPI/Rest/Rest.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Net; using System.Text; using System.Text.RegularExpressions; @@ -136,108 +135,4 @@ namespace Rests #endregion } - - public class RestVerbs : Dictionary - { - - } - - public class RestObject : Dictionary - { - public string Status - { - get { return this["status"]; } - set { this["status"] = value; } - } - public string Error - { - get { return this["error"]; } - set { this["error"] = value; } - } - public string Response - { - get { return this["response"]; } - set { this["response"] = value; } - } - - public RestObject(string status) - { - Status = status; - } - - /// - /// Gets value safely, if it does not exist, return null. Sets/Adds value safely, if null it will remove. - /// - /// - /// - /// Returns null if key does not exist. - public new string this[string key] - { - get - { - string ret; - if (TryGetValue(key, out ret)) - return ret; - return null; - } - set - { - if (!ContainsKey(key)) - { - if (value == null) - return; - Add(key, value); - } - else - { - if (value != null) - base[key] = value; - else - Remove(key); - } - } - } - } - - public class RestCommand - { - public string Name { get; protected set; } - public string UriTemplate { get; protected set; } - public string UriVerbMatch { get; protected set; } - public string[] UriVerbs { get; protected set; } - public RestCommandD Callback { get; protected set; } - public bool RequiesToken { get; set; } - - /// - /// - /// - /// Used for identification - /// Url template - /// Rest Command callback - public RestCommand(string name, string uritemplate, RestCommandD callback) - { - Name = name; - UriTemplate = uritemplate; - UriVerbMatch = string.Join("([^/]*)", Regex.Split(uritemplate, "\\{[^\\{\\}]*\\}")); - var matches = Regex.Matches(uritemplate, "\\{([^\\{\\}]*)\\}"); - UriVerbs = (from Match match in matches select match.Groups[1].Value).ToArray(); - Callback = callback; - RequiesToken = true; - } - /// - /// - /// - /// Url template - /// Rest Command callback - public RestCommand(string uritemplate, RestCommandD callback) - : this(string.Empty, uritemplate, callback) - { - - } - - public bool HasVerbs - { - get { return UriVerbs.Length > 0; } - } - } } diff --git a/TShockAPI/Rest/RestCommand.cs b/TShockAPI/Rest/RestCommand.cs new file mode 100644 index 00000000..b5bd37f2 --- /dev/null +++ b/TShockAPI/Rest/RestCommand.cs @@ -0,0 +1,47 @@ +using System.Linq; +using System.Text.RegularExpressions; + +namespace Rests +{ + public class RestCommand + { + public string Name { get; protected set; } + public string UriTemplate { get; protected set; } + public string UriVerbMatch { get; protected set; } + public string[] UriVerbs { get; protected set; } + public RestCommandD Callback { get; protected set; } + public bool RequiesToken { get; set; } + + /// + /// + /// + /// Used for identification + /// Url template + /// Rest Command callback + public RestCommand(string name, string uritemplate, RestCommandD callback) + { + Name = name; + UriTemplate = uritemplate; + UriVerbMatch = string.Join("([^/]*)", Regex.Split(uritemplate, "\\{[^\\{\\}]*\\}")); + var matches = Regex.Matches(uritemplate, "\\{([^\\{\\}]*)\\}"); + UriVerbs = (from Match match in matches select match.Groups[1].Value).ToArray(); + Callback = callback; + RequiesToken = true; + } + /// + /// + /// + /// Url template + /// Rest Command callback + public RestCommand(string uritemplate, RestCommandD callback) + : this(string.Empty, uritemplate, callback) + { + + } + + public bool HasVerbs + { + get { return UriVerbs.Length > 0; } + } + } +} \ No newline at end of file diff --git a/TShockAPI/Rest/RestObject.cs b/TShockAPI/Rest/RestObject.cs new file mode 100644 index 00000000..b7db5d2c --- /dev/null +++ b/TShockAPI/Rest/RestObject.cs @@ -0,0 +1,61 @@ +using System.Collections.Generic; + +namespace Rests +{ + public class RestObject : Dictionary + { + public string Status + { + get { return this["status"]; } + set { this["status"] = value; } + } + public string Error + { + get { return this["error"]; } + set { this["error"] = value; } + } + public string Response + { + get { return this["response"]; } + set { this["response"] = value; } + } + + public RestObject(string status) + { + Status = status; + } + + /// + /// Gets value safely, if it does not exist, return null. Sets/Adds value safely, if null it will remove. + /// + /// + /// + /// Returns null if key does not exist. + public new string this[string key] + { + get + { + string ret; + if (TryGetValue(key, out ret)) + return ret; + return null; + } + set + { + if (!ContainsKey(key)) + { + if (value == null) + return; + Add(key, value); + } + else + { + if (value != null) + base[key] = value; + else + Remove(key); + } + } + } + } +} \ No newline at end of file diff --git a/TShockAPI/Rest/RestVerbs.cs b/TShockAPI/Rest/RestVerbs.cs new file mode 100644 index 00000000..b75c59c6 --- /dev/null +++ b/TShockAPI/Rest/RestVerbs.cs @@ -0,0 +1,40 @@ +using System.Collections.Generic; + +namespace Rests +{ + public class RestVerbs : Dictionary + { + /// + /// Gets value safely, if it does not exist, return null. Sets/Adds value safely, if null it will remove. + /// + /// + /// + /// Returns null if key does not exist. + public new string this[string key] + { + get + { + string ret; + if (TryGetValue(key, out ret)) + return ret; + return null; + } + set + { + if (!ContainsKey(key)) + { + if (value == null) + return; + Add(key, value); + } + else + { + if (value != null) + base[key] = value; + else + Remove(key); + } + } + } + } +} \ No newline at end of file diff --git a/TShockAPI/TShockAPI.csproj b/TShockAPI/TShockAPI.csproj index 3d23fced..5a4d895b 100644 --- a/TShockAPI/TShockAPI.csproj +++ b/TShockAPI/TShockAPI.csproj @@ -125,7 +125,10 @@ Resources.resx + + + @@ -183,7 +186,7 @@ - +