diff --git a/Rest.Net/Rest.Net.csproj b/Rest.Net/Rest.Net.csproj index c559a22..ce7cd54 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.2.3 + 1.2.4 Logo_Symbol_Black_Outline.png diff --git a/Rest.Net/RestCSharpClientGenerator.cs b/Rest.Net/RestCSharpClientGenerator.cs index 9fdc610..943a034 100644 --- a/Rest.Net/RestCSharpClientGenerator.cs +++ b/Rest.Net/RestCSharpClientGenerator.cs @@ -13,6 +13,11 @@ namespace MontoyaTech.Rest.Net /// public class RestCSharpClientGenerator : RestClientGenerator { + /// + /// Whether or not to generate static code, if true the client will be static. + /// + public bool StaticCode = false; + /// /// Generates a CSharp Client from a given set of routes and returns it. /// @@ -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(); //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) diff --git a/Rest.Net/RouteListener.cs b/Rest.Net/RouteListener.cs index 2365b84..e3cbdbc 100644 --- a/Rest.Net/RouteListener.cs +++ b/Rest.Net/RouteListener.cs @@ -224,12 +224,14 @@ namespace MontoyaTech.Rest.Net /// Generates a C# client from this Route Listener and returns the code. /// /// The name of the Client. Default is Client. + /// Whether or not to generate a static client. /// - 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); }