From e9c33633130e0e6cd1f1942e238957f4f2d92f8d Mon Sep 17 00:00:00 2001 From: high Date: Sun, 4 Sep 2011 23:50:35 -0400 Subject: [PATCH] Now instead outputs json serialized objects. --- TShockAPI/Rest.cs | 10 ++++++---- TShockAPI/TShock.cs | 11 ++++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/TShockAPI/Rest.cs b/TShockAPI/Rest.cs index 55d8408b..7fe5a930 100644 --- a/TShockAPI/Rest.cs +++ b/TShockAPI/Rest.cs @@ -5,6 +5,7 @@ using System.Net; using System.Text; using HttpServer; using HttpServer.Headers; +using Newtonsoft.Json; using HttpListener = HttpServer.HttpListener; namespace TShockAPI @@ -14,8 +15,8 @@ namespace TShockAPI /// /// Parameters in the url /// Http request - /// Response body or null to not handle request - public delegate string RestCommandD(IParameterCollection parameters, RequestEventArgs request); + /// Response object or null to not handle request + public delegate object RestCommandD(IParameterCollection parameters, RequestEventArgs request); public class Rest : IDisposable { List commands = new List(); @@ -51,9 +52,10 @@ namespace TShockAPI var coms = commands.Where(r => r.Path.ToLower().Equals(e.Request.Uri.AbsolutePath.ToLower())); foreach (var com in coms) { - var str = com.Callback(e.Request.Parameters, e); - if (str != null) + var obj = com.Callback(e.Request.Parameters, e); + if (obj != null) { + var str = JsonConvert.SerializeObject(this, Formatting.Indented); e.Response.Connection.Type = ConnectionType.Close; e.Response.Body.Write(Encoding.ASCII.GetBytes(str), 0, str.Length); e.Response.Status = HttpStatusCode.OK; diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index a5488dbc..d914a988 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -236,14 +236,19 @@ namespace TShockAPI } //http://127.0.0.1:8080/users?type=status - string usertest(IParameterCollection parameters, RequestEventArgs request) + object usertest(IParameterCollection parameters, RequestEventArgs request) { + var ret = new Dictionary(); var type = parameters["type"]; if (type == null) - return "Invalid Type"; + { + ret.Add("Error", "Invalid Type"); + return ret; + } if (type == "status") { - return "Users online here"; + ret.Add("Users", "Info here"); + return ret; } return null; }