diff --git a/src/Flurl.Http/Configuration/FlurlClientCache.cs b/src/Flurl.Http/Configuration/FlurlClientCache.cs index a6b74a64..cca0dceb 100644 --- a/src/Flurl.Http/Configuration/FlurlClientCache.cs +++ b/src/Flurl.Http/Configuration/FlurlClientCache.cs @@ -14,8 +14,9 @@ public interface IFlurlClientCache /// /// Name of the IFlurlClient. Serves as a cache key. Subsequent calls to Get will return this client. /// Optional. The base URL associated with the new client. - /// A builder to further configure the new client. - IFlurlClientBuilder Add(string name, string baseUrl = null); + /// Optional. Configure the builder associated with the added client. + /// This IFlurlCache. + IFlurlClientCache Add(string name, string baseUrl = null, Action configure = null); /// /// Gets a preconfigured named IFlurlClient. @@ -54,27 +55,6 @@ public interface IFlurlClientCache IFlurlClientCache Clear(); } - /// - /// Extension methods on IFlurlClientCache. - /// - public static class IFlurlClientCacheExtensions - { - /// - /// Adds a new IFlurlClient to this cache. Call once per client at startup to register and configure a named client. - /// Allows configuring via a nested lambda, rather than returning a builder, so multiple Add calls can be fluently chained. - /// - /// This IFlurlCache - /// Name of the IFlurlClient. Serves as a cache key. Subsequent calls to Get will return this client. - /// The base URL associated with the new client. - /// Configure the builder associated with the added client. - /// This IFlurlCache. - public static IFlurlClientCache Add(this IFlurlClientCache cache, string name, string baseUrl, Action configure) { - var builder = cache.Add(name, baseUrl); - configure?.Invoke(builder); - return cache; - } - } - /// /// Default implementation of IFlurlClientCache. /// @@ -84,7 +64,7 @@ public class FlurlClientCache : IFlurlClientCache private readonly List> _defaultConfigs = new(); /// - public IFlurlClientBuilder Add(string name, string baseUrl = null) { + public IFlurlClientCache Add(string name, string baseUrl = null, Action configure = null) { if (name == null) throw new ArgumentNullException(nameof(name)); @@ -92,7 +72,8 @@ public IFlurlClientBuilder Add(string name, string baseUrl = null) { if (!_clients.TryAdd(name, new Lazy(builder.Build))) throw new ArgumentException($"A client named '{name}' was already registered. Add should be called just once per client at startup."); - return builder; + configure?.Invoke(builder); + return this; } /// diff --git a/src/Flurl.Http/FlurlHttp.cs b/src/Flurl.Http/FlurlHttp.cs index 67f01156..7c633c5d 100644 --- a/src/Flurl.Http/FlurlHttp.cs +++ b/src/Flurl.Http/FlurlHttp.cs @@ -21,7 +21,11 @@ public static class FlurlHttp /// Note that if you've overridden the caching strategy to vary clients by request properties other than Url, you should instead use /// FlurlHttp.Clients.Add(name) to ensure you are configuring the correct client. /// - public static IFlurlClientBuilder ConfigureClientForUrl(string url) => Clients.Add(_cachingStrategy(new FlurlRequest(url))); + public static IFlurlClientBuilder ConfigureClientForUrl(string url) { + IFlurlClientBuilder builder = null; + Clients.Add(_cachingStrategy(new FlurlRequest(url)), null, b => builder = b); + return builder; + } /// /// Gets or creates the IFlurlClient that would be selected for sending the given IFlurlRequest when the clientless pattern is used. diff --git a/test/Flurl.Test/Http/FlurlClientCacheTests.cs b/test/Flurl.Test/Http/FlurlClientCacheTests.cs index 17494d6e..ed0ec643 100644 --- a/test/Flurl.Test/Http/FlurlClientCacheTests.cs +++ b/test/Flurl.Test/Http/FlurlClientCacheTests.cs @@ -11,8 +11,10 @@ public class FlurlClientCacheTests [Test] public void can_add_and_get_client() { var cache = new FlurlClientCache(); - cache.Add("github", "https://api.github.com").WithSettings(s => s.Timeout = TimeSpan.FromSeconds(123)); - cache.Add("google", "https://api.google.com").WithSettings(s => s.Timeout = TimeSpan.FromSeconds(234)); + cache.Add("github", "https://api.github.com", builder => + builder.WithSettings(s => s.Timeout = TimeSpan.FromSeconds(123))); + cache.Add("google", "https://api.google.com", builder => + builder.WithSettings(s => s.Timeout = TimeSpan.FromSeconds(234))); var gh = cache.Get("github"); Assert.AreEqual("https://api.github.com", gh.BaseUrl); @@ -68,12 +70,9 @@ public void can_configure_defaults() { var cli1 = cache.GetOrAdd("foo"); - cache.Add("bar").WithSettings(s => { - s.Timeout = TimeSpan.FromSeconds(456); - }); - cache.WithDefaults(b => b.WithSettings(s => { - s.Timeout = TimeSpan.FromSeconds(789); - })); + cache + .Add("bar", null, builder => builder.WithSettings(s => s.Timeout = TimeSpan.FromSeconds(456))) + .WithDefaults(b => b.WithSettings(s => s.Timeout = TimeSpan.FromSeconds(789))); var cli2 = cache.GetOrAdd("bar"); var cli3 = cache.GetOrAdd("buzz");