Skip to content

Commit

Permalink
Add EventHub client extension methodS (#7034)
Browse files Browse the repository at this point in the history
  • Loading branch information
pakrym authored Jul 29, 2019
1 parent 85cfe4d commit 506f75f
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 18 deletions.
19 changes: 13 additions & 6 deletions sdk/core/Azure.Core.Extensions/src/AzureClientFactoryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Azure.Core.Extensions
{
public sealed class AzureClientFactoryBuilder : IAzureClientFactoryBuilderWithConfiguration<IConfiguration>, IAzureClientsBuilderWithCredential
public sealed class AzureClientFactoryBuilder : IAzureClientFactoryBuilderWithConfiguration<IConfiguration>, IAzureClientFactoryBuilderWithCredential
{
private readonly IServiceCollection _serviceCollection;

Expand All @@ -25,15 +25,22 @@ internal AzureClientFactoryBuilder(IServiceCollection serviceCollection)

IAzureClientBuilder<TClient, TOptions> IAzureClientFactoryBuilder.RegisterClientFactory<TClient, TOptions>(Func<TOptions, TClient> clientFactory)
{
return ((IAzureClientsBuilderWithCredential)this).RegisterClientFactory<TClient, TOptions>((options, _) => clientFactory(options));
return ((IAzureClientFactoryBuilderWithCredential)this).RegisterClientFactory<TClient, TOptions>((options, _) => clientFactory(options));
}

IAzureClientBuilder<TClient, TOptions> IAzureClientFactoryBuilderWithConfiguration<IConfiguration>.RegisterClientFactory<TClient, TOptions>(IConfiguration configuration)
{
return ((IAzureClientsBuilderWithCredential)this).RegisterClientFactory<TClient, TOptions>(
var credentialsFromConfig = ClientFactory.CreateCredential(configuration);
var clientBuilder =((IAzureClientFactoryBuilderWithCredential)this).RegisterClientFactory<TClient, TOptions>(
(options, credentials) => (TClient)ClientFactory.CreateClient(typeof(TClient), typeof(TOptions), options, configuration, credentials))
.ConfigureOptions(configuration)
.WithCredential(ClientFactory.CreateCredential(configuration));
.ConfigureOptions(configuration);

if (credentialsFromConfig != null)
{
clientBuilder.WithCredential(credentialsFromConfig);
}

return clientBuilder;
}

public AzureClientFactoryBuilder ConfigureDefaults(Action<ClientOptions> configureOptions)
Expand Down Expand Up @@ -63,7 +70,7 @@ public AzureClientFactoryBuilder ConfigureDefaults(IConfiguration configuration)
return this;
}

IAzureClientBuilder<TClient, TOptions> IAzureClientsBuilderWithCredential.RegisterClientFactory<TClient, TOptions>(Func<TOptions, TokenCredential, TClient> clientFactory)
IAzureClientBuilder<TClient, TOptions> IAzureClientFactoryBuilderWithCredential.RegisterClientFactory<TClient, TOptions>(Func<TOptions, TokenCredential, TClient> clientFactory)
{
var clientRegistration = new ClientRegistration<TClient, TOptions>(DefaultClientName, clientFactory);
_serviceCollection.AddSingleton(clientRegistration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Azure.Core.Extensions
{
internal class DefaultClientOptionsSetup<T> : IConfigureNamedOptions<T> where T : ClientOptions
internal class DefaultClientOptionsSetup<T> : IConfigureNamedOptions<T> where T : class
{
private readonly IOptions<AzureClientsGlobalOptions> _defaultOptions;
private readonly IServiceProvider _serviceProvider;
Expand All @@ -20,9 +20,12 @@ public DefaultClientOptionsSetup(IOptions<AzureClientsGlobalOptions> defaultOpti

public void Configure(T options)
{
foreach (var globalConfigureOption in _defaultOptions.Value.ConfigureOptionDelegates)
if (options is ClientOptions clientOptions)
{
globalConfigureOption(options, _serviceProvider);
foreach (var globalConfigureOption in _defaultOptions.Value.ConfigureOptionDelegates)
{
globalConfigureOption(clientOptions, _serviceProvider);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static IAzureClientBuilder<TestClientWithCredentials, TestClientOptions>
}

public static IAzureClientBuilder<TestClientWithCredentials, TestClientOptions> AddTestClientWithCredentials<TBuilder>(this TBuilder builder, Uri uri)
where TBuilder: IAzureClientsBuilderWithCredential
where TBuilder: IAzureClientFactoryBuilderWithCredential
{
return builder.RegisterClientFactory<TestClientWithCredentials, TestClientOptions>((options, cred) => new TestClientWithCredentials(uri, cred, options));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ namespace Azure.Core.Extensions
{
public interface IAzureClientFactoryBuilder
{
IAzureClientBuilder<TClient, TOptions> RegisterClientFactory<TClient, TOptions>(Func<TOptions, TClient> clientFactory) where TOptions : ClientOptions;
IAzureClientBuilder<TClient, TOptions> RegisterClientFactory<TClient, TOptions>(Func<TOptions, TClient> clientFactory) where TOptions : class;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Core.Pipeline;

namespace Azure.Core.Extensions
{

public interface IAzureClientFactoryBuilderWithConfiguration<in TConfiguration>: IAzureClientFactoryBuilder
{
IAzureClientBuilder<TClient, TOptions> RegisterClientFactory<TClient, TOptions>(TConfiguration configuration) where TOptions : ClientOptions;
IAzureClientBuilder<TClient, TOptions> RegisterClientFactory<TClient, TOptions>(TConfiguration configuration) where TOptions : class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
// Licensed under the MIT License.

using System;
using Azure.Core.Pipeline;

namespace Azure.Core.Extensions
{
public interface IAzureClientsBuilderWithCredential
public interface IAzureClientFactoryBuilderWithCredential
{
IAzureClientBuilder<TClient, TOptions> RegisterClientFactory<TClient, TOptions>(Func<TOptions, TokenCredential, TClient> clientFactory) where TOptions : ClientOptions;
IAzureClientBuilder<TClient, TOptions> RegisterClientFactory<TClient, TOptions>(Func<TOptions, TokenCredential, TClient> clientFactory) where TOptions: class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Core.Extensions;
using Azure.Messaging.EventHubs;

namespace Azure.ApplicationModel.Configuration
{
/// <summary>
/// Extension methods to add <see cref="EventHubClient"/> client to clients builder
/// </summary>
public static class AzureClientBuilderExtensions
{
/// <summary>
/// Registers a <see cref="EventHubClient"/> instance with the provided <paramref name="connectionString"/>
/// </summary>
public static IAzureClientBuilder<EventHubClient, EventHubClientOptions> AddEventHubClient<TBuilder>(this TBuilder builder, string connectionString)
where TBuilder: IAzureClientFactoryBuilder
{
return builder.RegisterClientFactory<EventHubClient, EventHubClientOptions>(options => new EventHubClient(connectionString, options));
}

/// <summary>
/// Registers a <see cref="EventHubClient"/> instance with the provided <paramref name="connectionString"/> and <paramref name="eventHubPath"/>
/// </summary>
public static IAzureClientBuilder<EventHubClient, EventHubClientOptions> AddEventHubClient<TBuilder>(this TBuilder builder, string connectionString, string eventHubPath)
where TBuilder: IAzureClientFactoryBuilder
{
return builder.RegisterClientFactory<EventHubClient, EventHubClientOptions>(options => new EventHubClient(connectionString, eventHubPath, options));
}

/// <summary>
/// Registers a <see cref="EventHubClient"/> instance with the provided <paramref name="host"/> and <paramref name="eventHubPath"/>
/// </summary>
public static IAzureClientBuilder<EventHubClient, EventHubClientOptions> AddEventHubClientWithHost<TBuilder>(this TBuilder builder, string host, string eventHubPath)
where TBuilder: IAzureClientFactoryBuilderWithCredential
{
return builder.RegisterClientFactory<EventHubClient, EventHubClientOptions>((options, token) => new EventHubClient(host, eventHubPath, token, options));
}

/// <summary>
/// Registers a <see cref="EventHubClient"/> instance with connection options loaded from the provided <paramref name="configuration"/> instance.
/// </summary>
public static IAzureClientBuilder<EventHubClient, EventHubClientOptions> AddEventHubClient<TBuilder, TConfiguration>(this TBuilder builder, TConfiguration configuration)
where TBuilder: IAzureClientFactoryBuilderWithConfiguration<TConfiguration>
{
return builder.RegisterClientFactory<EventHubClient, EventHubClientOptions>(configuration);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class AzureClientBuilderExtensions
/// Registers a <see cref="SecretClient"/> instance with the provided <paramref name="vaultUri"/>
/// </summary>
public static IAzureClientBuilder<SecretClient, SecretClientOptions> AddSecretClient<TBuilder>(this TBuilder builder, Uri vaultUri)
where TBuilder: IAzureClientsBuilderWithCredential
where TBuilder: IAzureClientFactoryBuilderWithCredential
{
return builder.RegisterClientFactory<SecretClient, SecretClientOptions>((options, cred) => new SecretClient(vaultUri, cred, options));
}
Expand Down

0 comments on commit 506f75f

Please sign in to comment.