diff --git a/stage/dotnet/dotnet-sdk-enterprise-cloud/src/Client/ClientFactory.cs b/stage/dotnet/dotnet-sdk-enterprise-cloud/src/Client/ClientFactory.cs index 9ce76e6..190276f 100644 --- a/stage/dotnet/dotnet-sdk-enterprise-cloud/src/Client/ClientFactory.cs +++ b/stage/dotnet/dotnet-sdk-enterprise-cloud/src/Client/ClientFactory.cs @@ -17,13 +17,8 @@ public class ClientFactory private IAuthenticationProvider? _authenticationProvider; private readonly HttpMessageHandler? _finalHandler; - private static readonly Lazy> s_handlers = - new(() => - [ - new APIVersionHandler(), - new UserAgentHandler(), - new RateLimitHandler(), - ]); + private readonly Lazy> _handlers = + new(() => [.. CreateDefaultHandlers()]); public ClientFactory(HttpMessageHandler? finalHandler = null) { @@ -77,8 +72,7 @@ public HttpClientRequestAdapter Build() if (_authenticationProvider == null) throw new ArgumentNullException("authenticationProvider"); var httpClient = new HttpClient(); - var defaultHandlers = CreateDefaultHandlers(); - var handler = ChainHandlersCollectionAndGetFirstLink(finalHandler: _finalHandler ?? GetDefaultHttpMessageHandler(), handlers: [.. defaultHandlers]); + var handler = ChainHandlersCollectionAndGetFirstLink(finalHandler: _finalHandler ?? GetDefaultHttpMessageHandler(), handlers: [.. _handlers.Value]); if (handler != null) { @@ -95,18 +89,24 @@ public HttpClientRequestAdapter Build() httpClient.BaseAddress = new Uri(_baseUrl); } - return RequestAdapter.Create(_authenticationProvider, httpClient); ; + return RequestAdapter.Create(_authenticationProvider, httpClient); } + /// /// Creates a list of default delegating handlers for the Octokit client. /// /// A list of default delegating handlers. public static IList CreateDefaultHandlers() { - var defaultHandlers = s_handlers.Value; + var defaultHandlers = new DelegatingHandler[] + { + new APIVersionHandler(), + new UserAgentHandler(), + new RateLimitHandler(), + }; var kiotaDefaultHandlers = KiotaClientFactory.CreateDefaultHandlers(); - return kiotaDefaultHandlers.Concat(defaultHandlers).ToList(); + return [.. kiotaDefaultHandlers, .. defaultHandlers]; } /// @@ -169,16 +169,16 @@ public static HttpMessageHandler GetDefaultHttpMessageHandler(IWebProxy? proxy = private void AddOrCreateHandler(THandler handler) where THandler : DelegatingHandler { // Find the index of the handler that matches the specified type - int index = s_handlers.Value.FindIndex(h => h is THandler); + int index = _handlers.Value.FindIndex(h => h is THandler); // If the handler is found, replace it with the new handler otehrwise add the new handler to the list if (index >= 0) { - s_handlers.Value[index] = handler; + _handlers.Value[index] = handler; } else { - s_handlers.Value.Add(handler); + _handlers.Value.Add(handler); } } } diff --git a/stage/dotnet/dotnet-sdk-enterprise-cloud/test/Client/ClientFactoryTest.cs b/stage/dotnet/dotnet-sdk-enterprise-cloud/test/Client/ClientFactoryTest.cs index 7a2bfc7..a9dfe29 100644 --- a/stage/dotnet/dotnet-sdk-enterprise-cloud/test/Client/ClientFactoryTest.cs +++ b/stage/dotnet/dotnet-sdk-enterprise-cloud/test/Client/ClientFactoryTest.cs @@ -45,6 +45,15 @@ public void CreateDefaultHandlers_Returns_Expected_Handlers() Assert.Contains(handlers, h => h is Microsoft.Kiota.Http.HttpClientLibrary.Middleware.RedirectHandler); } + [Fact] + public void CreateDefaultHandlers_Returns_Unique_Instances() + { + var handlers1 = new HashSet(ClientFactory.CreateDefaultHandlers()); + var handlers2 = new HashSet(ClientFactory.CreateDefaultHandlers()); + + Assert.False(handlers1.Overlaps(handlers2)); + } + [Fact] public void ChainHandlersCollectionAndGetFirstLink_ChainsHandlersCorrectly() { diff --git a/stage/dotnet/dotnet-sdk-enterprise-server/src/Client/ClientFactory.cs b/stage/dotnet/dotnet-sdk-enterprise-server/src/Client/ClientFactory.cs index 9ce76e6..190276f 100644 --- a/stage/dotnet/dotnet-sdk-enterprise-server/src/Client/ClientFactory.cs +++ b/stage/dotnet/dotnet-sdk-enterprise-server/src/Client/ClientFactory.cs @@ -17,13 +17,8 @@ public class ClientFactory private IAuthenticationProvider? _authenticationProvider; private readonly HttpMessageHandler? _finalHandler; - private static readonly Lazy> s_handlers = - new(() => - [ - new APIVersionHandler(), - new UserAgentHandler(), - new RateLimitHandler(), - ]); + private readonly Lazy> _handlers = + new(() => [.. CreateDefaultHandlers()]); public ClientFactory(HttpMessageHandler? finalHandler = null) { @@ -77,8 +72,7 @@ public HttpClientRequestAdapter Build() if (_authenticationProvider == null) throw new ArgumentNullException("authenticationProvider"); var httpClient = new HttpClient(); - var defaultHandlers = CreateDefaultHandlers(); - var handler = ChainHandlersCollectionAndGetFirstLink(finalHandler: _finalHandler ?? GetDefaultHttpMessageHandler(), handlers: [.. defaultHandlers]); + var handler = ChainHandlersCollectionAndGetFirstLink(finalHandler: _finalHandler ?? GetDefaultHttpMessageHandler(), handlers: [.. _handlers.Value]); if (handler != null) { @@ -95,18 +89,24 @@ public HttpClientRequestAdapter Build() httpClient.BaseAddress = new Uri(_baseUrl); } - return RequestAdapter.Create(_authenticationProvider, httpClient); ; + return RequestAdapter.Create(_authenticationProvider, httpClient); } + /// /// Creates a list of default delegating handlers for the Octokit client. /// /// A list of default delegating handlers. public static IList CreateDefaultHandlers() { - var defaultHandlers = s_handlers.Value; + var defaultHandlers = new DelegatingHandler[] + { + new APIVersionHandler(), + new UserAgentHandler(), + new RateLimitHandler(), + }; var kiotaDefaultHandlers = KiotaClientFactory.CreateDefaultHandlers(); - return kiotaDefaultHandlers.Concat(defaultHandlers).ToList(); + return [.. kiotaDefaultHandlers, .. defaultHandlers]; } /// @@ -169,16 +169,16 @@ public static HttpMessageHandler GetDefaultHttpMessageHandler(IWebProxy? proxy = private void AddOrCreateHandler(THandler handler) where THandler : DelegatingHandler { // Find the index of the handler that matches the specified type - int index = s_handlers.Value.FindIndex(h => h is THandler); + int index = _handlers.Value.FindIndex(h => h is THandler); // If the handler is found, replace it with the new handler otehrwise add the new handler to the list if (index >= 0) { - s_handlers.Value[index] = handler; + _handlers.Value[index] = handler; } else { - s_handlers.Value.Add(handler); + _handlers.Value.Add(handler); } } } diff --git a/stage/dotnet/dotnet-sdk-enterprise-server/test/Client/ClientFactoryTest.cs b/stage/dotnet/dotnet-sdk-enterprise-server/test/Client/ClientFactoryTest.cs index 7a2bfc7..a9dfe29 100644 --- a/stage/dotnet/dotnet-sdk-enterprise-server/test/Client/ClientFactoryTest.cs +++ b/stage/dotnet/dotnet-sdk-enterprise-server/test/Client/ClientFactoryTest.cs @@ -45,6 +45,15 @@ public void CreateDefaultHandlers_Returns_Expected_Handlers() Assert.Contains(handlers, h => h is Microsoft.Kiota.Http.HttpClientLibrary.Middleware.RedirectHandler); } + [Fact] + public void CreateDefaultHandlers_Returns_Unique_Instances() + { + var handlers1 = new HashSet(ClientFactory.CreateDefaultHandlers()); + var handlers2 = new HashSet(ClientFactory.CreateDefaultHandlers()); + + Assert.False(handlers1.Overlaps(handlers2)); + } + [Fact] public void ChainHandlersCollectionAndGetFirstLink_ChainsHandlersCorrectly() { diff --git a/stage/dotnet/dotnet-sdk/src/Client/ClientFactory.cs b/stage/dotnet/dotnet-sdk/src/Client/ClientFactory.cs index 9ce76e6..190276f 100644 --- a/stage/dotnet/dotnet-sdk/src/Client/ClientFactory.cs +++ b/stage/dotnet/dotnet-sdk/src/Client/ClientFactory.cs @@ -17,13 +17,8 @@ public class ClientFactory private IAuthenticationProvider? _authenticationProvider; private readonly HttpMessageHandler? _finalHandler; - private static readonly Lazy> s_handlers = - new(() => - [ - new APIVersionHandler(), - new UserAgentHandler(), - new RateLimitHandler(), - ]); + private readonly Lazy> _handlers = + new(() => [.. CreateDefaultHandlers()]); public ClientFactory(HttpMessageHandler? finalHandler = null) { @@ -77,8 +72,7 @@ public HttpClientRequestAdapter Build() if (_authenticationProvider == null) throw new ArgumentNullException("authenticationProvider"); var httpClient = new HttpClient(); - var defaultHandlers = CreateDefaultHandlers(); - var handler = ChainHandlersCollectionAndGetFirstLink(finalHandler: _finalHandler ?? GetDefaultHttpMessageHandler(), handlers: [.. defaultHandlers]); + var handler = ChainHandlersCollectionAndGetFirstLink(finalHandler: _finalHandler ?? GetDefaultHttpMessageHandler(), handlers: [.. _handlers.Value]); if (handler != null) { @@ -95,18 +89,24 @@ public HttpClientRequestAdapter Build() httpClient.BaseAddress = new Uri(_baseUrl); } - return RequestAdapter.Create(_authenticationProvider, httpClient); ; + return RequestAdapter.Create(_authenticationProvider, httpClient); } + /// /// Creates a list of default delegating handlers for the Octokit client. /// /// A list of default delegating handlers. public static IList CreateDefaultHandlers() { - var defaultHandlers = s_handlers.Value; + var defaultHandlers = new DelegatingHandler[] + { + new APIVersionHandler(), + new UserAgentHandler(), + new RateLimitHandler(), + }; var kiotaDefaultHandlers = KiotaClientFactory.CreateDefaultHandlers(); - return kiotaDefaultHandlers.Concat(defaultHandlers).ToList(); + return [.. kiotaDefaultHandlers, .. defaultHandlers]; } /// @@ -169,16 +169,16 @@ public static HttpMessageHandler GetDefaultHttpMessageHandler(IWebProxy? proxy = private void AddOrCreateHandler(THandler handler) where THandler : DelegatingHandler { // Find the index of the handler that matches the specified type - int index = s_handlers.Value.FindIndex(h => h is THandler); + int index = _handlers.Value.FindIndex(h => h is THandler); // If the handler is found, replace it with the new handler otehrwise add the new handler to the list if (index >= 0) { - s_handlers.Value[index] = handler; + _handlers.Value[index] = handler; } else { - s_handlers.Value.Add(handler); + _handlers.Value.Add(handler); } } } diff --git a/stage/dotnet/dotnet-sdk/test/Client/ClientFactoryTest.cs b/stage/dotnet/dotnet-sdk/test/Client/ClientFactoryTest.cs index 7a2bfc7..a9dfe29 100644 --- a/stage/dotnet/dotnet-sdk/test/Client/ClientFactoryTest.cs +++ b/stage/dotnet/dotnet-sdk/test/Client/ClientFactoryTest.cs @@ -45,6 +45,15 @@ public void CreateDefaultHandlers_Returns_Expected_Handlers() Assert.Contains(handlers, h => h is Microsoft.Kiota.Http.HttpClientLibrary.Middleware.RedirectHandler); } + [Fact] + public void CreateDefaultHandlers_Returns_Unique_Instances() + { + var handlers1 = new HashSet(ClientFactory.CreateDefaultHandlers()); + var handlers2 = new HashSet(ClientFactory.CreateDefaultHandlers()); + + Assert.False(handlers1.Overlaps(handlers2)); + } + [Fact] public void ChainHandlersCollectionAndGetFirstLink_ChainsHandlersCorrectly() {