Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.NET - always return unique instances of DelegatingHandler #146

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@ public class ClientFactory

private IAuthenticationProvider? _authenticationProvider;
private readonly HttpMessageHandler? _finalHandler;
private static readonly Lazy<List<DelegatingHandler>> s_handlers =
new(() =>
[
new APIVersionHandler(),
new UserAgentHandler(),
new RateLimitHandler(),
]);
private readonly Lazy<List<DelegatingHandler>> _handlers =
new(() => [.. CreateDefaultHandlers()]);

public ClientFactory(HttpMessageHandler? finalHandler = null)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -95,18 +89,24 @@ public HttpClientRequestAdapter Build()
httpClient.BaseAddress = new Uri(_baseUrl);
}

return RequestAdapter.Create(_authenticationProvider, httpClient); ;
return RequestAdapter.Create(_authenticationProvider, httpClient);
}

/// <summary>
/// Creates a list of default delegating handlers for the Octokit client.
/// </summary>
/// <returns>A list of default delegating handlers.</returns>
public static IList<DelegatingHandler> 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];
}

/// <summary>
Expand Down Expand Up @@ -169,16 +169,16 @@ public static HttpMessageHandler GetDefaultHttpMessageHandler(IWebProxy? proxy =
private void AddOrCreateHandler<THandler>(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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<DelegatingHandler>(ClientFactory.CreateDefaultHandlers());
var handlers2 = new HashSet<DelegatingHandler>(ClientFactory.CreateDefaultHandlers());

Assert.False(handlers1.Overlaps(handlers2));
}

[Fact]
public void ChainHandlersCollectionAndGetFirstLink_ChainsHandlersCorrectly()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@ public class ClientFactory

private IAuthenticationProvider? _authenticationProvider;
private readonly HttpMessageHandler? _finalHandler;
private static readonly Lazy<List<DelegatingHandler>> s_handlers =
new(() =>
[
new APIVersionHandler(),
new UserAgentHandler(),
new RateLimitHandler(),
]);
private readonly Lazy<List<DelegatingHandler>> _handlers =
new(() => [.. CreateDefaultHandlers()]);

public ClientFactory(HttpMessageHandler? finalHandler = null)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -95,18 +89,24 @@ public HttpClientRequestAdapter Build()
httpClient.BaseAddress = new Uri(_baseUrl);
}

return RequestAdapter.Create(_authenticationProvider, httpClient); ;
return RequestAdapter.Create(_authenticationProvider, httpClient);
}

/// <summary>
/// Creates a list of default delegating handlers for the Octokit client.
/// </summary>
/// <returns>A list of default delegating handlers.</returns>
public static IList<DelegatingHandler> 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];
}

/// <summary>
Expand Down Expand Up @@ -169,16 +169,16 @@ public static HttpMessageHandler GetDefaultHttpMessageHandler(IWebProxy? proxy =
private void AddOrCreateHandler<THandler>(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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<DelegatingHandler>(ClientFactory.CreateDefaultHandlers());
var handlers2 = new HashSet<DelegatingHandler>(ClientFactory.CreateDefaultHandlers());

Assert.False(handlers1.Overlaps(handlers2));
}

[Fact]
public void ChainHandlersCollectionAndGetFirstLink_ChainsHandlersCorrectly()
{
Expand Down
30 changes: 15 additions & 15 deletions stage/dotnet/dotnet-sdk/src/Client/ClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@ public class ClientFactory

private IAuthenticationProvider? _authenticationProvider;
private readonly HttpMessageHandler? _finalHandler;
private static readonly Lazy<List<DelegatingHandler>> s_handlers =
new(() =>
[
new APIVersionHandler(),
new UserAgentHandler(),
new RateLimitHandler(),
]);
private readonly Lazy<List<DelegatingHandler>> _handlers =
new(() => [.. CreateDefaultHandlers()]);

public ClientFactory(HttpMessageHandler? finalHandler = null)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -95,18 +89,24 @@ public HttpClientRequestAdapter Build()
httpClient.BaseAddress = new Uri(_baseUrl);
}

return RequestAdapter.Create(_authenticationProvider, httpClient); ;
return RequestAdapter.Create(_authenticationProvider, httpClient);
}

/// <summary>
/// Creates a list of default delegating handlers for the Octokit client.
/// </summary>
/// <returns>A list of default delegating handlers.</returns>
public static IList<DelegatingHandler> 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];
}

/// <summary>
Expand Down Expand Up @@ -169,16 +169,16 @@ public static HttpMessageHandler GetDefaultHttpMessageHandler(IWebProxy? proxy =
private void AddOrCreateHandler<THandler>(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);
}
}
}
9 changes: 9 additions & 0 deletions stage/dotnet/dotnet-sdk/test/Client/ClientFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DelegatingHandler>(ClientFactory.CreateDefaultHandlers());
var handlers2 = new HashSet<DelegatingHandler>(ClientFactory.CreateDefaultHandlers());

Assert.False(handlers1.Overlaps(handlers2));
}

[Fact]
public void ChainHandlersCollectionAndGetFirstLink_ChainsHandlersCorrectly()
{
Expand Down