diff --git a/NuGet.Config b/NuGet.Config index 86eec373..9d0b857d 100644 --- a/NuGet.Config +++ b/NuGet.Config @@ -4,7 +4,6 @@ - diff --git a/README.md b/README.md index 161decf8..a03d0635 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,12 @@ # Flurl -[![Build status](https://ci.appveyor.com/api/projects/status/hec8ioqg0j07ttg5/branch/master?svg=true)](https://ci.appveyor.com/project/kroniak/flurl/branch/master) -[![Flurl-stable](https://img.shields.io/nuget/v/Flurl.svg?maxAge=3600&label=Flurl%20nuget)](https://www.nuget.org/packages/Flurl/) -[![Flurl.Http-stable](https://img.shields.io/nuget/v/Flurl.Http.svg?maxAge=3600&label=Flurl.Http%20nuget)](https://www.nuget.org/packages/Flurl.Http/) -[![Flurl-pre-release](https://img.shields.io/nuget/vpre/Flurl.svg?maxAge=3600&label=Flurl%20Pre-Release%20nuget)](https://www.nuget.org/packages/Flurl/) -[![Flurl.Http-pre-release](https://img.shields.io/nuget/vpre/Flurl.Http.svg?maxAge=3600&label=Flurl.Http%20Pre-Release%20nuget)](https://www.nuget.org/packages/Flurl.Http/) +[![build](https://github.com/tmenier/Flurl/actions/workflows/ci.yml/badge.svg)](https://github.com/tmenier/Flurl/actions/workflows/ci.yml) +[![NuGet Version](http://img.shields.io/nuget/v/Flurl.Http.svg?style=flat)](https://www.nuget.org/packages/Flurl.Http/) +[![NuGet Downloads](https://img.shields.io/nuget/dt/Flurl.Http.svg)](https://www.nuget.org/packages/Flurl.Http/) Flurl is a modern, fluent, asynchronous, testable, portable, buzzword-laden URL builder and HTTP client library. -````c# +```cs var result = await "https://api.mysite.com" .AppendPathSegment("person") .SetQueryParams(new { api_key = "xyz" }) @@ -19,20 +17,20 @@ var result = await "https://api.mysite.com" [Test] public void Can_Create_Person() { // fake & record all http calls in the test subject - using (var httpTest = new HttpTest()) { - // arrange - httpTest.RespondWith("OK", 200); + using var httpTest = new HttpTest(); - // act - await sut.CreatePersonAsync("Claire", "Underwood"); + // arrange + httpTest.RespondWith("OK", 200); + + // act + await sut.CreatePersonAsync("Frank", "Reynolds"); - // assert - httpTest.ShouldHaveCalled("http://api.mysite.com/*") - .WithVerb(HttpMethod.Post) - .WithContentType("application/json"); - } + // assert + httpTest.ShouldHaveCalled("http://api.mysite.com/*") + .WithVerb(HttpMethod.Post) + .WithContentType("application/json"); } -```` +``` Get it on NuGet: diff --git a/src/Flurl.Http.Newtonsoft/Flurl.Http.Newtonsoft.csproj b/src/Flurl.Http.Newtonsoft/Flurl.Http.Newtonsoft.csproj index 7837533e..f6d3e546 100644 --- a/src/Flurl.Http.Newtonsoft/Flurl.Http.Newtonsoft.csproj +++ b/src/Flurl.Http.Newtonsoft/Flurl.Http.Newtonsoft.csproj @@ -4,9 +4,10 @@ 9.0 True Flurl.Http.Newtonsoft - 0.9.0-pre3 + 0.9.0 Todd Menier A Newtonsoft-based JSON serializer for Flurl.Http 4.0 and above. + Copyright (c) Todd Menier 2023. https://flurl.dev icon.png https://github.com/tmenier/Flurl.git 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/Flurl.Http.csproj b/src/Flurl.Http/Flurl.Http.csproj index bf383df2..4a466925 100644 --- a/src/Flurl.Http/Flurl.Http.csproj +++ b/src/Flurl.Http/Flurl.Http.csproj @@ -4,11 +4,13 @@ 9.0 True Flurl.Http - 4.0.0-pre7 + 4.0.0 Todd Menier - A fluent, portable, testable HTTP client library. + 4.0 contains breaking changes! See flurl.dev/upgrade + Copyright (c) Todd Menier 2023. https://flurl.dev icon.png + README.md https://github.com/tmenier/Flurl.git git MIT @@ -25,8 +27,9 @@ - + + 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/src/Flurl/Flurl.csproj b/src/Flurl/Flurl.csproj index c3092eff..ae3e3d75 100644 --- a/src/Flurl/Flurl.csproj +++ b/src/Flurl/Flurl.csproj @@ -3,9 +3,10 @@ netstandard2.0;net461;net472 True Flurl - 4.0.0-pre4 + 4.0.0 Todd Menier A fluent, portable URL builder. To make HTTP calls off the fluent chain, check out Flurl.Http. + Copyright (c) Todd Menier 2023. https://flurl.dev icon.png https://github.com/tmenier/Flurl.git 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");