Skip to content

Commit

Permalink
SendAsync extensions for string and Url, added HttpCompletionOption t…
Browse files Browse the repository at this point in the history
…o all HTTP extension methods
  • Loading branch information
tmenier committed Jul 17, 2016
1 parent 6e5292c commit 17a4579
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 191 deletions.
4 changes: 2 additions & 2 deletions Build/nuspec/Flurl.Http.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Flurl.Http</id>
<version>1.0.0-beta8</version>
<version>1.0.0-beta9</version>
<title>Flurl.Http</title>
<authors>Todd Menier</authors>
<projectUrl>http://tmenier.github.io/Flurl</projectUrl>
Expand All @@ -13,7 +13,7 @@
A fluent, portable, testable HTTP client library that extends Flurl's URL builder chain.
</description>
<releaseNotes>
1.0.0 - Many updates and new features: https://github.com/tmenier/Flurl/releases/tag/Flurl.Http.1.0.0
1.0.0 - Many updates and new features: https://github.com/tmenier/Flurl/releases/tag/Flurl.Http.1.0.0-beta9
0.10.1 - DLL version fix (github #90)
0.10.0 - Lib updates, including Flurl 2.0 which contains breaking changes: https://github.com/tmenier/Flurl/wiki/Release-Notes
0.9.0 - BREAKING CHANGES: https://github.com/tmenier/Flurl/wiki/Release-Notes
Expand Down
6 changes: 3 additions & 3 deletions src/Flurl.Http.CodeGen/ExtensionMethodModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public static IEnumerable<ExtensionMethodModel> GetAll() {
return
from httpVerb in new[] { null, "Get", "Post", "Head", "Put", "Delete", "Patch" }
from bodyType in new[] { null, "Json", /*"Xml",*/ "String", "UrlEncoded" }
where AllowRequestBody(httpVerb, bodyType)
from extensionType in new[] { "FlurlClient", "Url", "string" }
where SupportedCombo(httpVerb, bodyType, extensionType)
from deserializeType in new[] { null, "Json", "JsonList", /*"Xml",*/ "String", "Stream", "Bytes" }
where httpVerb == "Get" || deserializeType == null
from isGeneric in new[] { true, false }
Expand All @@ -24,10 +24,10 @@ where AllowDeserializeToGeneric(deserializeType) || !isGeneric
};
}

private static bool AllowRequestBody(string verb, string bodyType) {
private static bool SupportedCombo(string verb, string bodyType, string extensionType) {
switch (verb) {
case null: // Send
return bodyType != null;
return bodyType != null || extensionType != "FlurlClient";
case "Post":
return true;
case "Put":
Expand Down
29 changes: 15 additions & 14 deletions src/Flurl.Http.CodeGen/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private static void WriteExtensionMethods(CodeWriter writer)
{
string name = null;
foreach (var xm in ExtensionMethodModel.GetAll()) {
var normallyHasContent = (xm.HttpVerb == "Post" || xm.HttpVerb == "Put" || xm.HttpVerb == "Patch");
var hasRequestBody = (xm.HttpVerb == "Post" || xm.HttpVerb == "Put" || xm.HttpVerb == "Patch" || xm.HttpVerb == null);

if (xm.Name != name) {
Console.WriteLine($"writing {xm.Name}...");
Expand All @@ -72,34 +72,34 @@ private static void WriteExtensionMethods(CodeWriter writer)
else
writer.WriteLine("/// @0 an asynchronous @1 request.", summaryStart, xm.HttpVerb.ToUpperInvariant());
writer.WriteLine("/// </summary>");
if (xm.ExtentionOfType == "FlurlClient")
writer.WriteLine("/// <param name=\"client\">The Flurl client.</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 (normallyHasContent)
else if (hasRequestBody)
writer.WriteLine("/// <param name=\"content\">Contents of the request body.</param>");
if (xm.ExtentionOfType == "FlurlClient")
writer.WriteLine("/// <param name=\"client\">The Flurl client.</param>");
if (xm.ExtentionOfType == "Url")
writer.WriteLine("/// <param name=\"url\">The URL.</param>");
if (xm.ExtentionOfType == "string")
writer.WriteLine("/// <param name=\"url\">The URL.</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("/// <returns>A Task whose result is @0.</returns>", xm.ReturnTypeDescription);
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 == "FlurlClient" ? " client" : " url"));
if (xm.HttpVerb == null)
args.Add("HttpMethod verb");
if (xm.BodyType != null)
args.Add((xm.BodyType == "String" ? "string" : "object") + " data");
else if (normallyHasContent)
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));
writer.WriteLine("public static Task<@0> @1@2(@3) {", xm.TaskArg, xm.Name, xm.IsGeneric ? "<T>" : "", string.Join(", ", args));

if (xm.ExtentionOfType == "FlurlClient")
{
Expand All @@ -109,12 +109,13 @@ private static void WriteExtensionMethods(CodeWriter writer)
xm.HttpVerb == "Patch" ? "new HttpMethod(\"PATCH\")" : // there's no HttpMethod.Patch
"HttpMethod." + xm.HttpVerb);

if (xm.BodyType != null || normallyHasContent)
if (xm.BodyType != null || hasRequestBody)
args.Add("content: content");

args.Add("cancellationToken: cancellationToken");
args.Add("completionOption: completionOption");

if (xm.BodyType != null) {
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));
Expand Down
2 changes: 1 addition & 1 deletion src/Flurl.Http.Library/project.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "Flurl.Http",
"version": "1.0.0-beta8",
"version": "1.0.0-beta9",

"dependencies": {
"Flurl": "2.1.0-beta7",
Expand Down
4 changes: 4 additions & 0 deletions src/Flurl.Http.Shared/FlurlClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ private HttpClient EnsureHttpClient(HttpClient hc = null) {
/// Creates and asynchronously sends an HttpRequestMethod, disposing HttpClient if AutoDispose it true.
/// Mainly used to implement higher-level extension methods (GetJsonAsync, etc).
/// </summary>
/// <param name="verb">The HTTP method used to make the request.</param>
/// <param name="content">Contents of the request body.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation. Optional.</param>
/// <param name="completionOption">The HttpCompletionOption used in the request. Optional.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public async Task<HttpResponseMessage> SendAsync(HttpMethod verb, HttpContent content = null, CancellationToken? cancellationToken = null, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) {
try {
Expand Down
Loading

0 comments on commit 17a4579

Please sign in to comment.