From 023654aba25e307f60fc12ad4451334a660d3a2b Mon Sep 17 00:00:00 2001 From: MattMo Date: Thu, 2 Feb 2023 13:33:13 -0800 Subject: [PATCH] Cleaned up code and added new ResponseExtensions to help with bad requests. --- .../HttpListenerRequestExtensions.cs | 0 .../HttpListenerResponseExtensions.cs | 41 +++++++++++++++++++ Rest.Net/Rest.Net.csproj | 2 +- Rest.Net/{Extensions => }/StringExtensions.cs | 0 4 files changed, 42 insertions(+), 1 deletion(-) rename Rest.Net/{Extensions => }/HttpListenerRequestExtensions.cs (100%) rename Rest.Net/{Extensions => }/HttpListenerResponseExtensions.cs (81%) rename Rest.Net/{Extensions => }/StringExtensions.cs (100%) diff --git a/Rest.Net/Extensions/HttpListenerRequestExtensions.cs b/Rest.Net/HttpListenerRequestExtensions.cs similarity index 100% rename from Rest.Net/Extensions/HttpListenerRequestExtensions.cs rename to Rest.Net/HttpListenerRequestExtensions.cs diff --git a/Rest.Net/Extensions/HttpListenerResponseExtensions.cs b/Rest.Net/HttpListenerResponseExtensions.cs similarity index 81% rename from Rest.Net/Extensions/HttpListenerResponseExtensions.cs rename to Rest.Net/HttpListenerResponseExtensions.cs index da50c7c..1e15cbe 100644 --- a/Rest.Net/Extensions/HttpListenerResponseExtensions.cs +++ b/Rest.Net/HttpListenerResponseExtensions.cs @@ -225,5 +225,46 @@ namespace MontoyaTech.Rest.Net return response; } } + + /// + /// Sets the response to bad request with a text message saying the request was null or empty. + /// + /// + /// + public static HttpListenerResponse BadRequestNull(this HttpListenerResponse response) + { + return response.WithStatus(HttpStatusCode.BadRequest).WithText("Request was null or empty."); + } + + /// + /// Sets the response to a bad request with a text message saying the request was invalid. + /// + /// + /// + public static HttpListenerResponse BadRequestInvalid(this HttpListenerResponse response) + { + return response.WithStatus(HttpStatusCode.BadRequest).WithText("Request data was invalid."); + } + + /// + /// Sets the response to a bad request with a text message saying the request was out of range. + /// + /// + /// + public static HttpListenerResponse BadRequestOutOfRange(this HttpListenerResponse response) + { + return response.WithStatus(HttpStatusCode.BadRequest).WithText("Request data was out of range"); + } + + /// + /// Sets the response to a bad request with a text message saying a field in the request was out of range. + /// + /// + /// + /// + public static HttpListenerResponse BadRequestOutOfRange(this HttpListenerResponse response, string fieldName) + { + return response.WithStatus(HttpStatusCode.BadRequest).WithText($"{fieldName} in request is out of range."); + } } } diff --git a/Rest.Net/Rest.Net.csproj b/Rest.Net/Rest.Net.csproj index bea601d..aefd13a 100644 --- a/Rest.Net/Rest.Net.csproj +++ b/Rest.Net/Rest.Net.csproj @@ -17,7 +17,7 @@ MontoyaTech.Rest.Net MontoyaTech.Rest.Net True - 1.1.6 + 1.1.7 Logo_Symbol_Black_Outline.png diff --git a/Rest.Net/Extensions/StringExtensions.cs b/Rest.Net/StringExtensions.cs similarity index 100% rename from Rest.Net/Extensions/StringExtensions.cs rename to Rest.Net/StringExtensions.cs