SafeSet/SafeGet replaced with this[]
This commit is contained in:
parent
1cfca90b8d
commit
07e88dc8a7
4 changed files with 36 additions and 34 deletions
|
|
@ -9,7 +9,7 @@ using HttpServer.Headers;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using HttpListener = HttpServer.HttpListener;
|
using HttpListener = HttpServer.HttpListener;
|
||||||
|
|
||||||
namespace TShockAPI
|
namespace Rests
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Rest command delegate
|
/// Rest command delegate
|
||||||
|
|
@ -102,9 +102,9 @@ namespace TShockAPI
|
||||||
var obj = ExecuteCommand(com, verbs, e.Request.Parameters);
|
var obj = ExecuteCommand(com, verbs, e.Request.Parameters);
|
||||||
if (obj != null)
|
if (obj != null)
|
||||||
return obj;
|
return obj;
|
||||||
|
|
||||||
}
|
}
|
||||||
return new Dictionary<string, string> { { "status", "404" }, {"error", "Specified API endpoint doesn't exist. Refer to the documentation for a list of valid endpoints."} };
|
return new Dictionary<string, string> { { "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)
|
protected virtual object ExecuteCommand(RestCommand cmd, RestVerbs verbs, IParameterCollection parms)
|
||||||
|
|
@ -146,18 +146,18 @@ namespace TShockAPI
|
||||||
{
|
{
|
||||||
public string Status
|
public string Status
|
||||||
{
|
{
|
||||||
get { return SafeGet("status"); }
|
get { return this["status"]; }
|
||||||
set { SafeSet("status", value); }
|
set { this["status"] = value; }
|
||||||
}
|
}
|
||||||
public string Error
|
public string Error
|
||||||
{
|
{
|
||||||
get { return SafeGet("error"); }
|
get { return this["error"]; }
|
||||||
set { SafeSet("error", value); }
|
set { this["error"] = value; }
|
||||||
}
|
}
|
||||||
public string Response
|
public string Response
|
||||||
{
|
{
|
||||||
get { return SafeGet("response"); }
|
get { return this["response"]; }
|
||||||
set { SafeSet("response", value); }
|
set { this["response"] = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public RestObject(string status)
|
public RestObject(string status)
|
||||||
|
|
@ -166,36 +166,35 @@ namespace TShockAPI
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets value safely.
|
/// Gets value safely, if it does not exist, return null. Sets/Adds value safely, if null it will remove.
|
||||||
/// </summary>
|
|
||||||
/// <param name="key"></param>
|
|
||||||
/// <returns>Returns null if key does not exist.</returns>
|
|
||||||
public string SafeGet(string key)
|
|
||||||
{
|
|
||||||
string ret;
|
|
||||||
if (TryGetValue(key, out ret))
|
|
||||||
return ret;
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Sets/Adds value safely. If null it will remove.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="key"></param>
|
/// <param name="key"></param>
|
||||||
/// <param name="value"></param>
|
/// <param name="value"></param>
|
||||||
public void SafeSet(string key, string value)
|
/// <returns>Returns null if key does not exist.</returns>
|
||||||
|
public new string this[string key]
|
||||||
{
|
{
|
||||||
if (!ContainsKey(key))
|
get
|
||||||
{
|
{
|
||||||
if (value == null)
|
string ret;
|
||||||
return;
|
if (TryGetValue(key, out ret))
|
||||||
Add(key, value);
|
return ret;
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
else
|
set
|
||||||
{
|
{
|
||||||
if (value != null)
|
if (!ContainsKey(key))
|
||||||
this[key] = value;
|
{
|
||||||
|
if (value == null)
|
||||||
|
return;
|
||||||
|
Add(key, value);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
Remove(key);
|
{
|
||||||
|
if (value != null)
|
||||||
|
base[key] = value;
|
||||||
|
else
|
||||||
|
Remove(key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using HttpServer;
|
using HttpServer;
|
||||||
|
using Rests;
|
||||||
using Terraria;
|
using Terraria;
|
||||||
|
|
||||||
namespace TShockAPI {
|
namespace TShockAPI {
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,9 @@ using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using HttpServer;
|
using HttpServer;
|
||||||
|
using TShockAPI;
|
||||||
|
|
||||||
namespace TShockAPI
|
namespace Rests
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
|
|
@ -23,7 +24,7 @@ namespace TShockAPI
|
||||||
{
|
{
|
||||||
Tokens = new Dictionary<string, object>();
|
Tokens = new Dictionary<string, object>();
|
||||||
Register(new RestCommand("/token/create/{username}/{password}", NewToken) { RequiesToken = false });
|
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)
|
object DestroyToken(RestVerbs verbs, IParameterCollection parameters)
|
||||||
|
|
@ -66,7 +67,7 @@ namespace TShockAPI
|
||||||
|
|
||||||
Tokens.Add(hash, user);
|
Tokens.Add(hash, user);
|
||||||
|
|
||||||
obj.SafeSet("token", hash);
|
obj["token"] = hash;
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@ using System.Threading;
|
||||||
using Community.CsharpSqlite.SQLiteClient;
|
using Community.CsharpSqlite.SQLiteClient;
|
||||||
using HttpServer;
|
using HttpServer;
|
||||||
using MySql.Data.MySqlClient;
|
using MySql.Data.MySqlClient;
|
||||||
|
using Rests;
|
||||||
using Terraria;
|
using Terraria;
|
||||||
using TerrariaAPI;
|
using TerrariaAPI;
|
||||||
using TerrariaAPI.Hooks;
|
using TerrariaAPI.Hooks;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue