Skip to content

Commit

Permalink
#146 extracted IFlurlClient interface
Browse files Browse the repository at this point in the history
  • Loading branch information
tmenier committed Nov 21, 2016
1 parent 615fd5e commit adae57d
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 121 deletions.
4 changes: 2 additions & 2 deletions src/Flurl.Http.CodeGen/ExtensionMethodModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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" }
from extensionType in new[] { "FlurlClient", "Url", "string" }
from extensionType in new[] { "IFlurlClient", "Url", "string" }
where SupportedCombo(httpVerb, bodyType, extensionType)
from deserializeType in new[] { null, "Json", "JsonList", /*"Xml",*/ "String", "Stream", "Bytes" }
where httpVerb == "Get" || deserializeType == null
Expand All @@ -27,7 +27,7 @@ where AllowDeserializeToGeneric(deserializeType) || !isGeneric
private static bool SupportedCombo(string verb, string bodyType, string extensionType) {
switch (verb) {
case null: // Send
return bodyType != null || extensionType != "FlurlClient";
return bodyType != null || extensionType != "IFlurlClient";
case "Post":
return true;
case "Put":
Expand Down
12 changes: 6 additions & 6 deletions src/Flurl.Http.CodeGen/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ private static void WriteExtensionMethods(CodeWriter writer)
name = xm.Name;
}
writer.WriteLine("/// <summary>");
var summaryStart = (xm.ExtentionOfType == "FlurlClient") ? "Sends" : "Creates a FlurlClient from the URL and sends";
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 == "FlurlClient")
writer.WriteLine("/// <param name=\"client\">The Flurl client.</param>");
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)
Expand All @@ -87,7 +87,7 @@ private static void WriteExtensionMethods(CodeWriter writer)
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"));
args.Add("this " + xm.ExtentionOfType + (xm.ExtentionOfType == "IFlurlClient" ? " client" : " url"));
if (xm.HttpVerb == null)
args.Add("HttpMethod verb");
if (xm.BodyType != null)
Expand All @@ -101,7 +101,7 @@ private static void WriteExtensionMethods(CodeWriter writer)

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.ExtentionOfType == "IFlurlClient")
{
args.Clear();
args.Add(
Expand All @@ -121,7 +121,7 @@ private static void WriteExtensionMethods(CodeWriter writer)
xm.BodyType == "String" ? "data" : $"client.Settings.{xm.BodyType}Serializer.Serialize(data)");
}

var client = (xm.ExtentionOfType == "FlurlClient") ? "client" : "new FlurlClient(url, false)";
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);
}
Expand Down
108 changes: 54 additions & 54 deletions src/Flurl.Http.Shared/ClientConfigExtensions.cs

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions src/Flurl.Http.Shared/CookieExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ public static class CookieExtensions
/// <summary>
/// Allows cookies to be sent and received in calls made with this client. Not necessary to call when setting cookies via WithCookie/WithCookies.
/// </summary>
public static FlurlClient EnableCookies(this FlurlClient client) {
public static IFlurlClient EnableCookies(this IFlurlClient client) {
client.Settings.CookiesEnabled = true;
return client;
}

/// <summary>
/// Allows cookies to be sent and received in calls made to this Url. Not necessary to call when setting cookies via WithCookie/WithCookies.
/// </summary>
public static FlurlClient EnableCookies(this Url url) {
public static IFlurlClient EnableCookies(this Url url) {
return new FlurlClient(url).EnableCookies();
}

/// <summary>
/// Allows cookies to be sent and received in calls made to this Url. Not necessary to call when setting cookies via WithCookie/WithCookies.
/// </summary>
public static FlurlClient EnableCookies(this string url) {
public static IFlurlClient EnableCookies(this string url) {
return new FlurlClient(url).EnableCookies();
}

Expand All @@ -44,7 +44,7 @@ public static FlurlClient EnableCookies(this string url) {
/// <param name="cookie">The cookie to set.</param>
/// <returns>The modified FlurlClient.</returns>
/// <exception cref="ArgumentNullException"><paramref name="cookie" /> is null.</exception>
public static FlurlClient WithCookie(this FlurlClient client, Cookie cookie) {
public static IFlurlClient WithCookie(this IFlurlClient client, Cookie cookie) {
client.Settings.CookiesEnabled = true;
client.Cookies[cookie.Name] = cookie;
return client;
Expand All @@ -57,7 +57,7 @@ public static FlurlClient WithCookie(this FlurlClient client, Cookie cookie) {
/// <param name="cookie">the cookie to set.</param>
/// <returns>The modified FlurlClient.</returns>
/// <exception cref="ArgumentNullException"><paramref name="cookie" /> is null.</exception>
public static FlurlClient WithCookie(this string url, Cookie cookie) {
public static IFlurlClient WithCookie(this string url, Cookie cookie) {
return new FlurlClient(url, true).WithCookie(cookie);
}

Expand All @@ -68,7 +68,7 @@ public static FlurlClient WithCookie(this string url, Cookie cookie) {
/// <param name="cookie">the cookie to set.</param>
/// <returns>The modified FlurlClient.</returns>
/// <exception cref="ArgumentNullException"><paramref name="cookie" /> is null.</exception>
public static FlurlClient WithCookie(this Url url, Cookie cookie) {
public static IFlurlClient WithCookie(this Url url, Cookie cookie) {
return new FlurlClient(url, true).WithCookie(cookie);
}

Expand All @@ -81,7 +81,7 @@ public static FlurlClient WithCookie(this Url url, Cookie cookie) {
/// <param name="expires">cookie expiration (optional). If excluded, cookie only lives for duration of session.</param>
/// <returns>The modified FlurlClient.</returns>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is null.</exception>
public static FlurlClient WithCookie(this FlurlClient client, string name, object value, DateTime? expires = null) {
public static IFlurlClient WithCookie(this IFlurlClient client, string name, object value, DateTime? expires = null) {
var cookie = new Cookie(name, value?.ToInvariantString()) { Expires = expires ?? DateTime.MinValue };
return client.WithCookie(cookie);
}
Expand All @@ -95,7 +95,7 @@ public static FlurlClient WithCookie(this FlurlClient client, string name, objec
/// <param name="expires">cookie expiration (optional). If excluded, cookie only lives for duration of session.</param>
/// <returns>The modified FlurlClient.</returns>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is null.</exception>
public static FlurlClient WithCookie(this string url, string name, object value, DateTime? expires = null) {
public static IFlurlClient WithCookie(this string url, string name, object value, DateTime? expires = null) {
return new FlurlClient(url, true).WithCookie(name, value, expires);
}

Expand All @@ -108,7 +108,7 @@ public static FlurlClient WithCookie(this string url, string name, object value,
/// <param name="expires">cookie expiration (optional). If excluded, cookie only lives for duration of session.</param>
/// <returns>The modified FlurlClient.</returns>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is null.</exception>
public static FlurlClient WithCookie(this Url url, string name, object value, DateTime? expires = null) {
public static IFlurlClient WithCookie(this Url url, string name, object value, DateTime? expires = null) {
return new FlurlClient(url, true).WithCookie(name, value, expires);
}

Expand All @@ -120,7 +120,7 @@ public static FlurlClient WithCookie(this Url url, string name, object value, Da
/// <param name="expires">Expiration for all cookies (optional). If excluded, cookies only live for duration of session.</param>
/// <returns>The modified FlurlClient.</returns>
/// <exception cref="ArgumentNullException"><paramref name="cookies" /> is null.</exception>
public static FlurlClient WithCookies(this FlurlClient client, object cookies, DateTime? expires = null) {
public static IFlurlClient WithCookies(this IFlurlClient client, object cookies, DateTime? expires = null) {
if (cookies == null)
return client;

Expand All @@ -138,7 +138,7 @@ public static FlurlClient WithCookies(this FlurlClient client, object cookies, D
/// <param name="expires">Expiration for all cookies (optional). If excluded, cookies only live for duration of session.</param>
/// <returns>The modified FlurlClient.</returns>
/// <exception cref="ArgumentNullException"><paramref name="cookies" /> is null.</exception>
public static FlurlClient WithCookies(this Url url, object cookies, DateTime? expires = null) {
public static IFlurlClient WithCookies(this Url url, object cookies, DateTime? expires = null) {
return new FlurlClient(url, true).WithCookies(cookies);
}

Expand All @@ -150,7 +150,7 @@ public static FlurlClient WithCookies(this Url url, object cookies, DateTime? ex
/// <param name="expires">Expiration for all cookies (optional). If excluded, cookies only live for duration of session.</param>
/// <returns>The modified FlurlClient.</returns>
/// <exception cref="ArgumentNullException"><paramref name="cookies" /> is null.</exception>
public static FlurlClient WithCookies(this string url, object cookies, DateTime? expires = null) {
public static IFlurlClient WithCookies(this string url, object cookies, DateTime? expires = null) {
return new FlurlClient(url, true).WithCookies(cookies);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Flurl.Http.Shared/DownloadExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class DownloadExtensions
/// <param name="localFileName">Name of local file. If not specified, the source filename (last segment of the URL) is used.</param>
/// <param name="bufferSize">Buffer size in bytes. Default is 4096.</param>
/// <returns>A Task whose result is the local path of the downloaded file.</returns>
public static async Task<string> DownloadFileAsync(this FlurlClient client, string localFolderPath, string localFileName = null, int bufferSize = 4096) {
public static async Task<string> DownloadFileAsync(this IFlurlClient client, string localFolderPath, string localFileName = null, int bufferSize = 4096) {
if (localFileName == null)
localFileName = client.Url.Path.Split('/').Last();

Expand Down
50 changes: 49 additions & 1 deletion src/Flurl.Http.Shared/FlurlClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,58 @@

namespace Flurl.Http
{
/// <summary>
/// Interface defining FlurlClient's contract (useful for mocking and DI)
/// </summary>
public interface IFlurlClient : IDisposable {
/// <summary>
/// Creates a copy of this FlurlClient with a shared instance of HttpClient and HttpMessageHandler
/// </summary>
FlurlClient Clone();

/// <summary>
/// Gets or sets the FlurlHttpSettings object used by this client.
/// </summary>
FlurlHttpSettings Settings { get; set; }

/// <summary>
/// Gets or sets the URL to be called.
/// </summary>
Url Url { get; set; }

/// <summary>
/// Collection of HttpCookies sent and received.
/// </summary>
IDictionary<string, Cookie> Cookies { get; }

/// <summary>
/// Gets the HttpClient to be used in subsequent HTTP calls. Creation (when necessary) is delegated
/// to FlurlHttp.HttpClientFactory. Reused for the life of the FlurlClient.
/// </summary>
HttpClient HttpClient { get; }

/// <summary>
/// Gets the HttpMessageHandler to be used in subsequent HTTP calls. Creation (when necessary) is delegated
/// to FlurlHttp.HttpClientFactory.
/// </summary>
HttpMessageHandler HttpMessageHandler { get; }

/// <summary>
/// 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>
Task<HttpResponseMessage> SendAsync(HttpMethod verb, HttpContent content = null, CancellationToken? cancellationToken = null, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead);
}

/// <summary>
/// A chainable wrapper around HttpClient and Flurl.Url.
/// </summary>
public class FlurlClient : IDisposable
public class FlurlClient : IFlurlClient
{
/// <summary>
/// Initializes a new instance of the <see cref="FlurlClient"/> class.
Expand Down
Loading

0 comments on commit adae57d

Please sign in to comment.