Skip to content

Commit

Permalink
#146 FlurlClient now has a constructor that takes a FlurlHttpSettings…
Browse files Browse the repository at this point in the history
… object. Also one that takes a Action<FlurlHttpSettings> for if you just want to change one or a few default settings.
  • Loading branch information
tmenier committed Nov 20, 2016
1 parent 10a43ee commit f0cfb70
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 37 deletions.
8 changes: 4 additions & 4 deletions Test/Flurl.Test.Shared/Http/ClientLifetimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class ClientLifetimeTests
[Test]
public async Task autodispose_true_creates_new_httpclients() {
var fac = new TestHttpClientFactoryWithCounter();
var fc = new FlurlClient("http://www.mysite.com", true) {
Settings = { HttpClientFactory = fac }
var fc = new FlurlClient("http://www.mysite.com") {
Settings = { HttpClientFactory = fac, AutoDispose = true }
};
var x = await fc.GetAsync();
var y = await fc.GetAsync();
Expand All @@ -26,8 +26,8 @@ public async Task autodispose_true_creates_new_httpclients() {
[Test]
public async Task autodispose_false_reuses_httpclient() {
var fac = new TestHttpClientFactoryWithCounter();
var fc = new FlurlClient("http://www.mysite.com", false) {
Settings = { HttpClientFactory = fac }
var fc = new FlurlClient("http://www.mysite.com") {
Settings = { HttpClientFactory = fac, AutoDispose = false }
};
var x = await fc.GetAsync();
var y = await fc.GetAsync();
Expand Down
2 changes: 1 addition & 1 deletion src/Flurl.Http.Shared/ClientConfigExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static FlurlClient WithUrl(this FlurlClient client, Url url) {
var fc = client.Clone();
fc.Url = url;
// prevent the new client from automatically disposing the parent's HttpClient
fc.AutoDispose = false;
fc.Settings.AutoDispose = false;
return fc;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Flurl.Http.Shared/Configuration/FlurlHttpSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public FlurlHttpSettings() {
ResetDefaults();
}

/// <summary>
/// Gets or sets value indicating whether to automatically dispose underlying HttpClient immediately after each call.
/// </summary>
public bool AutoDispose { get; set; }

/// <summary>
/// Gets or sets the default timeout for every HTTP request.
/// </summary>
Expand Down Expand Up @@ -87,6 +92,7 @@ public FlurlHttpSettings() {
/// Clear all custom global options and set default values.
/// </summary>
public void ResetDefaults() {
AutoDispose = false;
DefaultTimeout = new HttpClient().Timeout;
AllowedHttpStatusRange = null;
CookiesEnabled = false;
Expand Down
8 changes: 4 additions & 4 deletions src/Flurl.Http.Shared/DownloadExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static async Task<string> DownloadFileAsync(this FlurlClient client, stri
localFileName = client.Url.Path.Split('/').Last();

// need to temporarily disable autodispose if set, otherwise reading from stream will fail
var autoDispose = client.AutoDispose;
client.AutoDispose = false;
var autoDispose = client.Settings.AutoDispose;
client.Settings.AutoDispose = false;

try {
var response = await client.SendAsync(HttpMethod.Get, completionOption: HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
Expand All @@ -37,8 +37,8 @@ public static async Task<string> DownloadFileAsync(this FlurlClient client, stri
return FileUtil.CombinePath(localFolderPath, localFileName);
}
finally {
client.AutoDispose = autoDispose;
if (client.AutoDispose)
client.Settings.AutoDispose = autoDispose;
if (client.Settings.AutoDispose)
client.Dispose();
}
}
Expand Down
61 changes: 33 additions & 28 deletions src/Flurl.Http.Shared/FlurlClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,53 +18,64 @@ public class FlurlClient : IDisposable
/// <summary>
/// Initializes a new instance of the <see cref="FlurlClient"/> class.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="autoDispose">if set to <c>true</c> [automatic dispose].</param>
public FlurlClient(Url url, bool autoDispose) {
Url = url;
AutoDispose = autoDispose;
Settings = FlurlHttp.GlobalSettings.Clone();
/// <param name="settings">The FlurlHttpSettings associated with this instance.</param>
public FlurlClient(FlurlHttpSettings settings = null) {
Settings = settings ?? FlurlHttp.GlobalSettings.Clone();
}

/// <summary>
/// Initializes a new instance of the <see cref="FlurlClient"/> class.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="autoDispose">if set to <c>true</c> [automatic dispose].</param>
/// <exception cref="ArgumentNullException"><paramref name="url" /> is <see langword="null" />.</exception>
public FlurlClient(string url, bool autoDispose) : this(new Url(url), autoDispose) { }
/// <param name="configure">Action allowing you to overide default settings inline.</param>
public FlurlClient(Action<FlurlHttpSettings> configure) : this() {
configure(Settings);
}

/// <summary>
/// Initializes a new instance of the <see cref="FlurlClient"/> class.
/// </summary>
/// <param name="url">The URL.</param>
public FlurlClient(Url url) : this(url, false) { }
/// <param name="url">The URL to call with this FlurlClient instance.</param>
public FlurlClient(Url url) : this() {
Url = url;
}

/// <summary>
/// Initializes a new instance of the <see cref="FlurlClient"/> class.
/// </summary>
/// <param name="url">The URL.</param>
/// <exception cref="ArgumentNullException"><paramref name="url" /> is <see langword="null" />.</exception>
public FlurlClient(string url) : this(new Url(url), false) { }

/// <param name="url">The URL to call with this FlurlClient instance.</param>
public FlurlClient(string url) : this() {
Url = new Url(url);
}

/// <summary>
/// Initializes a new instance of the <see cref="FlurlClient"/> class.
/// </summary>
public FlurlClient() : this((Url)null, false) { }
/// <param name="url">The URL to call with this FlurlClient instance.</param>
/// <param name="autoDispose">Indicates whether to automatically dispose underlying HttpClient immediately after each call.</param>
public FlurlClient(Url url, bool autoDispose) : this(url) {
Settings.AutoDispose = autoDispose;
}

/// <summary>
/// Creates a copy of this FlurlClient with a shared instance of HttpClient and HttpMessageHandler
/// Initializes a new instance of the <see cref="FlurlClient"/> class.
/// </summary>
/// <param name="url">The URL to call with this FlurlClient instance.</param>
/// <param name="autoDispose">Indicates whether to automatically dispose underlying HttpClient immediately after each call.</param>
public FlurlClient(string url, bool autoDispose) : this(url) {
Settings.AutoDispose = autoDispose;
}

/// <summary>
/// Creates a copy of this FlurlClient with a shared instance of HttpClient and HttpMessageHandler
/// </summary>
public FlurlClient Clone() {
return new FlurlClient {
_httpClient = _httpClient,
_httpMessageHandler = _httpMessageHandler,
_parent = this,
Settings = Settings,
Url = Url,
Cookies = Cookies,
AutoDispose = AutoDispose
Cookies = Cookies
};
}

Expand All @@ -87,12 +98,6 @@ public FlurlClient Clone() {
/// </summary>
public IDictionary<string, Cookie> Cookies { get; private set; } = new Dictionary<string, Cookie>();

/// <summary>
/// Gets a value indicating whether the underlying HttpClient
/// should be disposed immediately after the first HTTP call is made.
/// </summary>
public bool AutoDispose { get; set; }

/// <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.
Expand Down Expand Up @@ -151,7 +156,7 @@ public async Task<HttpResponseMessage> SendAsync(HttpMethod verb, HttpContent co
return resp;
}
finally {
if (AutoDispose) Dispose();
if (Settings.AutoDispose) Dispose();
}
}

Expand Down

0 comments on commit f0cfb70

Please sign in to comment.