< Summary

Class:Microsoft.Extensions.Azure.AzureClientBuilderExtensions
Assembly:Microsoft.Extensions.Azure
File(s):C:\Git\azure-sdk-for-net\sdk\core\Microsoft.Extensions.Azure\src\AzureClientBuilderExtensions.cs
Covered lines:15
Uncovered lines:1
Coverable lines:16
Total lines:125
Line coverage:93.7% (15 of 16)
Covered branches:1
Total branches:2
Branch coverage:50% (1 of 2)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
WithName(...)-100%100%
WithCredential(...)-100%100%
WithCredential(...)-100%100%
ConfigureOptions(...)-100%100%
ConfigureOptions(...)-100%100%
ConfigureOptions(...)-100%100%
WithVersion(...)-75%50%
ToBuilder(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Microsoft.Extensions.Azure\src\AzureClientBuilderExtensions.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using Azure.Core;
 5using Azure.Core.Extensions;
 6using Microsoft.Extensions.Configuration;
 7using Microsoft.Extensions.DependencyInjection;
 8using Microsoft.Extensions.Options;
 9using System;
 10
 11namespace Microsoft.Extensions.Azure
 12{
 13    /// <summary>
 14    /// Extension methods to configure client registrations.
 15    /// </summary>
 16    public static class AzureClientBuilderExtensions
 17    {
 18        /// <summary>
 19        /// Sets the name for the client registration. To resolve named clients use <see cref="IAzureClientFactory{TClie
 20        /// </summary>
 21        /// <typeparam name="TClient">The type of the client.</typeparam>
 22        /// <typeparam name="TOptions">The options type the client uses.</typeparam>
 23        /// <param name="builder">The client builder instance.</param>
 24        /// <param name="name">The name to set.</param>
 25        /// <returns>The client builder instance.</returns>
 26        public static IAzureClientBuilder<TClient, TOptions> WithName<TClient, TOptions>(this IAzureClientBuilder<TClien
 27        {
 228            builder.ToBuilder().Registration.Name = name;
 229            return builder;
 30        }
 31
 32        /// <summary>
 33        /// Set the credential to use for this client registration.
 34        /// </summary>
 35        /// <typeparam name="TClient">The type of the client.</typeparam>
 36        /// <typeparam name="TOptions">The options type the client uses.</typeparam>
 37        /// <param name="builder">The client builder instance.</param>
 38        /// <param name="credential">The credential to use.</param>
 39        /// <returns>The client builder instance.</returns>
 40        public static IAzureClientBuilder<TClient, TOptions> WithCredential<TClient, TOptions>(this IAzureClientBuilder<
 41        {
 1242            return builder.WithCredential(_ => credential);
 43        }
 44
 45        /// <summary>
 46        /// Set the credential factory to use for this client registration.
 47        /// </summary>
 48        /// <typeparam name="TClient">The type of the client.</typeparam>
 49        /// <typeparam name="TOptions">The options type the client uses.</typeparam>
 50        /// <param name="builder">The client builder instance.</param>
 51        /// <param name="credentialFactory">The credential factory to use.</param>
 52        /// <returns>The client builder instance.</returns>
 53        public static IAzureClientBuilder<TClient, TOptions> WithCredential<TClient, TOptions>(this IAzureClientBuilder<
 54        {
 655            var impl = builder.ToBuilder();
 656            impl.ServiceCollection.AddSingleton<IConfigureOptions<AzureClientCredentialOptions<TClient>>>(new ConfigureC
 657            return builder;
 58        }
 59
 60        /// <summary>
 61        /// Adds a delegate to configure the client options. All delegates are executed in order they were added.
 62        /// </summary>
 63        /// <typeparam name="TClient">The type of the client.</typeparam>
 64        /// <typeparam name="TOptions">The options type the client uses.</typeparam>
 65        /// <param name="builder">The client builder instance.</param>
 66        /// <param name="configureOptions">The delegate to use to configure options.</param>
 67        /// <returns>The client builder instance.</returns>
 68        public static IAzureClientBuilder<TClient, TOptions> ConfigureOptions<TClient, TOptions>(this IAzureClientBuilde
 69        {
 2470            return builder.ConfigureOptions((options, _) => configureOptions(options));
 71        }
 72
 73        /// <summary>
 74        /// Configures client options using provided <see cref="IConfiguration"/> instance.
 75        /// </summary>
 76        /// <typeparam name="TClient">The type of the client.</typeparam>
 77        /// <typeparam name="TOptions">The options type the client uses.</typeparam>
 78        /// <param name="builder">The client builder instance.</param>
 79        /// <param name="configuration">The configuration instance to use.</param>
 80        /// <returns>The client builder instance.</returns>
 81        public static IAzureClientBuilder<TClient, TOptions> ConfigureOptions<TClient, TOptions>(this IAzureClientBuilde
 82        {
 1283            return builder.ConfigureOptions(options => configuration.Bind(options));
 84        }
 85
 86        /// <summary>
 87        /// Adds a delegate to configure the client options. All delegates are executed in order they were added.
 88        /// </summary>
 89        /// <typeparam name="TClient">The type of the client.</typeparam>
 90        /// <typeparam name="TOptions">The options type the client uses.</typeparam>
 91        /// <param name="builder">The client builder instance.</param>
 92        /// <param name="configureOptions">The delegate to use to configure options.</param>
 93        /// <returns>The client builder instance.</returns>
 94        public static IAzureClientBuilder<TClient, TOptions> ConfigureOptions<TClient, TOptions>(this IAzureClientBuilde
 95        {
 1296            var impl = builder.ToBuilder();
 2497            impl.ServiceCollection.AddSingleton<IConfigureOptions<TOptions>>(provider => new ConfigureClientOptions<TCli
 1298            return builder;
 99        }
 100
 101        /// <summary>
 102        /// Sets the service version to use for this client registration.
 103        /// </summary>
 104        /// <typeparam name="TClient">The type of the client.</typeparam>
 105        /// <typeparam name="TOptions">The options type the client uses.</typeparam>
 106        /// <typeparam name="TVersion">The service version enum type.</typeparam>
 107        /// <param name="builder">The client builder instance.</param>
 108        /// <param name="version">The delegate to use to configure options.</param>
 109        /// <returns>The client builder instance.</returns>
 110        public static IAzureClientBuilder<TClient, TOptions> WithVersion<TClient, TOptions, TVersion>(this IAzureClientB
 111        {
 4112            if (typeof(TVersion).DeclaringType != typeof(TOptions))
 113            {
 0114                throw new ArgumentException($"Version should be of type {typeof(TOptions)}.ServiceVersion");
 115            }
 4116            builder.ToBuilder().Registration.Version = version;
 4117            return builder;
 118        }
 119
 120        private static AzureClientBuilder<TClient, TOptions> ToBuilder<TClient, TOptions>(this IAzureClientBuilder<TClie
 121        {
 24122            return (AzureClientBuilder<TClient, TOptions>)builder;
 123        }
 124    }
 125}