static void Main(string[] args) { if (args.Length == 0) throw new ArgumentException("Must provide a path to the .cs output file."); var codePath = args[0]; using (var writer = new CodeWriter(codePath)) { writer .WriteLine("// This file was auto-generated by Flurl.Http.CodeGen. Do not edit directly.") .WriteLine() .WriteLine("using System.Collections.Generic;") .WriteLine("using System.IO;") .WriteLine("using System.Net.Http;") .WriteLine("using System.Threading;") .WriteLine("using System.Threading.Tasks;") .WriteLine("using Flurl.Http.Content;") .WriteLine("") .WriteLine("namespace Flurl.Http") .WriteLine("{") .WriteLine("public static class HttpExtensions") .WriteLine("{"); WriteExtensionMethods(writer); writer .WriteLine("}") .WriteLine("}"); } }
private static void WriteExtensionMethods(CodeWriter writer) { foreach (var xm in ExtensionMethodModel.GetAll()) { writer.WriteLine("/// <summary>"); var summaryStart = (xm.ExtentionOfType == "FlurlClient") ? "Sends" : "Creates a FlurlClient from the URL and sends"; writer.WriteLine("/// @0 an asynchronous @1 request.", summaryStart, xm.HttpVerb.ToUpperInvariant()); writer.WriteLine("/// </summary>"); if (xm.BodyType != null) writer.WriteLine("/// <param name=\"data\">Contents of the request body.</param>"); if (xm.HasCancelationToken) writer.WriteLine("/// <param name=\"cancellationToken\">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>"); writer.WriteLine("/// <returns>A Task whose result is @0.</returns>", xm.ReturnTypeDescription); var args = new List<string>(); args.Add("this " + xm.ExtentionOfType + (xm.ExtentionOfType == "FlurlClient" ? " client" : " url")); if (xm.BodyType != null) args.Add((xm.BodyType == "String" ? "string" : "object") + " data"); if (xm.HasCancelationToken) args.Add("CancellationToken cancellationToken"); writer.WriteLine("public static Task<@0> @1@2(@3) {", xm.TaskArg, xm.Name, xm.IsGeneric ? "<T>" : "", string.Join(", ", args)); if (xm.ExtentionOfType == "FlurlClient") { if (xm.BodyType != null) { writer.WriteLine("var content = new Captured@0Content(@1);", xm.BodyType, xm.BodyType == "String" ? "data" : string.Format("client.Settings.{0}Serializer.Serialize(data)", xm.BodyType)); } args.Clear(); args.Add(xm.HttpVerb == "Patch" ? "new HttpMethod(\"PATCH\")" : "HttpMethod." + xm.HttpVerb); // there's no HttpMethod.Patch if (xm.BodyType != null) args.Add("content: content"); if (xm.HasCancelationToken) args.Add("cancellationToken: cancellationToken"); var client = (xm.ExtentionOfType == "FlurlClient") ? "client" : "new FlurlClient(url, false)"; var receive = (xm.DeserializeToType == null) ? "" : string.Format(".Receive{0}{1}()", xm.DeserializeToType, xm.IsGeneric ? "<T>" : ""); writer.WriteLine("return @0.SendAsync(@1)@2;", client, string.Join(", ", args), receive); } else { writer.WriteLine("return new FlurlClient(url, false).@0(@1);", xm.Name + (xm.IsGeneric ? "<T>" : ""), string.Join(", ", args.Skip(1).Select(a => a.Split(' ').Last()))); } writer.WriteLine("}").WriteLine(); } }
private static void WriteExtensionMethods(CodeWriter writer) { string name = null; foreach (var xm in HttpExtensionMethod.GetAll()) { var hasRequestBody = (xm.HttpVerb == "Post" || xm.HttpVerb == "Put" || xm.HttpVerb == "Patch" || xm.HttpVerb == null); if (xm.Name != name) { Console.WriteLine($"writing {xm.Name}..."); name = xm.Name; } writer.WriteLine("/// <summary>"); var summaryStart = (xm.ExtentionOfType == "IFlurlRequest") ? "Sends" : "Creates a FlurlRequest from the URL and sends"; if (xm.HttpVerb == null) { writer.WriteLine("/// @0 an asynchronous request.", summaryStart); } else { writer.WriteLine("/// @0 an asynchronous @1 request.", summaryStart, xm.HttpVerb.ToUpperInvariant()); } writer.WriteLine("/// </summary>"); if (xm.ExtentionOfType == "IFlurlRequest") { writer.WriteLine("/// <param name=\"request\">The IFlurlRequest instance.</param>"); } if (xm.ExtentionOfType == "Url" || xm.ExtentionOfType == "string") { writer.WriteLine("/// <param name=\"url\">The URL.</param>"); } if (xm.HttpVerb == null) { writer.WriteLine("/// <param name=\"verb\">The HTTP method used to make the request.</param>"); } if (hasRequestBody) { if (xm.RequestBodyType == "Json") { writer.WriteLine("/// <param name=\"data\">An object representing the request body, which will be serialized to JSON.</param>"); } else if (xm.RequestBodyType != null) { writer.WriteLine("/// <param name=\"data\">Contents of the request body.</param>"); } else { writer.WriteLine("/// <param name=\"content\">Contents of the request body.</param>"); } } writer.WriteLine("/// <param name=\"cancellationToken\">The token to monitor for cancellation requests.</param>"); writer.WriteLine("/// <param name=\"completionOption\">The HttpCompletionOption used in the request. Optional.</param>"); writer.WriteLine("/// <returns>A Task whose result is @0.</returns>", xm.ReturnTypeDescription); var args = new List <string>(); args.Add("this " + xm.ExtentionOfType + (xm.ExtentionOfType == "IFlurlRequest" ? " request" : " url")); if (xm.HttpVerb == null) { args.Add("HttpMethod verb"); } if (xm.RequestBodyType != null) { args.Add((xm.RequestBodyType == "String" ? "string" : "object") + " data"); } else if (hasRequestBody) { args.Add("HttpContent content"); } // http://stackoverflow.com/questions/22359706/default-parameter-for-cancellationtoken args.Add("CancellationToken cancellationToken = default(CancellationToken)"); args.Add("HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead"); writer.WriteLine("public static Task<@0> @1@2(@3) {", xm.TaskArg, xm.Name, xm.IsGeneric ? "<T>" : "", string.Join(", ", args)); if (xm.ExtentionOfType == "IFlurlRequest") { args.Clear(); args.Add( xm.HttpVerb == null ? "verb" : xm.HttpVerb == "Patch" ? "new HttpMethod(\"PATCH\")" : // there's no HttpMethod.Patch "HttpMethod." + xm.HttpVerb); if (xm.RequestBodyType != null || hasRequestBody) { args.Add("content: content"); } args.Add("cancellationToken: cancellationToken"); args.Add("completionOption: completionOption"); if (xm.RequestBodyType != null) { writer.WriteLine("var content = new Captured@0Content(@1);", xm.RequestBodyType, xm.RequestBodyType == "String" ? "data" : $"request.Settings.{xm.RequestBodyType}Serializer.Serialize(data)"); } var request = (xm.ExtentionOfType == "IFlurlRequest") ? "request" : "new FlurlRequest(url)"; var receive = (xm.ResponseBodyType == null) ? "" : string.Format(".Receive{0}{1}()", xm.ResponseBodyType, xm.IsGeneric ? "<T>" : ""); writer.WriteLine("return @0.SendAsync(@1)@2;", request, string.Join(", ", args), receive); } else { writer.WriteLine("return new FlurlRequest(url).@0(@1);", xm.Name + (xm.IsGeneric ? "<T>" : ""), string.Join(", ", args.Skip(1).Select(a => a.Split(' ')[1]))); } writer.WriteLine("}").WriteLine(); } foreach (var xtype in new[] { "Url", "string" }) { foreach (var xm in UrlExtensionMethod.GetAll()) { if (xm.Name != name) { Console.WriteLine($"writing {xm.Name}..."); name = xm.Name; } writer.WriteLine("/// <summary>"); writer.WriteLine($"/// {xm.Description}"); writer.WriteLine("/// </summary>"); writer.WriteLine("/// <param name=\"url\">The URL.</param>"); foreach (var p in xm.Params) { writer.WriteLine($"/// <param name=\"{p.Name}\">{p.Description}</param>"); } writer.WriteLine("/// <returns>The IFlurlRequest.</returns>"); var argList = new List <string> { $"this {xtype} url" }; argList.AddRange(xm.Params.Select(p => $"{p.Type} {p.Name}" + (p.Default == null ? "" : $" = {p.Default}"))); writer.WriteLine($"public static IFlurlRequest {xm.Name}({string.Join(", ", argList)}) {{"); writer.WriteLine($"return new FlurlRequest(url).{xm.Name}({string.Join(", ", xm.Params.Select(p => p.Name))});"); writer.WriteLine("}"); } } }