From 9a40c81b498f929150772793336c2d481cf8b0a8 Mon Sep 17 00:00:00 2001 From: CoderCow Date: Fri, 2 Aug 2013 10:20:11 +0200 Subject: [PATCH] Fixed all REST endpoints to use RestObjects instead of Dictionaries. --- TShockAPI/Rest/Rest.cs | 6 ++--- TShockAPI/Rest/RestManager.cs | 3 +-- TShockAPI/Rest/SecureRest.cs | 48 +++++++++++------------------------ 3 files changed, 18 insertions(+), 39 deletions(-) diff --git a/TShockAPI/Rest/Rest.cs b/TShockAPI/Rest/Rest.cs index 19b1bf0e..9f1bccf7 100644 --- a/TShockAPI/Rest/Rest.cs +++ b/TShockAPI/Rest/Rest.cs @@ -174,17 +174,15 @@ namespace Rests } catch (Exception exception) { - return new Dictionary + return new RestObject("500") { - {"status", "500"}, {"error", "Internal server error."}, {"errormsg", exception.Message}, {"stacktrace", exception.StackTrace}, }; } - return new Dictionary + return new RestObject("404") { - {"status", "404"}, {"error", "Specified API endpoint doesn't exist. Refer to the documentation for a list of valid endpoints."} }; } diff --git a/TShockAPI/Rest/RestManager.cs b/TShockAPI/Rest/RestManager.cs index 5af02ef1..03154587 100644 --- a/TShockAPI/Rest/RestManager.cs +++ b/TShockAPI/Rest/RestManager.cs @@ -252,9 +252,8 @@ namespace TShockAPI private object ServerTokenTest(RestVerbs verbs, IParameterCollection parameters, SecureRest.TokenData tokenData) { - return new Dictionary + return new RestObject() { - {"status", "200"}, {"response", "Token is valid and was passed through correctly."}, {"associateduser", tokenData.Username} }; diff --git a/TShockAPI/Rest/SecureRest.cs b/TShockAPI/Rest/SecureRest.cs index 5c5f8ea7..e052696e 100644 --- a/TShockAPI/Rest/SecureRest.cs +++ b/TShockAPI/Rest/SecureRest.cs @@ -63,19 +63,19 @@ namespace Rests } catch (Exception) { - return new Dictionary - {{"status", "400"}, {"error", "The specified token queued for destruction failed to be deleted."}}; + return new RestObject("400") + { Error = "The specified token queued for destruction failed to be deleted." }; } - return new Dictionary - {{"status", "200"}, {"response", "Requested token was successfully destroyed."}}; + return new RestObject() + { Response = "Requested token was successfully destroyed." }; } private object DestroyAllTokens(RestVerbs verbs, IParameterCollection parameters, SecureRest.TokenData tokenData) { Tokens.Clear(); - return new Dictionary - {{"status", "200"}, {"response", "All tokens were successfully destroyed."}}; + return new RestObject() + { Response = "All tokens were successfully destroyed." }; } private object NewTokenV2(RestVerbs verbs, IParameterCollection parameters) @@ -123,7 +123,7 @@ namespace Rests Tokens.Add(tokenHash, new TokenData { Username = userAccount.Name, UserGroupName = userGroup.Name }); - RestObject response = new RestObject("200") { Response = "Successful login" }; + RestObject response = new RestObject() { Response = "Successful login" }; response["token"] = tokenHash; return response; } @@ -135,46 +135,28 @@ namespace Rests var token = parms["token"]; if (token == null) - return new Dictionary - {{"status", "401"}, {"error", "Not authorized. The specified API endpoint requires a token."}}; + return new RestObject("401") + { Error = "Not authorized. The specified API endpoint requires a token." }; SecureRestCommand secureCmd = (SecureRestCommand)cmd; TokenData tokenData; if (!Tokens.TryGetValue(token, out tokenData)) - return new Dictionary - { - {"status", "403"}, - { - "error", - "Not authorized. The specified API endpoint requires a token, but the provided token was not valid." - } - }; + return new RestObject("403") + { Error = "Not authorized. The specified API endpoint requires a token, but the provided token was not valid." }; Group userGroup = TShock.Groups.GetGroupByName(tokenData.UserGroupName); if (userGroup == null) { Tokens.Remove(token); - return new Dictionary - { - {"status", "403"}, - { - "error", - "Not authorized. The provided token became invalid due to group changes, please create a new token." - } - }; + return new RestObject("403") + { Error = "Not authorized. The provided token became invalid due to group changes, please create a new token." }; } if (secureCmd.Permissions.Length > 0 && secureCmd.Permissions.All(perm => !userGroup.HasPermission(perm))) { - return new Dictionary - { - {"status", "403"}, - { - "error", - string.Format("Not authorized. User \"{0}\" has no access to use the specified API endpoint.", tokenData.Username) - } - }; + return new RestObject("403") + { Error = string.Format("Not authorized. User \"{0}\" has no access to use the specified API endpoint.", tokenData.Username) }; } object result = secureCmd.Execute(verbs, parms, tokenData);