Skip to content
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
17 changes: 16 additions & 1 deletion src/Microsoft.Graph.Core/Requests/GraphClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Microsoft.Graph
using System.Threading;
using Azure.Core;
using Microsoft.Graph.Authentication;
using Microsoft.Kiota.Abstractions;
using Microsoft.Kiota.Abstractions.Authentication;
using Microsoft.Kiota.Http.HttpClientLibrary;
using Microsoft.Kiota.Http.HttpClientLibrary.Middleware;
Expand Down Expand Up @@ -170,7 +171,21 @@ public static HttpClient Create(
/// <returns></returns>
public static IList<DelegatingHandler> CreateDefaultHandlers(GraphClientOptions graphClientOptions = null)
{
var handlers = KiotaClientFactory.CreateDefaultHandlers();
return CreateDefaultHandlers(graphClientOptions, null);
}

/// <summary>
/// Create a default set of middleware for calling Microsoft Graph, letting callers configure the
/// underlying Kiota handlers (for example a custom <see cref="Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options.RetryHandlerOption"/>)
/// without having to remove and reinsert a handler afterwards.
/// </summary>
/// <param name="graphClientOptions">The <see cref="GraphClientOptions"/> to use with the client</param>
/// <param name="optionsForHandlers">The request options to configure the default Kiota handlers with. See
/// <see cref="KiotaClientFactory.CreateDefaultHandlers(IRequestOption[])"/> for the handlers that read these options.</param>
/// <returns></returns>
public static IList<DelegatingHandler> CreateDefaultHandlers(GraphClientOptions graphClientOptions, IRequestOption[] optionsForHandlers)
{
var handlers = KiotaClientFactory.CreateDefaultHandlers(optionsForHandlers);
handlers.Add(new GraphTelemetryHandler(graphClientOptions));// add the telemetry handler last.

return handlers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ namespace Microsoft.Graph.DotnetCore.Core.Test.Requests
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Microsoft.Kiota.Abstractions;
using Microsoft.Kiota.Abstractions.Authentication;
using Microsoft.Kiota.Http.HttpClientLibrary.Middleware;
using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options;
Expand Down Expand Up @@ -148,6 +150,30 @@ public void CreatePipeline_Should_Throw_Exception_With_Duplicate_Handlers()
Assert.Contains($"{typeof(GraphTelemetryHandler)} has a duplicate handler.", exception.Message);
}

[Fact]
public void CreateDefaultHandlers_Should_Forward_RetryHandlerOption_To_RetryHandler()
{
var retryOption = new RetryHandlerOption
{
MaxRetry = 7,
Delay = 3
};

var defaultHandlers = GraphClientFactory.CreateDefaultHandlers(new GraphClientOptions(), new IRequestOption[] { retryOption });

var retryHandler = defaultHandlers.OfType<RetryHandler>().Single();

// RetryHandler.RetryOption is internal to the Kiota library, so we reach it with reflection
// to prove the option we passed in is the one the handler ended up with.
var retryOptionProperty = typeof(RetryHandler).GetProperty("RetryOption", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.NotNull(retryOptionProperty);
var actualRetryOption = (RetryHandlerOption)retryOptionProperty.GetValue(retryHandler);

Assert.Same(retryOption, actualRetryOption);
Assert.Equal(7, actualRetryOption.MaxRetry);
Assert.Equal(3, actualRetryOption.Delay);
}

[Fact]
public void CreateClient_CustomHttpHandlingBehaviors()
{
Expand Down