Add UI.PublishJson to enable APIs

Fixes #7
This commit is contained in:
Frank A. Krueger 2017-07-07 20:27:03 -07:00
parent cd8c96a27c
commit 16315c85d3
2 changed files with 79 additions and 8 deletions

View File

@ -102,13 +102,24 @@ namespace Ooui
PublishFile (path, filePath); PublishFile (path, filePath);
} }
public static void PublishFile (string path, string filePath, string contentType = null, string contentEncoding = null) public static void PublishFile (string path, string filePath, string contentType = null)
{ {
var data = System.IO.File.ReadAllBytes (filePath); var data = System.IO.File.ReadAllBytes (filePath);
if (contentType == null) { if (contentType == null) {
contentType = GuessContentType (path, filePath); contentType = GuessContentType (path, filePath);
} }
Publish (path, new DataHandler (data, contentType, contentEncoding)); Publish (path, new DataHandler (data, contentType));
}
public static void PublishJson (string path, Func<object> ctor)
{
Publish (path, new JsonHandler (ctor));
}
public static void PublishJson (string path, object value)
{
var data = JsonHandler.GetData (value);
Publish (path, new DataHandler (data, JsonHandler.ContentType));
} }
static string GuessContentType (string path, string filePath) static string GuessContentType (string path, string filePath)
@ -284,13 +295,11 @@ namespace Ooui
{ {
readonly byte[] data; readonly byte[] data;
readonly string contentType; readonly string contentType;
readonly string contentEncoding;
public DataHandler (byte[] data, string contentType = null, string contentEncoding = null) public DataHandler (byte[] data, string contentType = null)
{ {
this.data = data; this.data = data;
this.contentType = contentType; this.contentType = contentType;
this.contentEncoding = contentEncoding;
} }
public override void Respond (HttpListenerContext listenerContext, CancellationToken token) public override void Respond (HttpListenerContext listenerContext, CancellationToken token)
@ -302,8 +311,40 @@ namespace Ooui
response.StatusCode = 200; response.StatusCode = 200;
if (!string.IsNullOrEmpty (contentType)) if (!string.IsNullOrEmpty (contentType))
response.ContentType = contentType; response.ContentType = contentType;
if (!string.IsNullOrEmpty (contentEncoding)) response.ContentLength64 = data.LongLength;
response.ContentType = contentEncoding;
using (var s = response.OutputStream) {
s.Write (data, 0, data.Length);
}
response.Close ();
}
}
class JsonHandler : RequestHandler
{
public const string ContentType = "application/json; charset=utf-8";
readonly Func<object> ctor;
public JsonHandler (Func<object> ctor)
{
this.ctor = ctor;
}
public static byte[] GetData (object obj)
{
var r = Newtonsoft.Json.JsonConvert.SerializeObject (obj);
return System.Text.Encoding.UTF8.GetBytes (r);
}
public override void Respond (HttpListenerContext listenerContext, CancellationToken token)
{
var response = listenerContext.Response;
var data = GetData (ctor ());
response.StatusCode = 200;
response.ContentType = ContentType;
response.ContentLength64 = data.LongLength; response.ContentLength64 = data.LongLength;
using (var s = response.OutputStream) { using (var s = response.OutputStream) {

View File

@ -90,7 +90,7 @@ namespace Tests
{ {
var f = System.IO.Path.GetTempFileName (); var f = System.IO.Path.GetTempFileName ();
System.IO.File.WriteAllText (f, "Test Ooui Text File", System.Text.Encoding.UTF8); System.IO.File.WriteAllText (f, "Test Ooui Text File", System.Text.Encoding.UTF8);
UI.PublishFile ("/text-file", f, "text/plain", "utf-8"); UI.PublishFile ("/text-file", f, "text/plain; charset=utf-8");
UI.WaitUntilStarted (); UI.WaitUntilStarted ();
var c = new System.Net.WebClient (); var c = new System.Net.WebClient ();
var r = c.DownloadString (UI.GetUrl ("/text-file")); var r = c.DownloadString (UI.GetUrl ("/text-file"));
@ -108,5 +108,35 @@ namespace Tests
var r = c.DownloadString (UI.GetUrl ("/" + System.IO.Path.GetFileName (f))); var r = c.DownloadString (UI.GetUrl ("/" + System.IO.Path.GetFileName (f)));
Assert.AreEqual ("Test Ooui Text File 2", r); Assert.AreEqual ("Test Ooui Text File 2", r);
} }
[TestMethod]
public void PublishJsonObject ()
{
UI.PublishJson ("/json", new JsonTestObject ());
UI.WaitUntilStarted ();
var c = new System.Net.WebClient ();
var r = c.DownloadString (UI.GetUrl ("/json"));
Assert.AreEqual ("{\"Name\":\"X\",\"Value\":null}", r);
}
[TestMethod]
public void PublishJsonCtor ()
{
var i = 1;
UI.PublishJson ("/jsond", () => new JsonTestObject { Value = i++ });
UI.WaitUntilStarted ();
var c = new System.Net.WebClient ();
var r1 = c.DownloadString (UI.GetUrl ("/jsond"));
var r2 = c.DownloadString (UI.GetUrl ("/jsond"));
Assert.AreEqual ("{\"Name\":\"X\",\"Value\":1}", r1);
Assert.AreEqual ("{\"Name\":\"X\",\"Value\":2}", r2);
}
class JsonTestObject
{
public string Name = "X";
public object Value;
}
} }
} }