Skip to content

Commit

Permalink
feat: Add create() overloads to GraphClientFactory that enable reques…
Browse files Browse the repository at this point in the history
…ts to be authenticated
  • Loading branch information
Ndiritu committed Oct 30, 2024
1 parent 1506164 commit 22f745e
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/Microsoft.Graph.Core/Requests/GraphClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ namespace Microsoft.Graph
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Azure.Core;
using Microsoft.Graph.Authentication;
using Microsoft.Kiota.Abstractions.Authentication;
using Microsoft.Kiota.Http.HttpClientLibrary;
using Microsoft.Kiota.Http.HttpClientLibrary.Middleware;

Expand Down Expand Up @@ -106,6 +109,64 @@ public static HttpClient Create(
return client;
}

/// <summary>
/// Creates a new <see cref="HttpClient"/> instance configured to authenticate requests using the provided <see cref="BaseBearerTokenAuthenticationProvider"/>.
/// </summary>
/// <param name="authenticationProvider">The authentication provider to initialise the Authorization handler</param>
/// <param name="handlers">Custom middleware pipeline to which the Authorization handler is appended. If null, default handlers are initialised</param>
/// <param name="version">The Graph version to use in the base URL</param>
/// <param name="nationalCloud">The national cloud endpoint to use</param>
/// <param name="proxy">The proxy to be used with the created client</param>
/// <param name="finalHandler">The last HttpMessageHandler to HTTP calls.</param>
/// <param name="disposeHandler">true if the inner handler should be disposed of by Dispose(), false if you intend to reuse the inner handler..</param>
/// <returns></returns>
public static HttpClient Create(
BaseBearerTokenAuthenticationProvider authenticationProvider,
IEnumerable<DelegatingHandler> handlers = null,
string version = "v1.0",
string nationalCloud = Global_Cloud,
IWebProxy proxy = null,
HttpMessageHandler finalHandler = null,
bool disposeHandler = true)
{
if (handlers == null)
{
handlers = CreateDefaultHandlers();
}
handlers = handlers.Append(new AuthorizationHandler(authenticationProvider));
return Create(handlers, version, nationalCloud, proxy, finalHandler, disposeHandler);
}

/// <summary>
/// Creates a new <see cref="HttpClient"/> instance configured to authenticate requests using the provided <see cref="TokenCredential"/>.
/// </summary>
/// <param name="tokenCredential">Token credential object use to initialise an <see cref="AzureIdentityAuthenticationProvider"></param>
/// <param name="handlers">Custom middleware pipeline to which the Authorization handler is appended. If null, default handlers are initialised</param>
/// <param name="version">The Graph version to use in the base URL</param>
/// <param name="nationalCloud">The national cloud endpoint to use</param>
/// <param name="proxy">The proxy to be used with the created client</param>
/// <param name="finalHandler">The last HttpMessageHandler to HTTP calls</param>
/// <param name="disposeHandler">true if the inner handler should be disposed of by Dispose(), false if you intend to reuse the inner handler..</param>
/// <returns></returns>
public static HttpClient Create(
TokenCredential tokenCredential,
IEnumerable<DelegatingHandler> handlers = null,
string version = "v1.0",
string nationalCloud = Global_Cloud,
IWebProxy proxy = null,
HttpMessageHandler finalHandler = null,
bool disposeHandler = true)
{
if (handlers == null)
{
handlers = CreateDefaultHandlers();
}
handlers = handlers.Append(new AuthorizationHandler(
new AzureIdentityAuthenticationProvider(tokenCredential, null, null, true)
));
return Create(handlers, version, nationalCloud, proxy, finalHandler, disposeHandler);
}

/// <summary>
/// Create a default set of middleware for calling Microsoft Graph
/// </summary>
Expand Down

0 comments on commit 22f745e

Please sign in to comment.