Skip to content

Commit ce6a88b

Browse files
committed
feat: Add create() overloads to GraphClientFactory that enable requests to be authenticated
1 parent 6c49f09 commit ce6a88b

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/Microsoft.Graph.Core/Requests/GraphClientFactory.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ namespace Microsoft.Graph
99
using System.Net;
1010
using System.Net.Http;
1111
using System.Net.Http.Headers;
12+
using Azure.Core;
13+
using Microsoft.Graph.Authentication;
14+
using Microsoft.Kiota.Abstractions.Authentication;
1215
using Microsoft.Kiota.Http.HttpClientLibrary;
1316
using Microsoft.Kiota.Http.HttpClientLibrary.Middleware;
1417

@@ -106,6 +109,64 @@ public static HttpClient Create(
106109
return client;
107110
}
108111

112+
/// <summary>
113+
/// Creates a new <see cref="HttpClient"/> instance configured to authenticate requests using the provided <see cref="BaseBearerTokenAuthenticationProvider"/>.
114+
/// </summary>
115+
/// <param name="authenticationProvider">The authentication provider to initialise the Authorization handler</param>
116+
/// <param name="handlers">Custom middleware pipeline to which the Authorization handler is appended. If null, default handlers are initialised</param>
117+
/// <param name="version">The Graph version to use in the base URL</param>
118+
/// <param name="nationalCloud">The national cloud endpoint to use</param>
119+
/// <param name="proxy">The proxy to be used with the created client</param>
120+
/// <param name="finalHandler">The last HttpMessageHandler to HTTP calls.</param>
121+
/// <param name="disposeHandler">true if the inner handler should be disposed of by Dispose(), false if you intend to reuse the inner handler..</param>
122+
/// <returns></returns>
123+
public static HttpClient Create(
124+
BaseBearerTokenAuthenticationProvider authenticationProvider,
125+
IEnumerable<DelegatingHandler> handlers = null,
126+
string version = "v1.0",
127+
string nationalCloud = Global_Cloud,
128+
IWebProxy proxy = null,
129+
HttpMessageHandler finalHandler = null,
130+
bool disposeHandler = true)
131+
{
132+
if (handlers == null)
133+
{
134+
handlers = CreateDefaultHandlers();
135+
}
136+
handlers = handlers.Append(new AuthorizationHandler(authenticationProvider));
137+
return Create(handlers, version, nationalCloud, proxy, finalHandler, disposeHandler);
138+
}
139+
140+
/// <summary>
141+
/// Creates a new <see cref="HttpClient"/> instance configured to authenticate requests using the provided <see cref="TokenCredential"/>.
142+
/// </summary>
143+
/// <param name="tokenCredential">Token credential object use to initialise an <see cref="AzureIdentityAuthenticationProvider"></param>
144+
/// <param name="handlers">Custom middleware pipeline to which the Authorization handler is appended. If null, default handlers are initialised</param>
145+
/// <param name="version">The Graph version to use in the base URL</param>
146+
/// <param name="nationalCloud">The national cloud endpoint to use</param>
147+
/// <param name="proxy">The proxy to be used with the created client</param>
148+
/// <param name="finalHandler">The last HttpMessageHandler to HTTP calls</param>
149+
/// <param name="disposeHandler">true if the inner handler should be disposed of by Dispose(), false if you intend to reuse the inner handler..</param>
150+
/// <returns></returns>
151+
public static HttpClient Create(
152+
TokenCredential tokenCredential,
153+
IEnumerable<DelegatingHandler> handlers = null,
154+
string version = "v1.0",
155+
string nationalCloud = Global_Cloud,
156+
IWebProxy proxy = null,
157+
HttpMessageHandler finalHandler = null,
158+
bool disposeHandler = true)
159+
{
160+
if (handlers == null)
161+
{
162+
handlers = CreateDefaultHandlers();
163+
}
164+
handlers = handlers.Append(new AuthorizationHandler(
165+
new AzureIdentityAuthenticationProvider(tokenCredential, null, null, true)
166+
));
167+
return Create(handlers, version, nationalCloud, proxy, finalHandler, disposeHandler);
168+
}
169+
109170
/// <summary>
110171
/// Create a default set of middleware for calling Microsoft Graph
111172
/// </summary>

0 commit comments

Comments
 (0)