Skip to content

Commit

Permalink
feat: register self provider factory to each client
Browse files Browse the repository at this point in the history
  • Loading branch information
pogromistik committed May 21, 2024
1 parent f2329ac commit 25cce94
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 25cce94

Please sign in to comment.