mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-01-11 19:44:52 +00:00
82 lines
2.0 KiB
C#
82 lines
2.0 KiB
C#
using Crestron.SimplSharp.WebScripting;
|
|
using Newtonsoft.Json;
|
|
using PepperDash.Core;
|
|
using PepperDash.Core.Web.RequestHandlers;
|
|
|
|
namespace PepperDash.Essentials.Core.Web.RequestHandlers
|
|
{
|
|
public class AppDebugRequestHandler : WebApiBaseRequestHandler
|
|
{
|
|
/// <summary>
|
|
/// Constructor
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// base(true) enables CORS support by default
|
|
/// </remarks>
|
|
public AppDebugRequestHandler()
|
|
: base(true)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handles GET method requests
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
protected override void HandleGet(HttpCwsContext context)
|
|
{
|
|
var appDebug = new AppDebug { Level = Debug.Level };
|
|
|
|
var body = JsonConvert.SerializeObject(appDebug, Formatting.Indented);
|
|
|
|
context.Response.StatusCode = 200;
|
|
context.Response.StatusDescription = "OK";
|
|
context.Response.Write(body, false);
|
|
context.Response.End();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handles POST method requests
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
protected override void HandlePost(HttpCwsContext context)
|
|
{
|
|
if (context.Request.ContentLength < 0)
|
|
{
|
|
context.Response.StatusCode = 400;
|
|
context.Response.StatusDescription = "Bad Request";
|
|
context.Response.End();
|
|
|
|
return;
|
|
}
|
|
|
|
var data = EssentialsWebApiHelpers.GetRequestBody(context.Request);
|
|
if (string.IsNullOrEmpty(data))
|
|
{
|
|
context.Response.StatusCode = 400;
|
|
context.Response.StatusDescription = "Bad Request";
|
|
context.Response.End();
|
|
|
|
return;
|
|
}
|
|
|
|
var appDebug = new AppDebug();
|
|
var requestBody = JsonConvert.DeserializeAnonymousType(data, appDebug);
|
|
|
|
Debug.SetDebugLevel(requestBody.Level);
|
|
|
|
appDebug.Level = Debug.Level;
|
|
var responseBody = JsonConvert.SerializeObject(appDebug, Formatting.Indented);
|
|
|
|
context.Response.StatusCode = 200;
|
|
context.Response.StatusDescription = "OK";
|
|
context.Response.Write(responseBody, false);
|
|
context.Response.End();
|
|
}
|
|
}
|
|
|
|
public class AppDebug
|
|
{
|
|
[JsonProperty("level", NullValueHandling = NullValueHandling.Ignore)]
|
|
public int Level { get; set; }
|
|
}
|
|
} |