From 99d38c1e4f4e5e173ab01fe0df29d568a7870a47 Mon Sep 17 00:00:00 2001 From: Jan Abramek Date: Fri, 30 Aug 2024 13:43:24 +0200 Subject: [PATCH] Added ability to pass custom IFlurlClientFactory instance --- src/Flurl.CodeGen/CodeWriter.cs | 4 ++-- src/Flurl.CodeGen/HttpExtensionMethod.cs | 2 +- src/Flurl.CodeGen/Metadata.cs | 4 +--- src/Flurl.Http.Newtonsoft/ExtensionMethods.cs | 3 +-- .../Configuration/FlurlClientBuilder.cs | 16 +++++++++++++--- src/Flurl.Http/Configuration/ISerializer.cs | 5 +---- src/Flurl.Http/Configuration/RedirectSettings.cs | 9 +-------- src/Flurl.Http/Content/CapturedJsonContent.cs | 4 +--- src/Flurl.Http/Content/CapturedStringContent.cs | 2 -- src/Flurl.Http/FlurlClient.cs | 2 +- src/Flurl.Http/FlurlEventHandler.cs | 1 - src/Flurl.Http/FlurlRequest.cs | 3 +-- src/Flurl.Http/GeneratedExtensions.cs | 1 - src/Flurl.Http/HttpMessageExtensions.cs | 3 +-- src/Flurl.Http/IHeadersContainer.cs | 2 +- src/Flurl.Http/ISettingsContainer.cs | 2 -- src/Flurl.Http/Testing/Util.cs | 6 ++---- src/Flurl/NullValueHandling.cs | 6 +----- src/Flurl/Url.cs | 4 ++-- src/Flurl/Util/NameValueList.cs | 9 ++++----- 20 files changed, 34 insertions(+), 54 deletions(-) diff --git a/src/Flurl.CodeGen/CodeWriter.cs b/src/Flurl.CodeGen/CodeWriter.cs index 7c0efa58..ee1650f5 100644 --- a/src/Flurl.CodeGen/CodeWriter.cs +++ b/src/Flurl.CodeGen/CodeWriter.cs @@ -18,7 +18,7 @@ public CodeWriter(string filePath) } /// - /// use @0, @1, @2, etc for tokens. ({0} would be a pain because you'd alway need to escape "{" and "}") + /// use @0, @1, @2, etc. for tokens. ({0} would be a pain because you'd always need to escape "{" and "}") /// public CodeWriter WriteLine(string line, params object[] args) { @@ -30,7 +30,7 @@ public CodeWriter WriteLine(string line, params object[] args) line = line.Replace("@" + i, val); } - if (line == "}" || line == "{") + if (line is "}" or "{") { _indent--; } diff --git a/src/Flurl.CodeGen/HttpExtensionMethod.cs b/src/Flurl.CodeGen/HttpExtensionMethod.cs index e3dfd843..d9520b28 100644 --- a/src/Flurl.CodeGen/HttpExtensionMethod.cs +++ b/src/Flurl.CodeGen/HttpExtensionMethod.cs @@ -40,7 +40,7 @@ public HttpExtensionMethod(string verb, bool isGeneric, string reqBodyType, stri " an asynchronous ", (HttpVerb == null) ? "request." : $"{HttpVerb.ToUpperInvariant()} request."); - public bool HasRequestBody => (HttpVerb == "Post" || HttpVerb == "Put" || HttpVerb == "Patch" || HttpVerb == null); + public bool HasRequestBody => HttpVerb is "Post" or "Put" or "Patch" or null; public string HttpVerb { get; } public string RequestBodyType { get; } diff --git a/src/Flurl.CodeGen/Metadata.cs b/src/Flurl.CodeGen/Metadata.cs index 0bf9f392..645735c2 100644 --- a/src/Flurl.CodeGen/Metadata.cs +++ b/src/Flurl.CodeGen/Metadata.cs @@ -1,7 +1,5 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using System.Text; namespace Flurl.CodeGen { diff --git a/src/Flurl.Http.Newtonsoft/ExtensionMethods.cs b/src/Flurl.Http.Newtonsoft/ExtensionMethods.cs index 5c18248b..830c80f9 100644 --- a/src/Flurl.Http.Newtonsoft/ExtensionMethods.cs +++ b/src/Flurl.Http.Newtonsoft/ExtensionMethods.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Dynamic; using System.Threading.Tasks; using Flurl.Http.Configuration; diff --git a/src/Flurl.Http/Configuration/FlurlClientBuilder.cs b/src/Flurl.Http/Configuration/FlurlClientBuilder.cs index 44d1a4d2..d746e2dd 100644 --- a/src/Flurl.Http/Configuration/FlurlClientBuilder.cs +++ b/src/Flurl.Http/Configuration/FlurlClientBuilder.cs @@ -1,9 +1,11 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; -using System.Runtime.Versioning; using Flurl.Util; +#if NET +using System.Runtime.Versioning; +#endif namespace Flurl.Http.Configuration { @@ -42,6 +44,11 @@ public interface IFlurlClientBuilder : ISettingsContainer, IHeadersContainer, IE /// Builds an instance of IFlurlClient based on configurations specified. /// IFlurlClient Build(); + + /// + /// Configure factory + /// + void WithFactory(Func create); } /// @@ -72,6 +79,9 @@ public FlurlClientBuilder(string baseUrl = null) { _baseUrl = baseUrl; } + /// + public void WithFactory(Func create) => _factory = create(); + /// public IFlurlClientBuilder AddMiddleware(Func create) { _addMiddleware.Add(create); @@ -103,7 +113,7 @@ public IFlurlClientBuilder UseSocketsHttpHandler(Action conf if (_factory is DefaultFlurlClientFactory && _handlerConfigs.Any()) throw new FlurlConfigurationException("ConfigureInnerHandler and UseSocketsHttpHandler cannot be used together. The former configures and instance of HttpClientHandler and would be ignored when switching to SocketsHttpHandler."); - if (!(_factory is SocketsHandlerFlurlClientFactory)) + if (_factory is not SocketsHandlerFlurlClientFactory) _factory = new SocketsHandlerFlurlClientFactory(); _handlerConfigs.Add(h => configure(h as SocketsHttpHandler)); diff --git a/src/Flurl.Http/Configuration/ISerializer.cs b/src/Flurl.Http/Configuration/ISerializer.cs index 71b2a87b..6bbadd59 100644 --- a/src/Flurl.Http/Configuration/ISerializer.cs +++ b/src/Flurl.Http/Configuration/ISerializer.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; +using System.IO; namespace Flurl.Http.Configuration { diff --git a/src/Flurl.Http/Configuration/RedirectSettings.cs b/src/Flurl.Http/Configuration/RedirectSettings.cs index f39d6e8d..6f9f2796 100644 --- a/src/Flurl.Http/Configuration/RedirectSettings.cs +++ b/src/Flurl.Http/Configuration/RedirectSettings.cs @@ -1,11 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.Http; -using System.Text; -using System.Threading.Tasks; - -namespace Flurl.Http.Configuration +namespace Flurl.Http.Configuration { /// /// A set of properties that affect Flurl.Http behavior specific to auto-redirecting. diff --git a/src/Flurl.Http/Content/CapturedJsonContent.cs b/src/Flurl.Http/Content/CapturedJsonContent.cs index 7b2ccd6b..2dab193d 100644 --- a/src/Flurl.Http/Content/CapturedJsonContent.cs +++ b/src/Flurl.Http/Content/CapturedJsonContent.cs @@ -1,6 +1,4 @@ -using System.Text; - -namespace Flurl.Http.Content +namespace Flurl.Http.Content { /// /// Provides HTTP content based on a serialized JSON object, with the JSON string captured to a property diff --git a/src/Flurl.Http/Content/CapturedStringContent.cs b/src/Flurl.Http/Content/CapturedStringContent.cs index 7e26f40e..b303375c 100644 --- a/src/Flurl.Http/Content/CapturedStringContent.cs +++ b/src/Flurl.Http/Content/CapturedStringContent.cs @@ -1,6 +1,4 @@ using System.Net.Http; -using System.Net.Http.Headers; -using System.Text; namespace Flurl.Http.Content { diff --git a/src/Flurl.Http/FlurlClient.cs b/src/Flurl.Http/FlurlClient.cs index 474d8568..b80514c8 100644 --- a/src/Flurl.Http/FlurlClient.cs +++ b/src/Flurl.Http/FlurlClient.cs @@ -253,7 +253,7 @@ private async Task ProcessRedirectAsync(FlurlCall call, HttpComp // partially lifted from https://github.com/dotnet/runtime/blob/master/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/RedirectHandler.cs private static FlurlRedirect GetRedirect(FlurlCall call) { - if (call.Response.StatusCode < 300 || call.Response.StatusCode > 399) + if (call.Response.StatusCode is < 300 or > 399) return null; if (!call.Response.Headers.TryGetFirst("Location", out var location)) diff --git a/src/Flurl.Http/FlurlEventHandler.cs b/src/Flurl.Http/FlurlEventHandler.cs index d9137ea8..4be114e6 100644 --- a/src/Flurl.Http/FlurlEventHandler.cs +++ b/src/Flurl.Http/FlurlEventHandler.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; using System.Threading.Tasks; namespace Flurl.Http diff --git a/src/Flurl.Http/FlurlRequest.cs b/src/Flurl.Http/FlurlRequest.cs index c98e6649..5ee2efac 100644 --- a/src/Flurl.Http/FlurlRequest.cs +++ b/src/Flurl.Http/FlurlRequest.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; @@ -184,7 +183,7 @@ private void ApplyCookieJar(CookieJar jar) { this.WithCookies( from c in CookieJar - where c.ShouldSendTo(this.Url, out _) + where c.ShouldSendTo(Url, out _) // sort by longest path, then earliest creation time, per #2: https://tools.ietf.org/html/rfc6265#section-5.4 orderby (c.Path ?? c.OriginUrl.Path).Length descending, c.DateReceived select (c.Name, c.Value)); diff --git a/src/Flurl.Http/GeneratedExtensions.cs b/src/Flurl.Http/GeneratedExtensions.cs index 75b39ab3..b6ed706b 100644 --- a/src/Flurl.Http/GeneratedExtensions.cs +++ b/src/Flurl.Http/GeneratedExtensions.cs @@ -1,7 +1,6 @@ // This file was auto-generated by Flurl.CodeGen. Do not edit directly. using System; using System.IO; -using System.Net; using System.Net.Http; using System.Threading; using System.Threading.Tasks; diff --git a/src/Flurl.Http/HttpMessageExtensions.cs b/src/Flurl.Http/HttpMessageExtensions.cs index 0c7669dc..7b3d5dee 100644 --- a/src/Flurl.Http/HttpMessageExtensions.cs +++ b/src/Flurl.Http/HttpMessageExtensions.cs @@ -1,5 +1,4 @@ -using System; -using System.Net.Http; +using System.Net.Http; using System.Net.Http.Headers; using Flurl.Http.Content; using Flurl.Util; diff --git a/src/Flurl.Http/IHeadersContainer.cs b/src/Flurl.Http/IHeadersContainer.cs index 366f1b3f..9455e8c2 100644 --- a/src/Flurl.Http/IHeadersContainer.cs +++ b/src/Flurl.Http/IHeadersContainer.cs @@ -48,7 +48,7 @@ public static T WithHeaders(this T obj, object headers, bool replaceUnderscor return obj; // underscore replacement only applies when object properties are parsed to kv pairs - replaceUnderscoreWithHyphen = replaceUnderscoreWithHyphen && !(headers is string) && !(headers is IEnumerable); + replaceUnderscoreWithHyphen = replaceUnderscoreWithHyphen && headers is not string && headers is not IEnumerable; foreach (var kv in headers.ToKeyValuePairs()) { var key = replaceUnderscoreWithHyphen ? kv.Key.Replace("_", "-") : kv.Key; diff --git a/src/Flurl.Http/ISettingsContainer.cs b/src/Flurl.Http/ISettingsContainer.cs index 698fd87f..a8364ea2 100644 --- a/src/Flurl.Http/ISettingsContainer.cs +++ b/src/Flurl.Http/ISettingsContainer.cs @@ -1,6 +1,4 @@ using System; -using System.Linq; -using System.Net; using Flurl.Http.Configuration; namespace Flurl.Http diff --git a/src/Flurl.Http/Testing/Util.cs b/src/Flurl.Http/Testing/Util.cs index 12c2af83..6ff59004 100644 --- a/src/Flurl.Http/Testing/Util.cs +++ b/src/Flurl.Http/Testing/Util.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections; -using System.Collections.Generic; +using System.Collections; using System.Linq; using System.Net.Http; using System.Text.RegularExpressions; @@ -39,7 +37,7 @@ internal static bool HasQueryParam(this FlurlCall call, string name, object valu if (!paramVals.Any()) return false; - if (!(value is string) && value is IEnumerable en) { + if (value is not string && value is IEnumerable en) { var values = en.Cast().Select(o => o.ToInvariantString()).ToList(); return values.Intersect(paramVals).Count() == values.Count; } diff --git a/src/Flurl/NullValueHandling.cs b/src/Flurl/NullValueHandling.cs index 7180ab9c..30d8cadf 100644 --- a/src/Flurl/NullValueHandling.cs +++ b/src/Flurl/NullValueHandling.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Flurl +namespace Flurl { /// /// Describes how to handle null values in query parameters. diff --git a/src/Flurl/Url.cs b/src/Flurl/Url.cs index 8a713a84..39414b5e 100644 --- a/src/Flurl/Url.cs +++ b/src/Flurl/Url.cs @@ -633,12 +633,12 @@ public string ToString(bool encodeSpaceAsPlus) { /// /// The object to compare to this instance. /// - public override bool Equals(object obj) => obj is Url url && this.ToString().OrdinalEquals(url.ToString()); + public override bool Equals(object obj) => obj is Url url && ToString().OrdinalEquals(url.ToString()); /// /// Returns the hashcode for this Url. /// - public override int GetHashCode() => this.ToString().GetHashCode(); + public override int GetHashCode() => ToString().GetHashCode(); #endregion #region static utility methods diff --git a/src/Flurl/Util/NameValueList.cs b/src/Flurl/Util/NameValueList.cs index 52aaceff..1354c10b 100644 --- a/src/Flurl/Util/NameValueList.cs +++ b/src/Flurl/Util/NameValueList.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; namespace Flurl.Util @@ -96,11 +95,11 @@ public NameValueList(IEnumerable<(string Name, TValue Value)> items, bool caseSe public void AddOrReplace(string name, TValue value) { var i = 0; var replaced = false; - while (i < this.Count) { + while (i < Count) { if (!this[i].Name.OrdinalEquals(name, !_caseSensitiveNames)) i++; else if (replaced) - this.RemoveAt(i); + RemoveAt(i); else { this[i] = (name, value); replaced = true; @@ -109,7 +108,7 @@ public void AddOrReplace(string name, TValue value) { } if (!replaced) - this.Add(name, value); + Add(name, value); } ///