From 07e88dc8a77a71be69d7beef749e4889d101c2bf Mon Sep 17 00:00:00 2001 From: high Date: Mon, 5 Sep 2011 14:12:49 -0400 Subject: [PATCH] SafeSet/SafeGet replaced with this[] --- TShockAPI/Rest.cs | 61 ++++++++++++++++++++-------------------- TShockAPI/RestManager.cs | 1 + TShockAPI/SecureRest.cs | 7 +++-- TShockAPI/TShock.cs | 1 + 4 files changed, 36 insertions(+), 34 deletions(-) diff --git a/TShockAPI/Rest.cs b/TShockAPI/Rest.cs index 2c14d087..1a91cf88 100644 --- a/TShockAPI/Rest.cs +++ b/TShockAPI/Rest.cs @@ -9,7 +9,7 @@ using HttpServer.Headers; using Newtonsoft.Json; using HttpListener = HttpServer.HttpListener; -namespace TShockAPI +namespace Rests { /// /// Rest command delegate @@ -102,9 +102,9 @@ namespace TShockAPI var obj = ExecuteCommand(com, verbs, e.Request.Parameters); if (obj != null) return obj; - + } - return new Dictionary { { "status", "404" }, {"error", "Specified API endpoint doesn't exist. Refer to the documentation for a list of valid endpoints."} }; + return new Dictionary { { "status", "404" }, { "error", "Specified API endpoint doesn't exist. Refer to the documentation for a list of valid endpoints." } }; } protected virtual object ExecuteCommand(RestCommand cmd, RestVerbs verbs, IParameterCollection parms) @@ -146,18 +146,18 @@ namespace TShockAPI { public string Status { - get { return SafeGet("status"); } - set { SafeSet("status", value); } + get { return this["status"]; } + set { this["status"] = value; } } public string Error { - get { return SafeGet("error"); } - set { SafeSet("error", value); } + get { return this["error"]; } + set { this["error"] = value; } } public string Response { - get { return SafeGet("response"); } - set { SafeSet("response", value); } + get { return this["response"]; } + set { this["response"] = value; } } public RestObject(string status) @@ -166,36 +166,35 @@ namespace TShockAPI } /// - /// Gets value safely. - /// - /// - /// Returns null if key does not exist. - public string SafeGet(string key) - { - string ret; - if (TryGetValue(key, out ret)) - return ret; - return null; - } - /// - /// Sets/Adds value safely. If null it will remove. + /// Gets value safely, if it does not exist, return null. Sets/Adds value safely, if null it will remove. /// /// /// - public void SafeSet(string key, string value) + /// Returns null if key does not exist. + public new string this[string key] { - if (!ContainsKey(key)) + get { - if (value == null) - return; - Add(key, value); + string ret; + if (TryGetValue(key, out ret)) + return ret; + return null; } - else + set { - if (value != null) - this[key] = value; + if (!ContainsKey(key)) + { + if (value == null) + return; + Add(key, value); + } else - Remove(key); + { + if (value != null) + base[key] = value; + else + Remove(key); + } } } } diff --git a/TShockAPI/RestManager.cs b/TShockAPI/RestManager.cs index b68a1ee7..dfd0040b 100644 --- a/TShockAPI/RestManager.cs +++ b/TShockAPI/RestManager.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using HttpServer; +using Rests; using Terraria; namespace TShockAPI { diff --git a/TShockAPI/SecureRest.cs b/TShockAPI/SecureRest.cs index 46b8ddba..ec7440d2 100644 --- a/TShockAPI/SecureRest.cs +++ b/TShockAPI/SecureRest.cs @@ -4,8 +4,9 @@ using System.Linq; using System.Net; using System.Text; using HttpServer; +using TShockAPI; -namespace TShockAPI +namespace Rests { /// /// @@ -23,7 +24,7 @@ namespace TShockAPI { Tokens = new Dictionary(); Register(new RestCommand("/token/create/{username}/{password}", NewToken) { RequiesToken = false }); - Register(new RestCommand("/token/destroy/{token}", DestroyToken) {RequiesToken = true}); + Register(new RestCommand("/token/destroy/{token}", DestroyToken) { RequiesToken = true }); } object DestroyToken(RestVerbs verbs, IParameterCollection parameters) @@ -66,7 +67,7 @@ namespace TShockAPI Tokens.Add(hash, user); - obj.SafeSet("token", hash); + obj["token"] = hash; return obj; } diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 63dd9931..d4ff9dab 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -39,6 +39,7 @@ using System.Threading; using Community.CsharpSqlite.SQLiteClient; using HttpServer; using MySql.Data.MySqlClient; +using Rests; using Terraria; using TerrariaAPI; using TerrariaAPI.Hooks;