Added rest {verb} support

This commit is contained in:
high 2011-09-05 00:48:05 -04:00
parent fea3be69f5
commit d824e71507
2 changed files with 27 additions and 13 deletions

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using HttpServer; using HttpServer;
using HttpServer.Headers; using HttpServer.Headers;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -16,10 +17,10 @@ namespace TShockAPI
/// <param name="parameters">Parameters in the url</param> /// <param name="parameters">Parameters in the url</param>
/// <param name="request">Http request</param> /// <param name="request">Http request</param>
/// <returns>Response object or null to not handle request</returns> /// <returns>Response object or null to not handle request</returns>
public delegate object RestCommandD(IParameterCollection parameters, RequestEventArgs request); public delegate object RestCommandD(Dictionary<string,string> verbs, IParameterCollection parameters, RequestEventArgs request);
public class Rest : IDisposable public class Rest : IDisposable
{ {
List<RestCommand> commands = new List<RestCommand>(); readonly List<RestCommand> commands = new List<RestCommand>();
HttpListener listener; HttpListener listener;
public Rest(IPAddress ip, int port) public Rest(IPAddress ip, int port)
@ -61,12 +62,19 @@ namespace TShockAPI
protected virtual object Process(object sender, RequestEventArgs e) protected virtual object Process(object sender, RequestEventArgs e)
{ {
var coms = commands.Where(r => r.Path.ToLower().Equals(e.Request.Uri.AbsolutePath.ToLower())); foreach (var com in commands)
foreach (var com in coms)
{ {
var obj = com.Callback(e.Request.Parameters, e); var matches = Regex.Matches(e.Request.Uri.AbsolutePath, com.UriMatch);
if (obj != null) if (matches.Count == com.UriNames.Length)
return obj; {
var verbs = new Dictionary<string, string>();
for (int i = 0; i < matches.Count; i++)
verbs.Add(com.UriNames[i], matches[i].Groups[1].Value);
var obj = com.Callback(verbs, e.Request.Parameters, e);
if (obj != null)
return obj;
}
} }
return new Dictionary<string, string> { { "Error", "Invalid request" } }; return new Dictionary<string, string> { { "Error", "Invalid request" } };
} }
@ -98,12 +106,17 @@ namespace TShockAPI
public class RestCommand public class RestCommand
{ {
public string Path { get; protected set; } public string UriTemplate { get; protected set; }
public string UriMatch { get; protected set; }
public string[] UriNames { get; protected set; }
public RestCommandD Callback { get; protected set; } public RestCommandD Callback { get; protected set; }
public RestCommand(string path, RestCommandD callback) public RestCommand(string uritemplate, RestCommandD callback)
{ {
Path = path; UriTemplate = uritemplate;
UriMatch = string.Join("([^/]*)", Regex.Split(uritemplate, "\\{[^\\{\\}]*\\}"));
var matches = Regex.Matches(uritemplate, "\\{([^\\{\\}]*)\\}");
UriNames = (from Match match in matches select match.Groups[1].Value).ToArray();
Callback = callback; Callback = callback;
} }
} }

View file

@ -34,6 +34,7 @@ using System.IO;
using System.Net; using System.Net;
using System.Reflection; using System.Reflection;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using Community.CsharpSqlite.SQLiteClient; using Community.CsharpSqlite.SQLiteClient;
using HttpServer; using HttpServer;
@ -204,7 +205,7 @@ namespace TShockAPI
if (Initialized != null) if (Initialized != null)
Initialized(); Initialized();
RestApi.Register(new RestCommand("/users", usertest)); RestApi.Register(new RestCommand("/HelloWorld/name/{username}", usertest));
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -235,8 +236,8 @@ namespace TShockAPI
//RconHandler.ShutdownAllThreads(); //RconHandler.ShutdownAllThreads();
} }
//http://127.0.0.1:8080/users?type=status //http://127.0.0.1:8080/HelloWorld/name/{username}?type=status
object usertest(IParameterCollection parameters, RequestEventArgs request) object usertest(Dictionary<string,string> verbs, IParameterCollection parameters, RequestEventArgs request)
{ {
var ret = new Dictionary<string, string>(); var ret = new Dictionary<string, string>();
var type = parameters["type"]; var type = parameters["type"];