refactor: added 400 bad request responses

This commit is contained in:
jdevito
2023-02-01 12:22:52 -06:00
parent 1c1eabcef6
commit a3346d5ef4
7 changed files with 68 additions and 102 deletions

View File

@@ -54,5 +54,22 @@ namespace PepperDash.Essentials.Core.Web
JoinCapabilities = joinData.Value.Metadata.JoinCapabilities.ToString()
};
}
public static object MapDeviceTypeToObject(string key, DeviceFactoryWrapper device)
{
var kp = new KeyValuePair<string, DeviceFactoryWrapper>(key, device);
return MapDeviceTypeToObject(kp);
}
public static object MapDeviceTypeToObject(KeyValuePair<string, DeviceFactoryWrapper> device)
{
return new
{
Type = device.Key,
Description = device.Value.Description,
CType = device.Value.CType == null ? "---": device.Value.CType.ToString()
};
}
}
}

View File

@@ -86,7 +86,14 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param>
protected override void HandlePost(HttpCwsContext context)
{
if (context.Request.ContentLength < 0) return;
if (context.Request.ContentLength < 0)
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.End();
return;
}
var bytes = new Byte[context.Request.ContentLength];
context.Request.InputStream.Read(bytes, 0, context.Request.ContentLength);

View File

@@ -80,7 +80,14 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param>
protected override void HandlePost(HttpCwsContext context)
{
if (context.Request.ContentLength < 0) return;
if (context.Request.ContentLength < 0)
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.End();
return;
}
var bytes = new Byte[context.Request.ContentLength];
context.Request.InputStream.Read(bytes, 0, context.Request.ContentLength);

View File

@@ -1,19 +1,12 @@
using System;
using System.Linq;
using System.Linq;
using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json;
using PepperDash.Core;
using PepperDash.Core.Web.RequestHandlers;
namespace PepperDash.Essentials.Core.Web.RequestHandlers
{
public class GetTypesByFilterRequestHandler : WebApiBaseRequestHandler
{
private const string Key = "GetTypesByFilterRequestHandler";
private const uint Trace = 0;
private const uint Info = 1;
private const uint Verbose = 2;
/// <summary>
/// Handles CONNECT method requests
/// </summary>
@@ -52,14 +45,9 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
return;
}
var routeDataJson = JsonConvert.SerializeObject(routeData, Formatting.Indented);
Debug.Console(Verbose, "[{0}] routeData:\n{1}", Key.ToLower(), routeDataJson);
object filterObj;
if (!routeData.Values.TryGetValue("filter", out filterObj))
{
Debug.Console(Verbose, "TryGetValue filter failed");
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.End();
@@ -67,44 +55,25 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
return;
}
var types = DeviceFactory.GetDeviceFactoryDictionary(filterObj.ToString()).Select(type => new
var deviceFactory = DeviceFactory.GetDeviceFactoryDictionary(filterObj.ToString());
if (deviceFactory == null)
{
Type = type.Key,
Description = type.Value.Description,
CType = type.Value.CType == null ? "---" : type.Value.CType.ToString()
}).Cast<object>().ToList();
if (types == null)
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.StatusCode = 404;
context.Response.StatusDescription = "Not Found";
context.Response.End();
return;
}
try
{
var js = JsonConvert.SerializeObject(types, Formatting.Indented);
//Debug.Console(Verbose, "[{0}] HandleGet: \x0d\x0a{1}", Key.ToLower(), js);
var deviceTypes = deviceFactory.Select(t => EssentialsWebApiHelpers.MapDeviceTypeToObject(t)).ToList();
var js = JsonConvert.SerializeObject(deviceTypes, Formatting.Indented);
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.ContentType = "application/json";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.Write(js, false);
context.Response.End();
}
catch (Exception ex)
{
Debug.Console(Info, "[{0}] HandleGet Exception Message: {1}", Key.ToLower(), ex.Message);
Debug.Console(Verbose, "[{0}] HandleGet Exception StackTrace: {1}", Key.ToLower(), ex.StackTrace);
if (ex.InnerException != null) Debug.Console(Verbose, "[{0}] HandleGet Exception InnerException: {1}", Key.ToLower(), ex.InnerException);
context.Response.StatusCode = 500;
context.Response.StatusDescription = "Internal Server Error";
context.Response.End();
}
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.ContentType = "application/json";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.Write(js, false);
context.Response.End();
}
/// <summary>

View File

@@ -1,19 +1,12 @@
using System;
using System.Linq;
using System.Linq;
using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json;
using PepperDash.Core;
using PepperDash.Core.Web.RequestHandlers;
namespace PepperDash.Essentials.Core.Web.RequestHandlers
{
public class GetTypesRequestHandler : WebApiBaseRequestHandler
{
private const string Key = "GetTypesRequestHandler";
private const uint Trace = 0;
private const uint Info = 1;
private const uint Verbose = 2;
/// <summary>
/// Handles CONNECT method requests
/// </summary>
@@ -52,47 +45,25 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
return;
}
var routeDataJson = JsonConvert.SerializeObject(routeData, Formatting.Indented);
Debug.Console(Verbose, "[{0}] routeData:\n{1}", Key.ToLower(), routeDataJson);
var types = DeviceFactory.GetDeviceFactoryDictionary(string.Empty).Select(type => new
var deviceFactory = DeviceFactory.GetDeviceFactoryDictionary(null);
if (deviceFactory == null)
{
Type = type.Key,
Description = type.Value.Description,
CType = type.Value.CType == null ? "---" : type.Value.CType.ToString()
}).Cast<object>().ToList();
if (types == null)
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.StatusCode = 404;
context.Response.StatusDescription = "Not Found";
context.Response.End();
return;
}
try
{
var js = JsonConvert.SerializeObject(types, Formatting.Indented);
//Debug.Console(Verbose, "[{0}] HandleGet: \x0d\x0a{1}", Key.ToLower(), js);
var deviceTypes = deviceFactory.Select(t => EssentialsWebApiHelpers.MapDeviceTypeToObject(t)).ToList();
var js = JsonConvert.SerializeObject(deviceTypes, Formatting.Indented);
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.ContentType = "application/json";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.Write(js, false);
context.Response.End();
}
catch (Exception ex)
{
Debug.Console(Info, "[{0}] HandleGet Exception Message: {1}", Key.ToLower(), ex.Message);
Debug.Console(Verbose, "[{0}] HandleGet Exception StackTrace: {1}", Key.ToLower(), ex.StackTrace);
if (ex.InnerException != null) Debug.Console(Verbose, "[{0}] HandleGet Exception InnerException: {1}", Key.ToLower(), ex.InnerException);
context.Response.StatusCode = 500;
context.Response.StatusDescription = "Internal Server Error";
context.Response.End();
}
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.ContentType = "application/json";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.Write(js, false);
context.Response.End();
}
/// <summary>

View File

@@ -2,18 +2,12 @@
using System.Text;
using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json;
using PepperDash.Core;
using PepperDash.Core.Web.RequestHandlers;
namespace PepperDash.Essentials.Core.Web.RequestHandlers
{
public class SetDeviceStreamDebugRequestHandler : WebApiBaseRequestHandler
{
private const string Key = "SetDeviceStreamDebugRequestHandler";
private const uint Trace = 0;
private const uint Info = 1;
private const uint Verbose = 2;
/// <summary>
/// Handles CONNECT method requests
/// </summary>
@@ -86,13 +80,19 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param>
protected override void HandlePost(HttpCwsContext context)
{
if (context.Request.ContentLength < 0) return;
if (context.Request.ContentLength < 0)
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.End();
return;
}
var bytes = new Byte[context.Request.ContentLength];
context.Request.InputStream.Read(bytes, 0, context.Request.ContentLength);
var data = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
Debug.Console(Info, "[{0}] Request data:\n{1}", Key.ToLower(), data);
var o = new
{
DeviceKey = "",

View File

@@ -7,11 +7,6 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
{
public class ShowConfigRequestHandler : WebApiBaseRequestHandler
{
private const string Key = "ShowConfigRequestHandler";
private const uint Trace = 0;
private const uint Info = 1;
private const uint Verbose = 2;
/// <summary>
/// Handles CONNECT method requests
/// </summary>