From 22f745e0473f588451a613009f5ce701c84a5246 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Wed, 30 Oct 2024 12:11:15 +0200 Subject: [PATCH] feat: Add create() overloads to GraphClientFactory that enable requests to be authenticated --- .../Requests/GraphClientFactory.cs | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/Microsoft.Graph.Core/Requests/GraphClientFactory.cs b/src/Microsoft.Graph.Core/Requests/GraphClientFactory.cs index ed21c14d..e7cbb5ae 100644 --- a/src/Microsoft.Graph.Core/Requests/GraphClientFactory.cs +++ b/src/Microsoft.Graph.Core/Requests/GraphClientFactory.cs @@ -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; @@ -106,6 +109,64 @@ public static HttpClient Create( return client; } + /// + /// Creates a new instance configured to authenticate requests using the provided . + /// + /// The authentication provider to initialise the Authorization handler + /// Custom middleware pipeline to which the Authorization handler is appended. If null, default handlers are initialised + /// The Graph version to use in the base URL + /// The national cloud endpoint to use + /// The proxy to be used with the created client + /// The last HttpMessageHandler to HTTP calls. + /// true if the inner handler should be disposed of by Dispose(), false if you intend to reuse the inner handler.. + /// + public static HttpClient Create( + BaseBearerTokenAuthenticationProvider authenticationProvider, + IEnumerable 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); + } + + /// + /// Creates a new instance configured to authenticate requests using the provided . + /// + /// Token credential object use to initialise an + /// Custom middleware pipeline to which the Authorization handler is appended. If null, default handlers are initialised + /// The Graph version to use in the base URL + /// The national cloud endpoint to use + /// The proxy to be used with the created client + /// The last HttpMessageHandler to HTTP calls + /// true if the inner handler should be disposed of by Dispose(), false if you intend to reuse the inner handler.. + /// + public static HttpClient Create( + TokenCredential tokenCredential, + IEnumerable 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); + } + /// /// Create a default set of middleware for calling Microsoft Graph ///