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)); args.Clear(); args.Add(xm.HttpVerb == "Patch" ? "new HttpMethod(\"PATCH\")" : "HttpMethod." + xm.HttpVerb); // there's no HttpMethod.Patch if (xm.BodyType != null) { args.Add(string.Format("content: new Captured{0}Content(data)", xm.BodyType)); } 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); writer.WriteLine("}").WriteLine(); } }
private static void WriteExtensionMethods(CodeWriter writer) { string name = null; foreach (var xm in ExtensionMethodModel.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 == "IFlurlClient") ? "Sends" : "Creates a FlurlClient 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 == "IFlurlClient") { writer.WriteLine("/// <param name=\"client\">The IFlurlClient 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 (xm.BodyType != null) { writer.WriteLine("/// <param name=\"data\">Contents of the request body.</param>"); } else if (hasRequestBody) { writer.WriteLine("/// <param name=\"content\">Contents of the request body.</param>"); } writer.WriteLine("/// <param name=\"cancellationToken\">A cancellation token that can be used by other objects or threads to receive notice of cancellation. Optional.</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 == "IFlurlClient" ? " client" : " url")); if (xm.HttpVerb == null) { args.Add("HttpMethod verb"); } if (xm.BodyType != null) { args.Add((xm.BodyType == "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 == "IFlurlClient") { args.Clear(); args.Add( xm.HttpVerb == null ? "verb" : xm.HttpVerb == "Patch" ? "new HttpMethod(\"PATCH\")" : // there's no HttpMethod.Patch "HttpMethod." + xm.HttpVerb); if (xm.BodyType != null || hasRequestBody) { args.Add("content: content"); } args.Add("cancellationToken: cancellationToken"); args.Add("completionOption: completionOption"); if (xm.BodyType != null) { writer.WriteLine("var content = new Captured@0Content(@1);", xm.BodyType, xm.BodyType == "String" ? "data" : $"client.Settings.{xm.BodyType}Serializer.Serialize(data)"); } var client = (xm.ExtentionOfType == "IFlurlClient") ? "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(' ')[1]))); } writer.WriteLine("}").WriteLine(); } }