| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using Azure.Core; |
| | 5 | | using Azure.Core.Extensions; |
| | 6 | | using Microsoft.Extensions.Configuration; |
| | 7 | | using Microsoft.Extensions.DependencyInjection; |
| | 8 | | using Microsoft.Extensions.Options; |
| | 9 | | using System; |
| | 10 | |
|
| | 11 | | namespace 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 | | { |
| 2 | 28 | | builder.ToBuilder().Registration.Name = name; |
| 2 | 29 | | 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 | | { |
| 12 | 42 | | 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 | | { |
| 6 | 55 | | var impl = builder.ToBuilder(); |
| 6 | 56 | | impl.ServiceCollection.AddSingleton<IConfigureOptions<AzureClientCredentialOptions<TClient>>>(new ConfigureC |
| 6 | 57 | | 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 | | { |
| 24 | 70 | | 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 | | { |
| 12 | 83 | | 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 | | { |
| 12 | 96 | | var impl = builder.ToBuilder(); |
| 24 | 97 | | impl.ServiceCollection.AddSingleton<IConfigureOptions<TOptions>>(provider => new ConfigureClientOptions<TCli |
| 12 | 98 | | 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 | | { |
| 4 | 112 | | if (typeof(TVersion).DeclaringType != typeof(TOptions)) |
| | 113 | | { |
| 0 | 114 | | throw new ArgumentException($"Version should be of type {typeof(TOptions)}.ServiceVersion"); |
| | 115 | | } |
| 4 | 116 | | builder.ToBuilder().Registration.Version = version; |
| 4 | 117 | | return builder; |
| | 118 | | } |
| | 119 | |
|
| | 120 | | private static AzureClientBuilder<TClient, TOptions> ToBuilder<TClient, TOptions>(this IAzureClientBuilder<TClie |
| | 121 | | { |
| 24 | 122 | | return (AzureClientBuilder<TClient, TOptions>)builder; |
| | 123 | | } |
| | 124 | | } |
| | 125 | | } |