Skip to content

Commit

Permalink
feat: Add trim-safe handler lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
Hawxy committed Feb 5, 2025
1 parent e891593 commit a2800ab
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/http/httpClient/KiotaClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.Http;
using Microsoft.Kiota.Abstractions;
Expand Down Expand Up @@ -126,6 +127,7 @@ public static IList<DelegatingHandler> CreateDefaultHandlers(IRequestOption[]? o
/// </summary>
/// <returns>A list of all the default handlers</returns>
/// <remarks>Order matters</remarks>
[Obsolete("Use GetDefaultHandlerActivatableTypes instead")]
public static IList<System.Type> GetDefaultHandlerTypes()
{
return new List<System.Type>
Expand All @@ -140,6 +142,72 @@ public static IList<DelegatingHandler> CreateDefaultHandlers(IRequestOption[]? o
};
}

/// <summary>
/// Gets the default handler types.
/// </summary>
/// <returns>A list of all the default handlers</returns>
/// <remarks>Order matters</remarks>
public static IList<ActivatableType> GetDefaultHandlerActivatableTypes()
{
return new List<ActivatableType>()
{
new(typeof(UriReplacementHandler<UriReplacementHandlerOption>)),
new(typeof(RetryHandler)),
new(typeof(RedirectHandler)),
new(typeof(ParametersNameDecodingHandler)),
new(typeof(UserAgentHandler)),
new(typeof(HeadersInspectionHandler)),
new(typeof(BodyInspectionHandler)),
};
}

/// <summary>
/// Provides DI-safe trim annotations for an underlying type.
/// Required due to https://github.com/dotnet/runtime/issues/110239
/// </summary>
public readonly struct ActivatableType
{
#if NET5_0_OR_GREATER
/// <summary>
/// Provides DI-safe trim annotations for an underlying type.
/// </summary>
/// <param name="type">The type to be wrapped.</param>
public ActivatableType([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type)
{
Type = type;
}

/// <summary>
/// The underlying type.
/// </summary>
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
public readonly Type Type;

#else
/// <summary>
/// Provides DI-safe trim annotations for an underlying type.
/// </summary>
/// <param name="type">The type to be wrapped.</param>
public ActivatableType(Type type)
{
Type = type;
}

/// <summary>
/// The underlying type.
/// </summary>
public readonly Type Type;
#endif

/// <summary>
/// Implicitly converts from the wrapper to the underlying type.
/// </summary>
/// <param name="type">An instance of <see cref="ActivatableType"/></param>
/// <returns>The <see cref="Type"/></returns>
public static implicit operator Type(ActivatableType type) => type.Type;
}


/// <summary>
/// Creates a <see cref="DelegatingHandler"/> to use for the <see cref="HttpClient" /> from the provided <see cref="DelegatingHandler"/> instances. Order matters.
/// </summary>
Expand Down

0 comments on commit a2800ab

Please sign in to comment.