Fixed all REST endpoints to use RestObjects instead of Dictionaries.

This commit is contained in:
CoderCow 2013-08-02 10:20:11 +02:00
parent 11f3099d30
commit 9a40c81b49
3 changed files with 18 additions and 39 deletions

View file

@ -174,17 +174,15 @@ namespace Rests
} }
catch (Exception exception) catch (Exception exception)
{ {
return new Dictionary<string, string> return new RestObject("500")
{ {
{"status", "500"},
{"error", "Internal server error."}, {"error", "Internal server error."},
{"errormsg", exception.Message}, {"errormsg", exception.Message},
{"stacktrace", exception.StackTrace}, {"stacktrace", exception.StackTrace},
}; };
} }
return new Dictionary<string, string> return new RestObject("404")
{ {
{"status", "404"},
{"error", "Specified API endpoint doesn't exist. Refer to the documentation for a list of valid endpoints."} {"error", "Specified API endpoint doesn't exist. Refer to the documentation for a list of valid endpoints."}
}; };
} }

View file

@ -252,9 +252,8 @@ namespace TShockAPI
private object ServerTokenTest(RestVerbs verbs, IParameterCollection parameters, SecureRest.TokenData tokenData) private object ServerTokenTest(RestVerbs verbs, IParameterCollection parameters, SecureRest.TokenData tokenData)
{ {
return new Dictionary<string,object> return new RestObject()
{ {
{"status", "200"},
{"response", "Token is valid and was passed through correctly."}, {"response", "Token is valid and was passed through correctly."},
{"associateduser", tokenData.Username} {"associateduser", tokenData.Username}
}; };

View file

@ -63,19 +63,19 @@ namespace Rests
} }
catch (Exception) catch (Exception)
{ {
return new Dictionary<string, string> return new RestObject("400")
{{"status", "400"}, {"error", "The specified token queued for destruction failed to be deleted."}}; { Error = "The specified token queued for destruction failed to be deleted." };
} }
return new Dictionary<string, string> return new RestObject()
{{"status", "200"}, {"response", "Requested token was successfully destroyed."}}; { Response = "Requested token was successfully destroyed." };
} }
private object DestroyAllTokens(RestVerbs verbs, IParameterCollection parameters, SecureRest.TokenData tokenData) private object DestroyAllTokens(RestVerbs verbs, IParameterCollection parameters, SecureRest.TokenData tokenData)
{ {
Tokens.Clear(); Tokens.Clear();
return new Dictionary<string, string> return new RestObject()
{{"status", "200"}, {"response", "All tokens were successfully destroyed."}}; { Response = "All tokens were successfully destroyed." };
} }
private object NewTokenV2(RestVerbs verbs, IParameterCollection parameters) private object NewTokenV2(RestVerbs verbs, IParameterCollection parameters)
@ -123,7 +123,7 @@ namespace Rests
Tokens.Add(tokenHash, new TokenData { Username = userAccount.Name, UserGroupName = userGroup.Name }); 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; response["token"] = tokenHash;
return response; return response;
} }
@ -135,46 +135,28 @@ namespace Rests
var token = parms["token"]; var token = parms["token"];
if (token == null) if (token == null)
return new Dictionary<string, string> return new RestObject("401")
{{"status", "401"}, {"error", "Not authorized. The specified API endpoint requires a token."}}; { Error = "Not authorized. The specified API endpoint requires a token." };
SecureRestCommand secureCmd = (SecureRestCommand)cmd; SecureRestCommand secureCmd = (SecureRestCommand)cmd;
TokenData tokenData; TokenData tokenData;
if (!Tokens.TryGetValue(token, out tokenData)) if (!Tokens.TryGetValue(token, out tokenData))
return new Dictionary<string, string> return new RestObject("403")
{ { Error = "Not authorized. The specified API endpoint requires a token, but the provided token was not valid." };
{"status", "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); Group userGroup = TShock.Groups.GetGroupByName(tokenData.UserGroupName);
if (userGroup == null) if (userGroup == null)
{ {
Tokens.Remove(token); Tokens.Remove(token);
return new Dictionary<string, string> return new RestObject("403")
{ { Error = "Not authorized. The provided token became invalid due to group changes, please create a new token." };
{"status", "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))) if (secureCmd.Permissions.Length > 0 && secureCmd.Permissions.All(perm => !userGroup.HasPermission(perm)))
{ {
return new Dictionary<string, string> return new RestObject("403")
{ { Error = string.Format("Not authorized. User \"{0}\" has no access to use the specified API endpoint.", tokenData.Username) };
{"status", "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); object result = secureCmd.Execute(verbs, parms, tokenData);