using System; using System.Collections.Generic; using System.Net.Http; using Marechai.App.Services; using Microsoft.Kiota.Abstractions.Authentication; using Microsoft.Kiota.Http.HttpClientLibrary; using Microsoft.Kiota.Serialization.Json; using Microsoft.Kiota.Serialization.Multipart; namespace Uno.Extensions.Http.Kiota; /// /// Provides extension methods for registering Kiota clients within the . /// public static class ServiceCollectionExtensions { /// /// Registers a Kiota client with the specified and endpoint options. /// /// The Kiota client type to register. /// The to register the client with. /// The providing the hosting context. /// [Optional] The endpoint options for the client (loaded from appsettings if not specified). /// [Optional] The name for locating endpoint information in appsettings. /// [Optional] A callback for configuring the endpoint. /// The updated with the registered Kiota client. public static IServiceCollection AddKiotaClientV2(this IServiceCollection services, HostBuilderContext context, EndpointOptions? options = null, string? name = null, Func? configure = null) where TClient : class => services.AddKiotaClientWithEndpointV2(context, options, name, configure); /// /// Registers a Kiota client with the specified and supports additional endpoint options. /// /// The Kiota client type to register. /// The type of endpoint to register. /// The to register the client with. /// The providing the hosting context. /// [Optional] The endpoint options for the client (loaded from appsettings if not specified). /// [Optional] The name for locating endpoint information in appsettings. /// [Optional] A callback for configuring the endpoint. /// The updated with the registered Kiota client. public static IServiceCollection AddKiotaClientWithEndpointV2( this IServiceCollection services, HostBuilderContext context, TEndpoint? options = null, string? name = null, Func? configure = null) where TClient : class where TEndpoint : EndpointOptions, new() { services.AddKiotaHandlers(); string clientName = name ?? typeof(TClient).FullName ?? "DefaultClient"; return services.AddClientWithEndpoint(context, options, clientName, (s, c) => s.AddHttpClient(clientName) .AddTypedClient((httpClient, sp) => { var authProvider = new AnonymousAuthenticationProvider(); var parseNodeFactory = new JsonParseNodeFactory(); var serializationWriterFactory = new CompositeSerializationWriterFactory(); serializationWriterFactory .AddFactory(new JsonSerializationWriterFactory()); serializationWriterFactory .AddFactory(new MultipartSerializationWriterFactory()); var requestAdapter = new HttpClientRequestAdapter(authProvider, parseNodeFactory, serializationWriterFactory, httpClient); return (TClient) Activator .CreateInstance(typeof( TClient), requestAdapter)!; }) .AttachKiotaHandlers(), configure); } /// /// Dynamically adds Kiota handlers to the service collection. /// /// The to register the handlers with. /// The updated with the registered Kiota handlers. private static IServiceCollection AddKiotaHandlers(this IServiceCollection services) { IList kiotaHandlers = KiotaClientFactory.GetDefaultHandlerTypes(); foreach(Type handler in kiotaHandlers) services.AddTransient(handler); return services; } /// /// Attaches Kiota handlers to the . /// /// The to attach the handlers to. /// The updated with the attached Kiota handlers. private static IHttpClientBuilder AttachKiotaHandlers(this IHttpClientBuilder builder) { IList kiotaHandlers = KiotaClientFactory.GetDefaultHandlerTypes(); foreach(Type handler in kiotaHandlers) builder.AddHttpMessageHandler(sp => (DelegatingHandler)sp.GetRequiredService(handler)); return builder; } }