Skip to content

Commit

Permalink
Merge pull request #950 from sitkoru/grpc/client/grpc-providers
Browse files Browse the repository at this point in the history
feat: register self provider factory to each client
  • Loading branch information
pogromistik authored May 21, 2024
2 parents f2329ac + 25cce94 commit 36844cf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/Sitko.Core.Grpc.Client/GrpcClientModuleOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,22 @@ internal void ConfigureClient(IServiceCollection services, IHttpClientBuilder bu

builder.AddCallCredentials(async (_, metadata, serviceProvider) =>
{
var tokenProvider = serviceProvider.GetService<IGrpcTokenProvider>();
if (tokenProvider is not null)
var tokenProviderFactory = serviceProvider.GetService<IGrpcTokenProviderFactory<TClient>>();
if (tokenProviderFactory is not null)
{
var tokenProvider = tokenProviderFactory.GetProvider(serviceProvider);
var token = await tokenProvider.GetTokenAsync();
if (!string.IsNullOrEmpty(token))
{
SetMetadata(metadata, AuthorizationHeader, token);
}
}

var metadataProviders = serviceProvider.GetServices<IGrpcMetadataProvider>();
foreach (var metadataProvider in metadataProviders)

var metadataProviderFactory = serviceProvider.GetService<IGrpcMetadataProviderFactory<TClient>>();
if (metadataProviderFactory is not null)
{
var metadataProvider = metadataProviderFactory.GetProvider(serviceProvider);
var newMetadata = await metadataProvider.GetMetadataAsync();
if (newMetadata?.Count > 0)
{
Expand Down Expand Up @@ -76,7 +79,8 @@ public GrpcClientModuleOptions<TClient> AddMetadataProvider<TMetadataProvider>()
{
configureServicesActions.Add(services =>
{
services.AddTransient<IGrpcMetadataProvider, TMetadataProvider>();
services.AddTransient<IGrpcMetadataProviderFactory<TClient>, GrpcMetadataProviderFactory<TClient, TMetadataProvider>>();
services.TryAddTransient<IGrpcMetadataProvider, TMetadataProvider>();
});
return this;
}
Expand All @@ -86,6 +90,7 @@ public GrpcClientModuleOptions<TClient> AddTokenAuth<TTokenProvider>()
{
configureServicesActions.Add(services =>
{
services.AddTransient<IGrpcTokenProviderFactory<TClient>, GrpcTokenProviderFactory<TClient, TTokenProvider>>();
services.TryAddTransient<IGrpcTokenProvider, TTokenProvider>();
});
return this;
Expand Down
16 changes: 16 additions & 0 deletions src/Sitko.Core.Grpc.Client/IGrpcMetadataProviderFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Grpc.Core;
using Microsoft.Extensions.DependencyInjection;

namespace Sitko.Core.Grpc.Client;

internal interface IGrpcMetadataProviderFactory<TClient> where TClient : ClientBase<TClient>
{
public IGrpcMetadataProvider GetProvider(IServiceProvider serviceProvider);
}

internal class GrpcMetadataProviderFactory<TClient, TMetadataProvider> : IGrpcMetadataProviderFactory<TClient> where TClient : ClientBase<TClient>
where TMetadataProvider : class, IGrpcMetadataProvider

{
public IGrpcMetadataProvider GetProvider(IServiceProvider serviceProvider) => serviceProvider.GetService<TMetadataProvider>()!;
}
16 changes: 16 additions & 0 deletions src/Sitko.Core.Grpc.Client/IGrpcTokenProviderFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Grpc.Core;
using Microsoft.Extensions.DependencyInjection;

namespace Sitko.Core.Grpc.Client;

public interface IGrpcTokenProviderFactory<TClient> where TClient : ClientBase<TClient>
{
public IGrpcTokenProvider GetProvider(IServiceProvider serviceProvider);
}

internal class GrpcTokenProviderFactory<TClient, TTokenProvider> : IGrpcTokenProviderFactory<TClient> where TClient : ClientBase<TClient>
where TTokenProvider : class, IGrpcTokenProvider

{
public IGrpcTokenProvider GetProvider(IServiceProvider serviceProvider) => serviceProvider.GetService<TTokenProvider>()!;
}

0 comments on commit 36844cf

Please sign in to comment.