Skip to content

Commit

Permalink
#770 rename ConfigureAll -> WithDefaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Todd committed Nov 3, 2023
1 parent 8f4fe46 commit 94f0c3d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
15 changes: 8 additions & 7 deletions src/Flurl.Http/Configuration/FlurlClientCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ public interface IFlurlClientCache
IFlurlClient GetOrAdd(string name, string baseUrl = null, Action<IFlurlClientBuilder> configure = null);

/// <summary>
/// Configuration logic that gets executed for every new IFlurlClient added this case. Good place for things like default
/// settings. Executes before client-specific builder logic.
/// Adds initialization logic that gets executed for every new IFlurlClient added this cache.
/// Good place for things like default settings. Executes before client-specific builder logic.
/// Call at startup (or whenever the cache is first created); clients already cached will NOT have this logic applied.
/// </summary>
/// <returns>This IFlurlCache.</returns>
IFlurlClientCache ConfigureAll(Action<IFlurlClientBuilder> configure);
IFlurlClientCache WithDefaults(Action<IFlurlClientBuilder> configure);

/// <summary>
/// Removes a named client from this cache.
Expand Down Expand Up @@ -80,7 +81,7 @@ public static IFlurlClientCache Add(this IFlurlClientCache cache, string name, s
public class FlurlClientCache : IFlurlClientCache
{
private readonly ConcurrentDictionary<string, Lazy<IFlurlClient>> _clients = new();
private readonly List<Action<IFlurlClientBuilder>> _configureAll = new();
private readonly List<Action<IFlurlClientBuilder>> _defaultConfigs = new();

/// <inheritdoc />
public IFlurlClientBuilder Add(string name, string baseUrl = null) {
Expand Down Expand Up @@ -123,9 +124,9 @@ Lazy<IFlurlClient> Create() {
}

/// <inheritdoc />
public IFlurlClientCache ConfigureAll(Action<IFlurlClientBuilder> configure) {
public IFlurlClientCache WithDefaults(Action<IFlurlClientBuilder> configure) {
if (configure != null)
_configureAll.Add(configure);
_defaultConfigs.Add(configure);
return this;
}

Expand All @@ -146,7 +147,7 @@ public IFlurlClientCache Clear() {

private IFlurlClientBuilder CreateBuilder(string baseUrl) {
var builder = new FlurlClientBuilder(baseUrl);
foreach (var config in _configureAll)
foreach (var config in _defaultConfigs)
config(builder);
return builder;
}
Expand Down
6 changes: 3 additions & 3 deletions test/Flurl.Test/Http/FlurlClientCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ public void cannot_add_same_name_twice() {
}

[Test]
public void can_configure_all() {
public void can_configure_defaults() {
var cache = new FlurlClientCache()
.ConfigureAll(b => b.WithSettings(s => s.Timeout = TimeSpan.FromSeconds(123)));
.WithDefaults(b => b.Settings.Timeout = TimeSpan.FromSeconds(123));

var cli1 = cache.GetOrAdd("foo");

cache.Add("bar").WithSettings(s => {
s.Timeout = TimeSpan.FromSeconds(456);
});
cache.ConfigureAll(b => b.WithSettings(s => {
cache.WithDefaults(b => b.WithSettings(s => {
s.Timeout = TimeSpan.FromSeconds(789);
}));

Expand Down

0 comments on commit 94f0c3d

Please sign in to comment.