From 9b849f5ec171367746ad4473fc2db4766f2ae1a7 Mon Sep 17 00:00:00 2001 From: MattMo Date: Thu, 26 Jan 2023 11:48:56 -0800 Subject: [PATCH] Simplified some class names and structure. New nuget version 1.1.4 --- Rest.Net.Example/Program.cs | 12 ++-- .../RequestPostProcessEventHandler.cs | 4 +- .../RequestPreProcessEventHandler.cs | 4 +- Rest.Net/Rest.Net.csproj | 2 +- Rest.Net/Route.cs | 72 +++++++++---------- .../RouteExceptionEventHandler.cs | 4 +- Rest.Net/RouteListener.cs | 3 +- ...enerContext.cs => RouteListenerContext.cs} | 4 +- 8 files changed, 52 insertions(+), 53 deletions(-) rename Rest.Net/{Events => }/RequestPostProcessEventHandler.cs (67%) rename Rest.Net/{Events => }/RequestPreProcessEventHandler.cs (74%) rename Rest.Net/{Events => }/RouteExceptionEventHandler.cs (83%) rename Rest.Net/{ListenerContext.cs => RouteListenerContext.cs} (87%) diff --git a/Rest.Net.Example/Program.cs b/Rest.Net.Example/Program.cs index c478bef..441f67b 100644 --- a/Rest.Net.Example/Program.cs +++ b/Rest.Net.Example/Program.cs @@ -34,12 +34,12 @@ namespace MontoyaTech.Rest.Net.Example new Route(HttpRequestMethod.Get, "/json", Json) ); - listener.RequestPreProcessEvent += (ListenerContext context) => { + listener.RequestPreProcessEvent += (RouteListenerContext context) => { Console.WriteLine("Request start: " + context.Request.RawUrl); return true; }; - listener.RequestPostProcessEvent += (ListenerContext context) => + listener.RequestPostProcessEvent += (RouteListenerContext context) => { Console.WriteLine("Request end: " + context.Request.RawUrl); }; @@ -56,22 +56,22 @@ namespace MontoyaTech.Rest.Net.Example listener.Block(); } - public static HttpListenerResponse Status(ListenerContext context) + public static HttpListenerResponse Status(RouteListenerContext context) { return context.Response.WithStatus(HttpStatusCode.OK).WithText("Everything is operational. 👍"); } - public static HttpListenerResponse Add(ListenerContext context, double a, double b) + public static HttpListenerResponse Add(RouteListenerContext context, double a, double b) { return context.Response.WithStatus(HttpStatusCode.OK).WithText((a + b).ToString()); } - public static HttpListenerResponse Signup(ListenerContext context, User user) + public static HttpListenerResponse Signup(RouteListenerContext context, User user) { return context.Response.WithStatus(HttpStatusCode.OK).WithText("User:" + user.Name); } - public static HttpListenerResponse Json(ListenerContext context) + public static HttpListenerResponse Json(RouteListenerContext context) { return context.Response.WithStatus(HttpStatusCode.OK).WithJson(new User("Rest.Net")); } diff --git a/Rest.Net/Events/RequestPostProcessEventHandler.cs b/Rest.Net/RequestPostProcessEventHandler.cs similarity index 67% rename from Rest.Net/Events/RequestPostProcessEventHandler.cs rename to Rest.Net/RequestPostProcessEventHandler.cs index 69d1814..7d59212 100644 --- a/Rest.Net/Events/RequestPostProcessEventHandler.cs +++ b/Rest.Net/RequestPostProcessEventHandler.cs @@ -4,11 +4,11 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace MontoyaTech.Rest.Net.Events +namespace MontoyaTech.Rest.Net { /// /// A delegate to post process requests. /// /// - public delegate void RequestPostProcessEventHandler(ListenerContext context); + public delegate void RequestPostProcessEventHandler(RouteListenerContext context); } diff --git a/Rest.Net/Events/RequestPreProcessEventHandler.cs b/Rest.Net/RequestPreProcessEventHandler.cs similarity index 74% rename from Rest.Net/Events/RequestPreProcessEventHandler.cs rename to Rest.Net/RequestPreProcessEventHandler.cs index 0c24b76..600df49 100644 --- a/Rest.Net/Events/RequestPreProcessEventHandler.cs +++ b/Rest.Net/RequestPreProcessEventHandler.cs @@ -4,12 +4,12 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace MontoyaTech.Rest.Net.Events +namespace MontoyaTech.Rest.Net { /// /// A delegate that can be used to pre process requests and stop them from being handled by routes if needed. /// /// /// - public delegate bool RequestPreProcessEventHandler(ListenerContext context); + public delegate bool RequestPreProcessEventHandler(RouteListenerContext context); } diff --git a/Rest.Net/Rest.Net.csproj b/Rest.Net/Rest.Net.csproj index 3128b6f..90a083f 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.3 + 1.1.4 HttpListener now returns NotFound if no route was found. Added WithRedirect and WithHtml extensions. Logo_Symbol_Black_Outline.png diff --git a/Rest.Net/Route.cs b/Rest.Net/Route.cs index f654322..edf9746 100644 --- a/Rest.Net/Route.cs +++ b/Rest.Net/Route.cs @@ -25,7 +25,7 @@ namespace MontoyaTech.Rest.Net /// /// The target function to invoke if this route is invoked. /// - private Func Target; + private Func Target; /// /// Whether or not to close the response after the route is invoked. @@ -42,7 +42,7 @@ namespace MontoyaTech.Rest.Net /// /// /// - public Route(string method, string syntax, Func target, bool closeResponse = true) + public Route(string method, string syntax, Func target, bool closeResponse = true) { this.Method = method; this.Syntax = syntax; @@ -58,7 +58,7 @@ namespace MontoyaTech.Rest.Net /// /// /// - public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) + public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) : this(method.ToString(), syntax, target, closeResponse) { } /// @@ -110,7 +110,7 @@ namespace MontoyaTech.Rest.Net /// /// /// - public virtual void Invoke(ListenerContext context, params string[] arguments) + public virtual void Invoke(RouteListenerContext context, params string[] arguments) { this.Target.Invoke(context); } @@ -118,9 +118,9 @@ namespace MontoyaTech.Rest.Net public class Route : Route { - private Func Target; + private Func Target; - public Route(string method, string syntax, Func target, bool closeResponse = true) + public Route(string method, string syntax, Func target, bool closeResponse = true) { this.Method = method; this.Syntax = syntax; @@ -128,10 +128,10 @@ namespace MontoyaTech.Rest.Net this.CloseResponse = closeResponse; } - public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) + public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) : this(method.ToString(), syntax, target, closeResponse) { } - public override void Invoke(ListenerContext context, params string[] arguments) + public override void Invoke(RouteListenerContext context, params string[] arguments) { this.Target.DynamicInvoke(context, RouteArgumentConverter.Convert(arguments[0])); } @@ -139,9 +139,9 @@ namespace MontoyaTech.Rest.Net public class Route : Route { - private Func Target; + private Func Target; - public Route(string method, string syntax, Func target, bool closeResponse = true) + public Route(string method, string syntax, Func target, bool closeResponse = true) { this.Method = method; this.Syntax = syntax; @@ -149,10 +149,10 @@ namespace MontoyaTech.Rest.Net this.CloseResponse = closeResponse; } - public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) + public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) : this(method.ToString(), syntax, target, closeResponse) { } - public override void Invoke(ListenerContext context, params string[] arguments) + public override void Invoke(RouteListenerContext context, params string[] arguments) { this.Target.DynamicInvoke( context, @@ -164,9 +164,9 @@ namespace MontoyaTech.Rest.Net public class Route : Route { - private Func Target; + private Func Target; - public Route(string method, string syntax, Func target, bool closeResponse = true) + public Route(string method, string syntax, Func target, bool closeResponse = true) { this.Method = method; this.Syntax = syntax; @@ -174,10 +174,10 @@ namespace MontoyaTech.Rest.Net this.CloseResponse = closeResponse; } - public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) + public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) : this(method.ToString(), syntax, target, closeResponse) { } - public override void Invoke(ListenerContext context, params string[] arguments) + public override void Invoke(RouteListenerContext context, params string[] arguments) { this.Target.DynamicInvoke( context, @@ -190,9 +190,9 @@ namespace MontoyaTech.Rest.Net public class Route : Route { - private Func Target; + private Func Target; - public Route(string method, string syntax, Func target, bool closeResponse = true) + public Route(string method, string syntax, Func target, bool closeResponse = true) { this.Method = method; this.Syntax = syntax; @@ -200,10 +200,10 @@ namespace MontoyaTech.Rest.Net this.CloseResponse = closeResponse; } - public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) + public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) : this(method.ToString(), syntax, target, closeResponse) { } - public override void Invoke(ListenerContext context, params string[] arguments) + public override void Invoke(RouteListenerContext context, params string[] arguments) { this.Target.DynamicInvoke( context, @@ -217,9 +217,9 @@ namespace MontoyaTech.Rest.Net public class Route : Route { - private Func Target; + private Func Target; - public Route(string method, string syntax, Func target, bool closeResponse = true) + public Route(string method, string syntax, Func target, bool closeResponse = true) { this.Method = method; this.Syntax = syntax; @@ -227,10 +227,10 @@ namespace MontoyaTech.Rest.Net this.CloseResponse = closeResponse; } - public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) + public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) : this(method.ToString(), syntax, target, closeResponse) { } - public override void Invoke(ListenerContext context, params string[] arguments) + public override void Invoke(RouteListenerContext context, params string[] arguments) { this.Target.DynamicInvoke( context, @@ -245,9 +245,9 @@ namespace MontoyaTech.Rest.Net public class Route : Route { - private Func Target; + private Func Target; - public Route(string method, string syntax, Func target, bool closeResponse = true) + public Route(string method, string syntax, Func target, bool closeResponse = true) { this.Method = method; this.Syntax = syntax; @@ -255,10 +255,10 @@ namespace MontoyaTech.Rest.Net this.CloseResponse = closeResponse; } - public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) + public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) : this(method.ToString(), syntax, target, closeResponse) { } - public override void Invoke(ListenerContext context, params string[] arguments) + public override void Invoke(RouteListenerContext context, params string[] arguments) { this.Target.DynamicInvoke( context, @@ -274,9 +274,9 @@ namespace MontoyaTech.Rest.Net public class Route : Route { - private Func Target; + private Func Target; - public Route(string method, string syntax, Func target, bool closeResponse = true) + public Route(string method, string syntax, Func target, bool closeResponse = true) { this.Method = method; this.Syntax = syntax; @@ -284,10 +284,10 @@ namespace MontoyaTech.Rest.Net this.CloseResponse = closeResponse; } - public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) + public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) : this(method.ToString(), syntax, target, closeResponse) { } - public override void Invoke(ListenerContext context, params string[] arguments) + public override void Invoke(RouteListenerContext context, params string[] arguments) { this.Target.DynamicInvoke( context, @@ -304,9 +304,9 @@ namespace MontoyaTech.Rest.Net public class Route : Route { - private Func Target; + private Func Target; - public Route(string method, string syntax, Func target, bool closeResponse = true) + public Route(string method, string syntax, Func target, bool closeResponse = true) { this.Method = method; this.Syntax = syntax; @@ -314,10 +314,10 @@ namespace MontoyaTech.Rest.Net this.CloseResponse = closeResponse; } - public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) + public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) : this(method.ToString(), syntax, target, closeResponse) { } - public override void Invoke(ListenerContext context, params string[] arguments) + public override void Invoke(RouteListenerContext context, params string[] arguments) { this.Target.DynamicInvoke( context, diff --git a/Rest.Net/Events/RouteExceptionEventHandler.cs b/Rest.Net/RouteExceptionEventHandler.cs similarity index 83% rename from Rest.Net/Events/RouteExceptionEventHandler.cs rename to Rest.Net/RouteExceptionEventHandler.cs index fb6ea31..3b53bdc 100644 --- a/Rest.Net/Events/RouteExceptionEventHandler.cs +++ b/Rest.Net/RouteExceptionEventHandler.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace MontoyaTech.Rest.Net.Events +namespace MontoyaTech.Rest.Net { /// /// A delegate that can be used to handle route exceptions. @@ -12,5 +12,5 @@ namespace MontoyaTech.Rest.Net.Events /// /// /// - public delegate void RouteExceptionEventHandler(Route route, ListenerContext context, Exception ex); + public delegate void RouteExceptionEventHandler(Route route, RouteListenerContext context, Exception ex); } diff --git a/Rest.Net/RouteListener.cs b/Rest.Net/RouteListener.cs index cf4b7c0..aaf420d 100644 --- a/Rest.Net/RouteListener.cs +++ b/Rest.Net/RouteListener.cs @@ -5,7 +5,6 @@ using System.Text; using System.Threading.Tasks; using System.Net; using System.Threading; -using MontoyaTech.Rest.Net.Events; namespace MontoyaTech.Rest.Net { @@ -122,7 +121,7 @@ namespace MontoyaTech.Rest.Net bool close = true; string[] arguments = null; - var context = new ListenerContext(ctx.Request, ctx.Response); + var context = new RouteListenerContext(ctx.Request, ctx.Response); //Preprocess the route context, if it returns false, then we have to not invoke the route. try diff --git a/Rest.Net/ListenerContext.cs b/Rest.Net/RouteListenerContext.cs similarity index 87% rename from Rest.Net/ListenerContext.cs rename to Rest.Net/RouteListenerContext.cs index 98cca61..00c789c 100644 --- a/Rest.Net/ListenerContext.cs +++ b/Rest.Net/RouteListenerContext.cs @@ -11,7 +11,7 @@ namespace MontoyaTech.Rest.Net /// An outline of a Listener Context which includes /// the given request and a resposne. /// - public class ListenerContext + public class RouteListenerContext { /// /// The Http Request that requested this route. @@ -28,7 +28,7 @@ namespace MontoyaTech.Rest.Net /// /// /// - public ListenerContext(HttpListenerRequest request, HttpListenerResponse response) + public RouteListenerContext(HttpListenerRequest request, HttpListenerResponse response) { this.Request = request; this.Response = response;