From d22b49cfa83e5c7690c6619d1049e35fe133585a Mon Sep 17 00:00:00 2001 From: Glax Date: Fri, 19 Apr 2024 21:58:29 +0200 Subject: [PATCH] Change Method Header for Handlers to return the response to HandleRequest so we don't forget to send a response. --- Tranga/ServerV2.cs | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/Tranga/ServerV2.cs b/Tranga/ServerV2.cs index 6c115d1..423ad0c 100644 --- a/Tranga/ServerV2.cs +++ b/Tranga/ServerV2.cs @@ -17,23 +17,16 @@ public partial class Server Dictionary requestBody = GetRequestBody(request); //Variables in the JSON body Dictionary requestParams = requestVariables.UnionBy(requestBody, v => v.Key) .ToDictionary(kv => kv.Key, kv => kv.Value); //The actual variable used for the API - - - switch (request.HttpMethod) + + ValueTuple responseMessage = request.HttpMethod switch { - case "GET": - HandleGetV2(path, response, requestParams); - break; - case "POST": - HandlePostV2(path, response, requestParams); - break; - case "DELETE": - HandleDeleteV2(path, response, requestParams); - break; - default: - SendResponse(HttpStatusCode.MethodNotAllowed, response); - break; - } + "GET" => HandleGetV2(path, response, requestParams), + "POST" => HandlePostV2(path, response, requestParams), + "DELETE" => HandleDeleteV2(path, response, requestParams), + _ => new ValueTuple(HttpStatusCode.MethodNotAllowed, null) + }; + + SendResponse(responseMessage.Item1, response, responseMessage.Item2); } private Dictionary GetRequestBody(HttpListenerRequest request) @@ -60,19 +53,19 @@ public partial class Server return new Dictionary(); } - private void HandleGetV2(string path, HttpListenerResponse response, + private ValueTuple HandleGetV2(string path, HttpListenerResponse response, Dictionary requestParameters) { throw new NotImplementedException("v2 not implemented yet"); } - private void HandlePostV2(string path, HttpListenerResponse response, + private ValueTuple HandlePostV2(string path, HttpListenerResponse response, Dictionary requestParameters) { throw new NotImplementedException("v2 not implemented yet"); } - private void HandleDeleteV2(string path, HttpListenerResponse response, + private ValueTuple HandleDeleteV2(string path, HttpListenerResponse response, Dictionary requestParameters) { throw new NotImplementedException("v2 not implemented yet");