Use JSON for POST responses
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace ChatTwo.Http.MessageProtocol;
|
||||
|
||||
#region Outgoing
|
||||
#region Outgoing SSE
|
||||
public struct SwitchChannel(string name)
|
||||
{
|
||||
[JsonProperty("channel")] public string Name = name;
|
||||
@@ -25,7 +25,19 @@ public struct MessageResponse()
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Incoming
|
||||
#region Outgoing POST
|
||||
public struct OkResponse(string message)
|
||||
{
|
||||
[JsonProperty("message")] public string Message = message;
|
||||
}
|
||||
|
||||
public struct ErrorResponse(string reason)
|
||||
{
|
||||
[JsonProperty("reason")] public string Reason = reason;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Incoming POST
|
||||
public struct IncomingMessage()
|
||||
{
|
||||
[JsonProperty("message")] public string Message = string.Empty;
|
||||
|
||||
@@ -158,14 +158,16 @@ public class RouteController
|
||||
{
|
||||
if (ctx.Request.ContentType != "application/json")
|
||||
{
|
||||
await ctx.Response.Send("Request contains wrong content type.");
|
||||
ctx.Response.StatusCode = 415;
|
||||
await ctx.Response.Send(JsonConvert.SerializeObject(new ErrorResponse("Request contains wrong media type.")));
|
||||
return;
|
||||
}
|
||||
|
||||
var content = JsonConvert.DeserializeObject<IncomingMessage>(ctx.Request.DataAsString, JsonSettings);
|
||||
if (content.Message.Length is < 2 or > 500)
|
||||
{
|
||||
await ctx.Response.Send("Invalid message received.");
|
||||
ctx.Response.StatusCode = 400;
|
||||
await ctx.Response.Send(JsonConvert.SerializeObject(new ErrorResponse("Invalid message received.")));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -175,21 +177,24 @@ public class RouteController
|
||||
Plugin.ChatLogWindow.SendChatBox(Plugin.ChatLogWindow.CurrentTab);
|
||||
});
|
||||
|
||||
await ctx.Response.Send("Message was send to the channel.");
|
||||
ctx.Response.StatusCode = 201;
|
||||
await ctx.Response.Send(JsonConvert.SerializeObject(new OkResponse("Message was send to the channel.")));
|
||||
}
|
||||
|
||||
private async Task ReceiveChannelSwitch(HttpContextBase ctx)
|
||||
{
|
||||
if (ctx.Request.ContentType != "application/json")
|
||||
{
|
||||
await ctx.Response.Send("Request contains wrong content type.");
|
||||
ctx.Response.StatusCode = 415;
|
||||
await ctx.Response.Send(JsonConvert.SerializeObject(new ErrorResponse("Request contains wrong media type.")));
|
||||
return;
|
||||
}
|
||||
|
||||
var channel = JsonConvert.DeserializeObject<IncomingChannel>(ctx.Request.DataAsString, JsonSettings);
|
||||
if (!Enum.IsDefined(typeof(InputChannel), channel.Channel))
|
||||
{
|
||||
await ctx.Response.Send("Invalid channel received.");
|
||||
ctx.Response.StatusCode = 400;
|
||||
await ctx.Response.Send(JsonConvert.SerializeObject(new ErrorResponse("Invalid channel received.")));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -198,7 +203,8 @@ public class RouteController
|
||||
Plugin.ChatLogWindow.SetChannel((InputChannel)channel.Channel);
|
||||
});
|
||||
|
||||
await ctx.Response.Send("Function to switch channels has been called.");
|
||||
ctx.Response.StatusCode = 201;
|
||||
await ctx.Response.Send(JsonConvert.SerializeObject(new OkResponse("Channel switch got initiated.")));
|
||||
}
|
||||
|
||||
private async Task NewSSEConnection(HttpContextBase ctx)
|
||||
|
||||
Reference in New Issue
Block a user