Skip to content

Commit

Permalink
feat: Add create() overload in KiotaClientFactory to add authenticati…
Browse files Browse the repository at this point in the history
…on middleware to the chain
  • Loading branch information
Ndiritu committed Oct 10, 2024
1 parent cdb8ffa commit 773fbbe
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/http/httpClient/ContinuousAccessEvaluation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ namespace Microsoft.Kiota.Http.HttpClientLibrary
/// <summary>
/// Process continuous access evaluation
/// </summary>
internal class ContinuousAccessEvaluation
static internal class ContinuousAccessEvaluation
{

internal const string ClaimsKey = "claims";
internal const string BearerAuthenticationScheme = "Bearer";
private static readonly char[] ComaSplitSeparator = [','];
Expand Down
24 changes: 24 additions & 0 deletions src/http/httpClient/KiotaClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@ public static HttpClient Create(IList<DelegatingHandler> handlers, HttpMessageHa
return handler != null ? new HttpClient(handler) : new HttpClient();
}

/// <summary>
/// Initializes the <see cref="HttpClient"/> with the default configuration and authentication middleware using the <see cref="IAuthenticationProvider"/> if provided.
/// </summary>
/// <param name="authenticationProvider"></param>
/// <param name="optionsForHandlers"></param>
/// <param name="finalHandler"></param>
/// <returns></returns>
public static HttpClient Create(BaseBearerTokenAuthenticationProvider authenticationProvider, IRequestOption[]? optionsForHandlers = null, HttpMessageHandler? finalHandler = null)
{
var defaultHandlersEnumerable = CreateDefaultHandlers(optionsForHandlers);
defaultHandlersEnumerable.Add(new AuthorizationHandler(authenticationProvider));
int count = 0;
foreach(var _ in defaultHandlersEnumerable) count++;

var defaultHandlersArray = new DelegatingHandler[count];
int index = 0;
foreach(var handler2 in defaultHandlersEnumerable)
{
defaultHandlersArray[index++] = handler2;
}
var handler = ChainHandlersCollectionAndGetFirstLink(finalHandler ?? GetDefaultHttpMessageHandler(), defaultHandlersArray);
return handler != null ? new HttpClient(handler) : new HttpClient();
}

/// <summary>
/// Creates a default set of middleware to be used by the <see cref="HttpClient"/>.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/http/httpClient/Middleware/AuthorizationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class AuthorizationHandler : DelegatingHandler
{

private const string AuthorizationHeader = "Authorization";
private BaseBearerTokenAuthenticationProvider authenticationProvider;
private readonly BaseBearerTokenAuthenticationProvider authenticationProvider;

/// <summary>
/// Constructs an <see cref="AuthorizationHandler"/>
Expand Down

0 comments on commit 773fbbe

Please sign in to comment.