Added static code generation option to the CSharp Client generator. Bumped package version to 1.2.4

This commit is contained in:
MattMo 2023-02-06 11:21:56 -08:00
parent 2076a1d02b
commit e4c3ee0673
3 changed files with 79 additions and 30 deletions

View File

@ -17,7 +17,7 @@
<AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
<RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<Version>1.2.3</Version>
<Version>1.2.4</Version>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon>
</PropertyGroup>

View File

@ -13,6 +13,11 @@ namespace MontoyaTech.Rest.Net
/// </summary>
public class RestCSharpClientGenerator : RestClientGenerator
{
/// <summary>
/// Whether or not to generate static code, if true the client will be static.
/// </summary>
public bool StaticCode = false;
/// <summary>
/// Generates a CSharp Client from a given set of routes and returns it.
/// </summary>
@ -46,31 +51,56 @@ namespace MontoyaTech.Rest.Net
writer.WriteBreak().WriteLine($"public class {this.ClientName}").WriteLine("{").Indent();
writer.WriteBreak().WriteLine("public string BaseUrl;");
if (this.StaticCode)
writer.WriteBreak().WriteLine("public static string BaseUrl;");
else
writer.WriteBreak().WriteLine("public string BaseUrl;");
writer.WriteBreak().WriteLine("public HttpClient HttpClient;");
if (this.StaticCode)
writer.WriteBreak().WriteLine("public static HttpClient HttpClient;");
else
writer.WriteBreak().WriteLine("public HttpClient HttpClient;");
//Create fields foreach route group so they can be accessed.
foreach (var group in routeGroups)
writer.WriteBreak().WriteLine($"public {group.Key}Api {group.Key};");
if (!this.StaticCode)
foreach (var group in routeGroups)
writer.WriteBreak().WriteLine($"public {group.Key}Api {group.Key};");
//Create the client constructor
writer.WriteBreak().WriteLine("public Client(string baseUrl)").WriteLine("{").Indent();
//Create the client constructor or init method
if (this.StaticCode)
{
writer.WriteBreak().WriteLine("public static void Init(string baseUrl)").WriteLine("{").Indent();
//Init the base url
writer.WriteLine("this.BaseUrl = baseUrl;");
//Init the base url
writer.WriteLine($"{this.ClientName}.BaseUrl = baseUrl;");
//Init all the route group fields
foreach (var group in routeGroups)
writer.WriteLine($"this.{group.Key} = new {group.Key}Api(this);");
//Init the http client
writer.WriteLine($"{this.ClientName}.HttpClient = new HttpClient();");
writer.WriteLine(@$"{this.ClientName}.HttpClient.DefaultRequestHeaders.Add(""Accept"", ""*/*"");");
writer.WriteLine(@$"{this.ClientName}.HttpClient.DefaultRequestHeaders.Add(""Connection"", ""keep-alive"");");
writer.WriteLine(@$"{this.ClientName}.HttpClient.DefaultRequestHeaders.Add(""Accept-Encoding"", ""identity"");");
//Init the http client
writer.WriteLine("this.HttpClient = new HttpClient();");
writer.WriteLine(@"this.HttpClient.DefaultRequestHeaders.Add(""Accept"", ""*/*"");");
writer.WriteLine(@"this.HttpClient.DefaultRequestHeaders.Add(""Connection"", ""keep-alive"");");
writer.WriteLine(@"this.HttpClient.DefaultRequestHeaders.Add(""Accept-Encoding"", ""identity"");");
writer.Outdent().WriteLine("}");
}
else
{
writer.WriteBreak().WriteLine("public Client(string baseUrl)").WriteLine("{").Indent();
writer.Outdent().WriteLine("}");
//Init the base url
writer.WriteLine("this.BaseUrl = baseUrl;");
//Init all the route group fields
foreach (var group in routeGroups)
writer.WriteLine($"this.{group.Key} = new {group.Key}Api(this);");
//Init the http client
writer.WriteLine("this.HttpClient = new HttpClient();");
writer.WriteLine(@"this.HttpClient.DefaultRequestHeaders.Add(""Accept"", ""*/*"");");
writer.WriteLine(@"this.HttpClient.DefaultRequestHeaders.Add(""Connection"", ""keep-alive"");");
writer.WriteLine(@"this.HttpClient.DefaultRequestHeaders.Add(""Accept-Encoding"", ""identity"");");
writer.Outdent().WriteLine("}");
}
this.GenerateCSharpRouteGroups(routeGroups, writer);
@ -165,20 +195,28 @@ namespace MontoyaTech.Rest.Net
{
writer.WriteBreak();
writer.WriteLine($"public class {name}Api").WriteLine("{").Indent();
//Output the class header
if (this.StaticCode)
writer.WriteLine($"public class {name}").WriteLine("{").Indent();
else
writer.WriteLine($"public class {name}Api").WriteLine("{").Indent();
writer.WriteBreak();
//Output the client instance
if (!this.StaticCode)
writer.WriteBreak().WriteLine("public Client Client;");
writer.WriteLine("public Client Client;");
writer.WriteBreak();
//Output the constuctor if not static.
if (!this.StaticCode)
{
writer.WriteBreak().WriteLine($"public {name}Api(Client client)").WriteLine("{").Indent();
writer.WriteLine($"public {name}Api(Client client)").WriteLine("{").Indent();
writer.WriteLine("this.Client = client;");
writer.WriteLine("this.Client = client;");
writer.Outdent().WriteLine("}");
writer.Outdent().WriteLine("}");
}
//Output all the route functions.
foreach (var route in routes)
this.GenerateCSharpRouteFunction(route, writer);
@ -204,7 +242,10 @@ namespace MontoyaTech.Rest.Net
var routeResponse = methodInfo.GetCustomAttribute<RouteResponse>();
//Generate the route function header
writer.Write($"public {(routeResponse == null ? "void" : this.GetTypeFullyResolvedName(routeResponse.ResponseType))} {(routeName == null ? methodInfo.Name : routeName.Name)}(");
if (this.StaticCode)
writer.Write($"public static {(routeResponse == null ? "void" : this.GetTypeFullyResolvedName(routeResponse.ResponseType))} {(routeName == null ? methodInfo.Name : routeName.Name)}(");
else
writer.Write($"public {(routeResponse == null ? "void" : this.GetTypeFullyResolvedName(routeResponse.ResponseType))} {(routeName == null ? methodInfo.Name : routeName.Name)}(");
//Generate the functions parameters
var parameters = methodInfo.GetParameters();
@ -259,7 +300,10 @@ namespace MontoyaTech.Rest.Net
throw new NotSupportedException("Unsupport route method:" + route.Method);
}
writer.WriteSeparator().Write('$').Write('"').Write("{this.Client.BaseUrl}");
if (this.StaticCode)
writer.WriteSeparator().Write('$').Write('"').Write("{Client.BaseUrl}");
else
writer.WriteSeparator().Write('$').Write('"').Write("{this.Client.BaseUrl}");
//Reconstruct the route syntax into a request url.
var components = route.Syntax.Split('/');
@ -301,7 +345,10 @@ namespace MontoyaTech.Rest.Net
}
//Generate the response code
writer.WriteBreak().WriteLine("var response = this.Client.HttpClient.Send(message);");
if (this.StaticCode)
writer.WriteBreak().WriteLine("var response = Client.HttpClient.Send(message);");
else
writer.WriteBreak().WriteLine("var response = this.Client.HttpClient.Send(message);");
//Handle the response
if (routeResponse != null)

View File

@ -224,12 +224,14 @@ namespace MontoyaTech.Rest.Net
/// Generates a C# client from this Route Listener and returns the code.
/// </summary>
/// <param name="clientName">The name of the Client. Default is Client.</param>
/// <param name="staticCode">Whether or not to generate a static client.</param>
/// <returns></returns>
public string GenerateCSharpClient(string clientName = "Client")
public string GenerateCSharpClient(string clientName = "Client", bool staticCode =false)
{
var generator = new RestCSharpClientGenerator();
generator.ClientName = clientName;
generator.StaticCode = staticCode;
return generator.Generate(this);
}