Simplified some class names and structure. New nuget version 1.1.4

This commit is contained in:
MattMo 2023-01-26 11:48:56 -08:00
parent 6db21454c9
commit 9b849f5ec1
8 changed files with 52 additions and 53 deletions

View File

@ -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"));
}

View File

@ -4,11 +4,11 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MontoyaTech.Rest.Net.Events
namespace MontoyaTech.Rest.Net
{
/// <summary>
/// A delegate to post process requests.
/// </summary>
/// <param name="context"></param>
public delegate void RequestPostProcessEventHandler(ListenerContext context);
public delegate void RequestPostProcessEventHandler(RouteListenerContext context);
}

View File

@ -4,12 +4,12 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MontoyaTech.Rest.Net.Events
namespace MontoyaTech.Rest.Net
{
/// <summary>
/// A delegate that can be used to pre process requests and stop them from being handled by routes if needed.
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public delegate bool RequestPreProcessEventHandler(ListenerContext context);
public delegate bool RequestPreProcessEventHandler(RouteListenerContext context);
}

View File

@ -17,7 +17,7 @@
<AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
<RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<Version>1.1.3</Version>
<Version>1.1.4</Version>
<PackageReleaseNotes>HttpListener now returns NotFound if no route was found. Added WithRedirect and WithHtml extensions.</PackageReleaseNotes>
<PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon>
</PropertyGroup>

View File

@ -25,7 +25,7 @@ namespace MontoyaTech.Rest.Net
/// <summary>
/// The target function to invoke if this route is invoked.
/// </summary>
private Func<ListenerContext, HttpListenerResponse> Target;
private Func<RouteListenerContext, HttpListenerResponse> Target;
/// <summary>
/// Whether or not to close the response after the route is invoked.
@ -42,7 +42,7 @@ namespace MontoyaTech.Rest.Net
/// <param name="syntax"></param>
/// <param name="target"></param>
/// <param name="closeResponse"></param>
public Route(string method, string syntax, Func<ListenerContext, HttpListenerResponse> target, bool closeResponse = true)
public Route(string method, string syntax, Func<RouteListenerContext, HttpListenerResponse> target, bool closeResponse = true)
{
this.Method = method;
this.Syntax = syntax;
@ -58,7 +58,7 @@ namespace MontoyaTech.Rest.Net
/// <param name="syntax"></param>
/// <param name="target"></param>
/// <param name="closeResponse"></param>
public Route(HttpRequestMethod method, string syntax, Func<ListenerContext, HttpListenerResponse> target, bool closeResponse = true)
public Route(HttpRequestMethod method, string syntax, Func<RouteListenerContext, HttpListenerResponse> target, bool closeResponse = true)
: this(method.ToString(), syntax, target, closeResponse) { }
/// <summary>
@ -110,7 +110,7 @@ namespace MontoyaTech.Rest.Net
/// </summary>
/// <param name="context"></param>
/// <param name="arguments"></param>
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<T1> : Route
{
private Func<ListenerContext, T1, HttpListenerResponse> Target;
private Func<RouteListenerContext, T1, HttpListenerResponse> Target;
public Route(string method, string syntax, Func<ListenerContext, T1, HttpListenerResponse> target, bool closeResponse = true)
public Route(string method, string syntax, Func<RouteListenerContext, T1, HttpListenerResponse> 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<ListenerContext, T1, HttpListenerResponse> target, bool closeResponse = true)
public Route(HttpRequestMethod method, string syntax, Func<RouteListenerContext, T1, HttpListenerResponse> 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<T1>(arguments[0]));
}
@ -139,9 +139,9 @@ namespace MontoyaTech.Rest.Net
public class Route<T1, T2> : Route
{
private Func<ListenerContext, T1, T2, HttpListenerResponse> Target;
private Func<RouteListenerContext, T1, T2, HttpListenerResponse> Target;
public Route(string method, string syntax, Func<ListenerContext, T1, T2, HttpListenerResponse> target, bool closeResponse = true)
public Route(string method, string syntax, Func<RouteListenerContext, T1, T2, HttpListenerResponse> 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<ListenerContext, T1, T2, HttpListenerResponse> target, bool closeResponse = true)
public Route(HttpRequestMethod method, string syntax, Func<RouteListenerContext, T1, T2, HttpListenerResponse> 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<T1, T2, T3> : Route
{
private Func<ListenerContext, T1, T2, T3, HttpListenerResponse> Target;
private Func<RouteListenerContext, T1, T2, T3, HttpListenerResponse> Target;
public Route(string method, string syntax, Func<ListenerContext, T1, T2, T3, HttpListenerResponse> target, bool closeResponse = true)
public Route(string method, string syntax, Func<RouteListenerContext, T1, T2, T3, HttpListenerResponse> 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<ListenerContext, T1, T2, T3, HttpListenerResponse> target, bool closeResponse = true)
public Route(HttpRequestMethod method, string syntax, Func<RouteListenerContext, T1, T2, T3, HttpListenerResponse> 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<T1, T2, T3, T4> : Route
{
private Func<ListenerContext, T1, T2, T3, T4, HttpListenerResponse> Target;
private Func<RouteListenerContext, T1, T2, T3, T4, HttpListenerResponse> Target;
public Route(string method, string syntax, Func<ListenerContext, T1, T2, T3, T4, HttpListenerResponse> target, bool closeResponse = true)
public Route(string method, string syntax, Func<RouteListenerContext, T1, T2, T3, T4, HttpListenerResponse> 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<ListenerContext, T1, T2, T3, T4, HttpListenerResponse> target, bool closeResponse = true)
public Route(HttpRequestMethod method, string syntax, Func<RouteListenerContext, T1, T2, T3, T4, HttpListenerResponse> 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<T1, T2, T3, T4, T5> : Route
{
private Func<ListenerContext, T1, T2, T3, T4, T5, HttpListenerResponse> Target;
private Func<RouteListenerContext, T1, T2, T3, T4, T5, HttpListenerResponse> Target;
public Route(string method, string syntax, Func<ListenerContext, T1, T2, T3, T4, T5, HttpListenerResponse> target, bool closeResponse = true)
public Route(string method, string syntax, Func<RouteListenerContext, T1, T2, T3, T4, T5, HttpListenerResponse> 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<ListenerContext, T1, T2, T3, T4, T5, HttpListenerResponse> target, bool closeResponse = true)
public Route(HttpRequestMethod method, string syntax, Func<RouteListenerContext, T1, T2, T3, T4, T5, HttpListenerResponse> 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<T1, T2, T3, T4, T5, T6> : Route
{
private Func<ListenerContext, T1, T2, T3, T4, T5, T6, HttpListenerResponse> Target;
private Func<RouteListenerContext, T1, T2, T3, T4, T5, T6, HttpListenerResponse> Target;
public Route(string method, string syntax, Func<ListenerContext, T1, T2, T3, T4, T5, T6, HttpListenerResponse> target, bool closeResponse = true)
public Route(string method, string syntax, Func<RouteListenerContext, T1, T2, T3, T4, T5, T6, HttpListenerResponse> 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<ListenerContext, T1, T2, T3, T4, T5, T6, HttpListenerResponse> target, bool closeResponse = true)
public Route(HttpRequestMethod method, string syntax, Func<RouteListenerContext, T1, T2, T3, T4, T5, T6, HttpListenerResponse> 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<T1, T2, T3, T4, T5, T6, T7> : Route
{
private Func<ListenerContext, T1, T2, T3, T4, T5, T6, T7, HttpListenerResponse> Target;
private Func<RouteListenerContext, T1, T2, T3, T4, T5, T6, T7, HttpListenerResponse> Target;
public Route(string method, string syntax, Func<ListenerContext, T1, T2, T3, T4, T5, T6, T7, HttpListenerResponse> target, bool closeResponse = true)
public Route(string method, string syntax, Func<RouteListenerContext, T1, T2, T3, T4, T5, T6, T7, HttpListenerResponse> 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<ListenerContext, T1, T2, T3, T4, T5, T6, T7, HttpListenerResponse> target, bool closeResponse = true)
public Route(HttpRequestMethod method, string syntax, Func<RouteListenerContext, T1, T2, T3, T4, T5, T6, T7, HttpListenerResponse> 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<T1, T2, T3, T4, T5, T6, T7, T8> : Route
{
private Func<ListenerContext, T1, T2, T3, T4, T5, T6, T7, T8, HttpListenerResponse> Target;
private Func<RouteListenerContext, T1, T2, T3, T4, T5, T6, T7, T8, HttpListenerResponse> Target;
public Route(string method, string syntax, Func<ListenerContext, T1, T2, T3, T4, T5, T6, T7, T8, HttpListenerResponse> target, bool closeResponse = true)
public Route(string method, string syntax, Func<RouteListenerContext, T1, T2, T3, T4, T5, T6, T7, T8, HttpListenerResponse> 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<ListenerContext, T1, T2, T3, T4, T5, T6, T7, T8, HttpListenerResponse> target, bool closeResponse = true)
public Route(HttpRequestMethod method, string syntax, Func<RouteListenerContext, T1, T2, T3, T4, T5, T6, T7, T8, HttpListenerResponse> 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,

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MontoyaTech.Rest.Net.Events
namespace MontoyaTech.Rest.Net
{
/// <summary>
/// A delegate that can be used to handle route exceptions.
@ -12,5 +12,5 @@ namespace MontoyaTech.Rest.Net.Events
/// <param name="route"></param>
/// <param name="context"></param>
/// <param name="ex"></param>
public delegate void RouteExceptionEventHandler(Route route, ListenerContext context, Exception ex);
public delegate void RouteExceptionEventHandler(Route route, RouteListenerContext context, Exception ex);
}

View File

@ -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

View File

@ -11,7 +11,7 @@ namespace MontoyaTech.Rest.Net
/// An outline of a Listener Context which includes
/// the given request and a resposne.
/// </summary>
public class ListenerContext
public class RouteListenerContext
{
/// <summary>
/// The Http Request that requested this route.
@ -28,7 +28,7 @@ namespace MontoyaTech.Rest.Net
/// </summary>
/// <param name="request"></param>
/// <param name="response"></param>
public ListenerContext(HttpListenerRequest request, HttpListenerResponse response)
public RouteListenerContext(HttpListenerRequest request, HttpListenerResponse response)
{
this.Request = request;
this.Response = response;