Working on C# client code generation.
This commit is contained in:
parent
19ccdb9026
commit
6bb01464e7
47
Rest.Net.Example/Client.cs
Normal file
47
Rest.Net.Example/Client.cs
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MontoyaTech.Rest.Net.Example
|
||||||
|
{
|
||||||
|
public class Client
|
||||||
|
{
|
||||||
|
public string BaseUrl = null;
|
||||||
|
|
||||||
|
public TestFunctions Test;
|
||||||
|
|
||||||
|
public class TestFunctions
|
||||||
|
{
|
||||||
|
public Client Client;
|
||||||
|
|
||||||
|
public TestFunctions(Client client)
|
||||||
|
{
|
||||||
|
this.Client = client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Status()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Signup()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Json()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Client(string baseUrl)
|
||||||
|
{
|
||||||
|
this.BaseUrl = baseUrl;
|
||||||
|
this.Test = new TestFunctions(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -34,7 +34,9 @@ namespace MontoyaTech.Rest.Net.Example
|
|||||||
new Route(HttpRequestMethod.Get, "/json", Json)
|
new Route(HttpRequestMethod.Get, "/json", Json)
|
||||||
);
|
);
|
||||||
|
|
||||||
Console.WriteLine(CodeGenerator.GenerateCSharpClient(listener.Routes));
|
string code = CodeGenerator.GenerateCSharpClient(listener.Routes);
|
||||||
|
|
||||||
|
Console.WriteLine(code);
|
||||||
|
|
||||||
Console.ReadLine();
|
Console.ReadLine();
|
||||||
|
|
||||||
@ -61,24 +63,35 @@ namespace MontoyaTech.Rest.Net.Example
|
|||||||
}
|
}
|
||||||
|
|
||||||
[RouteGroup("Test")]
|
[RouteGroup("Test")]
|
||||||
|
[RouteResponse(typeof(string))]
|
||||||
public static HttpListenerResponse Status(HttpListenerContext context)
|
public static HttpListenerResponse Status(HttpListenerContext context)
|
||||||
{
|
{
|
||||||
return context.Response.WithStatus(HttpStatusCode.OK).WithText("Everything is operational. 👍");
|
return context.Response.WithStatus(HttpStatusCode.OK).WithText("Everything is operational. 👍");
|
||||||
}
|
}
|
||||||
|
|
||||||
[RouteGroup("Test")]
|
[RouteGroup("Test")]
|
||||||
|
[RouteResponse(typeof(string))]
|
||||||
public static HttpListenerResponse Add(HttpListenerContext context, double a, double b)
|
public static HttpListenerResponse Add(HttpListenerContext context, double a, double b)
|
||||||
{
|
{
|
||||||
return context.Response.WithStatus(HttpStatusCode.OK).WithText((a + b).ToString());
|
return context.Response.WithStatus(HttpStatusCode.OK).WithText((a + b).ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[RouteGroup("Test")]
|
[RouteGroup("Test")]
|
||||||
|
[RouteResponse(typeof(string))]
|
||||||
public static HttpListenerResponse Signup(HttpListenerContext context, User user)
|
public static HttpListenerResponse Signup(HttpListenerContext context, User user)
|
||||||
{
|
{
|
||||||
return context.Response.WithStatus(HttpStatusCode.OK).WithText("User:" + user.Name);
|
return context.Response.WithStatus(HttpStatusCode.OK).WithText("User:" + user.Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
[RouteGroup("Test")]
|
[RouteGroup("Test")]
|
||||||
|
[RouteRequest(typeof(User))]
|
||||||
|
public static HttpListenerResponse SignupRequest(HttpListenerContext context)
|
||||||
|
{
|
||||||
|
return context.Response.WithStatus(HttpStatusCode.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
[RouteGroup("Test")]
|
||||||
|
[RouteResponse(typeof(User))]
|
||||||
public static HttpListenerResponse Json(HttpListenerContext context)
|
public static HttpListenerResponse Json(HttpListenerContext context)
|
||||||
{
|
{
|
||||||
return context.Response.WithStatus(HttpStatusCode.OK).WithJson(new User("Rest.Net"));
|
return context.Response.WithStatus(HttpStatusCode.OK).WithJson(new User("Rest.Net"));
|
||||||
|
@ -24,21 +24,25 @@ namespace MontoyaTech.Rest.Net
|
|||||||
var writer = new CodeWriter();
|
var writer = new CodeWriter();
|
||||||
|
|
||||||
writer.WriteLine("public class Client").WriteLine("{").Indent();
|
writer.WriteLine("public class Client").WriteLine("{").Indent();
|
||||||
writer.WriteSpacer();
|
|
||||||
writer.WriteLine("public string BaseUrl = null;");
|
writer.WriteLine("public string BaseUrl = null;");
|
||||||
writer.WriteSpacer();
|
|
||||||
|
GenerateCSharpRouteClasses(routes, writer, out List<string> routeClasses);
|
||||||
|
|
||||||
|
writer.WriteBreak();
|
||||||
writer.WriteLine("public Client(string baseUrl)").WriteLine("{").Indent();
|
writer.WriteLine("public Client(string baseUrl)").WriteLine("{").Indent();
|
||||||
writer.WriteLine("this.BaseUrl = baseUrl;");
|
writer.WriteLine("this.BaseUrl = baseUrl;");
|
||||||
writer.Outdent().WriteLine("}");
|
|
||||||
|
|
||||||
GenerateCSharpRouteClasses(routes, writer);
|
foreach (var @class in routeClasses)
|
||||||
|
writer.WriteLine($"this.{@class} = new {@class}Functions(this);");
|
||||||
|
|
||||||
|
writer.Outdent().WriteLine("}");
|
||||||
|
|
||||||
writer.Outdent().WriteLine("}");
|
writer.Outdent().WriteLine("}");
|
||||||
|
|
||||||
return writer.ToString();
|
return writer.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void GenerateCSharpRouteClasses(IList<Route> routes, CodeWriter writer)
|
private static void GenerateCSharpRouteClasses(IList<Route> routes, CodeWriter writer, out List<string> routeClasses)
|
||||||
{
|
{
|
||||||
var groups = new Dictionary<string, List<Route>>();
|
var groups = new Dictionary<string, List<Route>>();
|
||||||
|
|
||||||
@ -46,11 +50,15 @@ namespace MontoyaTech.Rest.Net
|
|||||||
foreach (var route in routes)
|
foreach (var route in routes)
|
||||||
{
|
{
|
||||||
//Get the method that this route is tied to.
|
//Get the method that this route is tied to.
|
||||||
var methodInfo = route.Target.GetMethodInfo();
|
var methodInfo = route.GetTarget().GetMethodInfo();
|
||||||
|
|
||||||
//See if this method defines the group for us.
|
//See if this method defines the group for us.
|
||||||
var routeGroup = methodInfo.GetCustomAttribute<RouteGroup>();
|
var routeGroup = methodInfo.GetCustomAttribute<RouteGroup>();
|
||||||
|
|
||||||
|
//If there wasn't a route group on the method, see if there is one on the declaring type.
|
||||||
|
if (routeGroup == null)
|
||||||
|
routeGroup = methodInfo.DeclaringType.GetCustomAttribute<RouteGroup>();
|
||||||
|
|
||||||
//Group this route.
|
//Group this route.
|
||||||
string group = (routeGroup != null ? routeGroup.Name : methodInfo.DeclaringType.Name);
|
string group = (routeGroup != null ? routeGroup.Name : methodInfo.DeclaringType.Name);
|
||||||
|
|
||||||
@ -59,10 +67,51 @@ namespace MontoyaTech.Rest.Net
|
|||||||
else
|
else
|
||||||
groups.Add(group, new List<Route>() { route });
|
groups.Add(group, new List<Route>() { route });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Generate classes for these groups
|
||||||
|
foreach (var group in groups)
|
||||||
|
GenerateCSharpRouteClass(group.Key, group.Value, writer);
|
||||||
|
|
||||||
|
//Set the route classes
|
||||||
|
routeClasses = new List<string>();
|
||||||
|
routeClasses.AddRange(groups.Select(group => group.Key));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void GenerateCSharpRouteClass(string name, List<Route> routes, CodeWriter writer)
|
private static void GenerateCSharpRouteClass(string name, List<Route> routes, CodeWriter writer)
|
||||||
{
|
{
|
||||||
|
writer.WriteBreak();
|
||||||
|
|
||||||
|
writer.WriteLine($"public {name}Functions {name};");
|
||||||
|
|
||||||
|
writer.WriteBreak();
|
||||||
|
|
||||||
|
writer.WriteLine($"public class {name}Functions").WriteLine("{").Indent();
|
||||||
|
|
||||||
|
writer.WriteLine("public Client Client;");
|
||||||
|
|
||||||
|
writer.WriteBreak();
|
||||||
|
|
||||||
|
writer.WriteLine($"public {name}Functions(Client client)").WriteLine("{").Indent();
|
||||||
|
|
||||||
|
writer.WriteLine("this.Client = client;");
|
||||||
|
|
||||||
|
writer.Outdent().WriteLine("}");
|
||||||
|
|
||||||
|
foreach (var route in routes)
|
||||||
|
GenerateCSharpRouteFunction(route, writer);
|
||||||
|
|
||||||
|
writer.Outdent().WriteLine("}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void GenerateCSharpRouteFunction(Route route, CodeWriter writer)
|
||||||
|
{
|
||||||
|
writer.WriteBreak();
|
||||||
|
|
||||||
|
var methodInfo = route.GetTarget().GetMethodInfo();
|
||||||
|
|
||||||
|
writer.WriteLine($"public void {methodInfo.Name}()").WriteLine("{").Indent();
|
||||||
|
|
||||||
|
writer.Outdent().WriteLine("}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -25,7 +25,7 @@ namespace MontoyaTech.Rest.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The target function to invoke if this route is invoked.
|
/// The target function to invoke if this route is invoked.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal Func<HttpListenerContext, HttpListenerResponse> Target;
|
private Func<HttpListenerContext, HttpListenerResponse> Target;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Whether or not to close the response after the route is invoked.
|
/// Whether or not to close the response after the route is invoked.
|
||||||
@ -47,6 +47,13 @@ namespace MontoyaTech.Rest.Net
|
|||||||
/// <param name="closeResponse"></param>
|
/// <param name="closeResponse"></param>
|
||||||
public Route(string method, string syntax, Func<HttpListenerContext, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(string method, string syntax, Func<HttpListenerContext, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
{
|
{
|
||||||
|
if (target == null)
|
||||||
|
throw new ArgumentNullException(nameof(target));
|
||||||
|
else if (string.IsNullOrWhiteSpace(method))
|
||||||
|
throw new ArgumentException("Cannot be null or empty", nameof(method));
|
||||||
|
else if (string.IsNullOrWhiteSpace(syntax))
|
||||||
|
throw new ArgumentException("Cannot be null or empty", nameof(syntax));
|
||||||
|
|
||||||
this.Method = method;
|
this.Method = method;
|
||||||
this.Syntax = syntax;
|
this.Syntax = syntax;
|
||||||
this.Target = target;
|
this.Target = target;
|
||||||
@ -117,6 +124,15 @@ namespace MontoyaTech.Rest.Net
|
|||||||
{
|
{
|
||||||
this.Target.Invoke(context);
|
this.Target.Invoke(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the target delegate for this route.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual Delegate GetTarget()
|
||||||
|
{
|
||||||
|
return this.Target;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Route<T1> : Route
|
public class Route<T1> : Route
|
||||||
@ -125,6 +141,13 @@ namespace MontoyaTech.Rest.Net
|
|||||||
|
|
||||||
public Route(string method, string syntax, Func<HttpListenerContext, T1, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(string method, string syntax, Func<HttpListenerContext, T1, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
{
|
{
|
||||||
|
if (target == null)
|
||||||
|
throw new ArgumentNullException(nameof(target));
|
||||||
|
else if (string.IsNullOrWhiteSpace(method))
|
||||||
|
throw new ArgumentException("Cannot be null or empty", nameof(method));
|
||||||
|
else if (string.IsNullOrWhiteSpace(syntax))
|
||||||
|
throw new ArgumentException("Cannot be null or empty", nameof(syntax));
|
||||||
|
|
||||||
this.Method = method;
|
this.Method = method;
|
||||||
this.Syntax = syntax;
|
this.Syntax = syntax;
|
||||||
this.Target = target;
|
this.Target = target;
|
||||||
@ -138,6 +161,11 @@ namespace MontoyaTech.Rest.Net
|
|||||||
{
|
{
|
||||||
this.Target.DynamicInvoke(context, RouteArgumentConverter.Convert<T1>(arguments[0]));
|
this.Target.DynamicInvoke(context, RouteArgumentConverter.Convert<T1>(arguments[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override Delegate GetTarget()
|
||||||
|
{
|
||||||
|
return this.Target;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Route<T1, T2> : Route
|
public class Route<T1, T2> : Route
|
||||||
@ -146,6 +174,13 @@ namespace MontoyaTech.Rest.Net
|
|||||||
|
|
||||||
public Route(string method, string syntax, Func<HttpListenerContext, T1, T2, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(string method, string syntax, Func<HttpListenerContext, T1, T2, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
{
|
{
|
||||||
|
if (target == null)
|
||||||
|
throw new ArgumentNullException(nameof(target));
|
||||||
|
else if (string.IsNullOrWhiteSpace(method))
|
||||||
|
throw new ArgumentException("Cannot be null or empty", nameof(method));
|
||||||
|
else if (string.IsNullOrWhiteSpace(syntax))
|
||||||
|
throw new ArgumentException("Cannot be null or empty", nameof(syntax));
|
||||||
|
|
||||||
this.Method = method;
|
this.Method = method;
|
||||||
this.Syntax = syntax;
|
this.Syntax = syntax;
|
||||||
this.Target = target;
|
this.Target = target;
|
||||||
@ -163,6 +198,11 @@ namespace MontoyaTech.Rest.Net
|
|||||||
RouteArgumentConverter.Convert<T2>(arguments[1])
|
RouteArgumentConverter.Convert<T2>(arguments[1])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override Delegate GetTarget()
|
||||||
|
{
|
||||||
|
return this.Target;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Route<T1, T2, T3> : Route
|
public class Route<T1, T2, T3> : Route
|
||||||
@ -171,6 +211,13 @@ namespace MontoyaTech.Rest.Net
|
|||||||
|
|
||||||
public Route(string method, string syntax, Func<HttpListenerContext, T1, T2, T3, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(string method, string syntax, Func<HttpListenerContext, T1, T2, T3, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
{
|
{
|
||||||
|
if (target == null)
|
||||||
|
throw new ArgumentNullException(nameof(target));
|
||||||
|
else if (string.IsNullOrWhiteSpace(method))
|
||||||
|
throw new ArgumentException("Cannot be null or empty", nameof(method));
|
||||||
|
else if (string.IsNullOrWhiteSpace(syntax))
|
||||||
|
throw new ArgumentException("Cannot be null or empty", nameof(syntax));
|
||||||
|
|
||||||
this.Method = method;
|
this.Method = method;
|
||||||
this.Syntax = syntax;
|
this.Syntax = syntax;
|
||||||
this.Target = target;
|
this.Target = target;
|
||||||
@ -189,6 +236,11 @@ namespace MontoyaTech.Rest.Net
|
|||||||
RouteArgumentConverter.Convert<T3>(arguments[2])
|
RouteArgumentConverter.Convert<T3>(arguments[2])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override Delegate GetTarget()
|
||||||
|
{
|
||||||
|
return this.Target;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Route<T1, T2, T3, T4> : Route
|
public class Route<T1, T2, T3, T4> : Route
|
||||||
@ -197,6 +249,13 @@ namespace MontoyaTech.Rest.Net
|
|||||||
|
|
||||||
public Route(string method, string syntax, Func<HttpListenerContext, T1, T2, T3, T4, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(string method, string syntax, Func<HttpListenerContext, T1, T2, T3, T4, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
{
|
{
|
||||||
|
if (target == null)
|
||||||
|
throw new ArgumentNullException(nameof(target));
|
||||||
|
else if (string.IsNullOrWhiteSpace(method))
|
||||||
|
throw new ArgumentException("Cannot be null or empty", nameof(method));
|
||||||
|
else if (string.IsNullOrWhiteSpace(syntax))
|
||||||
|
throw new ArgumentException("Cannot be null or empty", nameof(syntax));
|
||||||
|
|
||||||
this.Method = method;
|
this.Method = method;
|
||||||
this.Syntax = syntax;
|
this.Syntax = syntax;
|
||||||
this.Target = target;
|
this.Target = target;
|
||||||
@ -216,6 +275,11 @@ namespace MontoyaTech.Rest.Net
|
|||||||
RouteArgumentConverter.Convert<T4>(arguments[3])
|
RouteArgumentConverter.Convert<T4>(arguments[3])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override Delegate GetTarget()
|
||||||
|
{
|
||||||
|
return this.Target;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Route<T1, T2, T3, T4, T5> : Route
|
public class Route<T1, T2, T3, T4, T5> : Route
|
||||||
@ -224,6 +288,13 @@ namespace MontoyaTech.Rest.Net
|
|||||||
|
|
||||||
public Route(string method, string syntax, Func<HttpListenerContext, T1, T2, T3, T4, T5, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(string method, string syntax, Func<HttpListenerContext, T1, T2, T3, T4, T5, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
{
|
{
|
||||||
|
if (target == null)
|
||||||
|
throw new ArgumentNullException(nameof(target));
|
||||||
|
else if (string.IsNullOrWhiteSpace(method))
|
||||||
|
throw new ArgumentException("Cannot be null or empty", nameof(method));
|
||||||
|
else if (string.IsNullOrWhiteSpace(syntax))
|
||||||
|
throw new ArgumentException("Cannot be null or empty", nameof(syntax));
|
||||||
|
|
||||||
this.Method = method;
|
this.Method = method;
|
||||||
this.Syntax = syntax;
|
this.Syntax = syntax;
|
||||||
this.Target = target;
|
this.Target = target;
|
||||||
@ -244,6 +315,11 @@ namespace MontoyaTech.Rest.Net
|
|||||||
RouteArgumentConverter.Convert<T5>(arguments[4])
|
RouteArgumentConverter.Convert<T5>(arguments[4])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override Delegate GetTarget()
|
||||||
|
{
|
||||||
|
return this.Target;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Route<T1, T2, T3, T4, T5, T6> : Route
|
public class Route<T1, T2, T3, T4, T5, T6> : Route
|
||||||
@ -252,6 +328,13 @@ namespace MontoyaTech.Rest.Net
|
|||||||
|
|
||||||
public Route(string method, string syntax, Func<HttpListenerContext, T1, T2, T3, T4, T5, T6, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(string method, string syntax, Func<HttpListenerContext, T1, T2, T3, T4, T5, T6, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
{
|
{
|
||||||
|
if (target == null)
|
||||||
|
throw new ArgumentNullException(nameof(target));
|
||||||
|
else if (string.IsNullOrWhiteSpace(method))
|
||||||
|
throw new ArgumentException("Cannot be null or empty", nameof(method));
|
||||||
|
else if (string.IsNullOrWhiteSpace(syntax))
|
||||||
|
throw new ArgumentException("Cannot be null or empty", nameof(syntax));
|
||||||
|
|
||||||
this.Method = method;
|
this.Method = method;
|
||||||
this.Syntax = syntax;
|
this.Syntax = syntax;
|
||||||
this.Target = target;
|
this.Target = target;
|
||||||
@ -273,6 +356,11 @@ namespace MontoyaTech.Rest.Net
|
|||||||
RouteArgumentConverter.Convert<T6>(arguments[5])
|
RouteArgumentConverter.Convert<T6>(arguments[5])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override Delegate GetTarget()
|
||||||
|
{
|
||||||
|
return this.Target;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Route<T1, T2, T3, T4, T5, T6, T7> : Route
|
public class Route<T1, T2, T3, T4, T5, T6, T7> : Route
|
||||||
@ -281,6 +369,13 @@ namespace MontoyaTech.Rest.Net
|
|||||||
|
|
||||||
public Route(string method, string syntax, Func<HttpListenerContext, T1, T2, T3, T4, T5, T6, T7, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(string method, string syntax, Func<HttpListenerContext, T1, T2, T3, T4, T5, T6, T7, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
{
|
{
|
||||||
|
if (target == null)
|
||||||
|
throw new ArgumentNullException(nameof(target));
|
||||||
|
else if (string.IsNullOrWhiteSpace(method))
|
||||||
|
throw new ArgumentException("Cannot be null or empty", nameof(method));
|
||||||
|
else if (string.IsNullOrWhiteSpace(syntax))
|
||||||
|
throw new ArgumentException("Cannot be null or empty", nameof(syntax));
|
||||||
|
|
||||||
this.Method = method;
|
this.Method = method;
|
||||||
this.Syntax = syntax;
|
this.Syntax = syntax;
|
||||||
this.Target = target;
|
this.Target = target;
|
||||||
@ -303,6 +398,11 @@ namespace MontoyaTech.Rest.Net
|
|||||||
RouteArgumentConverter.Convert<T7>(arguments[6])
|
RouteArgumentConverter.Convert<T7>(arguments[6])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override Delegate GetTarget()
|
||||||
|
{
|
||||||
|
return this.Target;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Route<T1, T2, T3, T4, T5, T6, T7, T8> : Route
|
public class Route<T1, T2, T3, T4, T5, T6, T7, T8> : Route
|
||||||
@ -311,6 +411,13 @@ namespace MontoyaTech.Rest.Net
|
|||||||
|
|
||||||
public Route(string method, string syntax, Func<HttpListenerContext, T1, T2, T3, T4, T5, T6, T7, T8, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(string method, string syntax, Func<HttpListenerContext, T1, T2, T3, T4, T5, T6, T7, T8, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
{
|
{
|
||||||
|
if (target == null)
|
||||||
|
throw new ArgumentNullException(nameof(target));
|
||||||
|
else if (string.IsNullOrWhiteSpace(method))
|
||||||
|
throw new ArgumentException("Cannot be null or empty", nameof(method));
|
||||||
|
else if (string.IsNullOrWhiteSpace(syntax))
|
||||||
|
throw new ArgumentException("Cannot be null or empty", nameof(syntax));
|
||||||
|
|
||||||
this.Method = method;
|
this.Method = method;
|
||||||
this.Syntax = syntax;
|
this.Syntax = syntax;
|
||||||
this.Target = target;
|
this.Target = target;
|
||||||
@ -334,5 +441,10 @@ namespace MontoyaTech.Rest.Net
|
|||||||
RouteArgumentConverter.Convert<T8>(arguments[7])
|
RouteArgumentConverter.Convert<T8>(arguments[7])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override Delegate GetTarget()
|
||||||
|
{
|
||||||
|
return this.Target;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ namespace MontoyaTech.Rest.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The outline of an attribute that controls what group a route belongs to.
|
/// The outline of an attribute that controls what group a route belongs to.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
||||||
public class RouteGroup : Attribute
|
public class RouteGroup : Attribute
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
34
Rest.Net/RouteRequest.cs
Normal file
34
Rest.Net/RouteRequest.cs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MontoyaTech.Rest.Net
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The outline of an attribute that defines a routes request type.
|
||||||
|
/// </summary>
|
||||||
|
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
||||||
|
public class RouteRequest : Attribute
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The type of the request.
|
||||||
|
/// </summary>
|
||||||
|
public Type RequestType = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a default route request.
|
||||||
|
/// </summary>
|
||||||
|
public RouteRequest() { }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a route request with the request type.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="requestType"></param>
|
||||||
|
public RouteRequest(Type requestType)
|
||||||
|
{
|
||||||
|
this.RequestType = requestType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
34
Rest.Net/RouteResponse.cs
Normal file
34
Rest.Net/RouteResponse.cs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MontoyaTech.Rest.Net
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The outline of an attribute that defines a routes response type.
|
||||||
|
/// </summary>
|
||||||
|
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
||||||
|
public class RouteResponse : Attribute
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The type of the request.
|
||||||
|
/// </summary>
|
||||||
|
public Type ResponseType = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a default route response.
|
||||||
|
/// </summary>
|
||||||
|
public RouteResponse() { }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a route response with the response type.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="responseType"></param>
|
||||||
|
public RouteResponse(Type responseType)
|
||||||
|
{
|
||||||
|
this.ResponseType = responseType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user