Skip to content

Commit

Permalink
feat: add metadata provider
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicGD committed Mar 26, 2024
1 parent 4522f7d commit e4db75c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Sitko.Core.Grpc.Client/GrpcClientModuleOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,34 @@ internal void ConfigureClient(IServiceCollection services, IHttpClientBuilder bu
configureAuthAction?.Invoke(services, builder);
}

public GrpcClientModuleOptions<TClient> AddMetadataProvider<TMetadataProvider>()
where TMetadataProvider : class, IGrpcMetadataProvider
{
configureAuthAction = (services, builder) =>
{
services.TryAddTransient<IGrpcMetadataProvider, TMetadataProvider>();
builder.AddCallCredentials(async (_, metadata, serviceProvider) =>
{
var provider = serviceProvider.GetRequiredService<TMetadataProvider>();
var newMetadata = await provider.GetMetadataAsync();
if (newMetadata.Count != 0)
{
foreach (var (key, value) in newMetadata)
{
var oldEntries = metadata.GetAll(key);
foreach (var entry in oldEntries)
{
metadata.Remove(entry);
}

metadata.Add(key, value);
}
}
});
};
return this;
}

public GrpcClientModuleOptions<TClient> AddTokenAuth<TTokenProvider>()
where TTokenProvider : class, IGrpcTokenProvider
{
Expand Down
6 changes: 6 additions & 0 deletions src/Sitko.Core.Grpc.Client/IGrpcMetadataProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Sitko.Core.Grpc.Client;

public interface IGrpcMetadataProvider
{
Task<Dictionary<string, string>> GetMetadataAsync();
}

0 comments on commit e4db75c

Please sign in to comment.