Skip to content

Commit

Permalink
Merge pull request #951 from sitkoru/grpc/client/grpc-providers
Browse files Browse the repository at this point in the history
fix: get generic grpc token\metadata provider
  • Loading branch information
pogromistik authored May 22, 2024
2 parents 36844cf + 69ef0e7 commit 8676141
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Sitko.Core.Grpc.Client/GrpcClientModuleOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public GrpcClientModuleOptions<TClient> AddMetadataProvider<TMetadataProvider>()
configureServicesActions.Add(services =>
{
services.AddTransient<IGrpcMetadataProviderFactory<TClient>, GrpcMetadataProviderFactory<TClient, TMetadataProvider>>();
services.TryAddTransient<IGrpcMetadataProvider, TMetadataProvider>();
services.AddTransient<IGrpcMetadataProvider, TMetadataProvider>();
});
return this;
}
Expand All @@ -91,7 +91,7 @@ public GrpcClientModuleOptions<TClient> AddTokenAuth<TTokenProvider>()
configureServicesActions.Add(services =>
{
services.AddTransient<IGrpcTokenProviderFactory<TClient>, GrpcTokenProviderFactory<TClient, TTokenProvider>>();
services.TryAddTransient<IGrpcTokenProvider, TTokenProvider>();
services.AddTransient<IGrpcTokenProvider, TTokenProvider>();
});
return this;
}
Expand Down
7 changes: 5 additions & 2 deletions src/Sitko.Core.Grpc.Client/IGrpcMetadataProviderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ internal interface IGrpcMetadataProviderFactory<TClient> where TClient : ClientB
public IGrpcMetadataProvider GetProvider(IServiceProvider serviceProvider);
}

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

{
public IGrpcMetadataProvider GetProvider(IServiceProvider serviceProvider) => serviceProvider.GetService<TMetadataProvider>()!;
public IGrpcMetadataProvider GetProvider(IServiceProvider serviceProvider) =>
serviceProvider.GetServices<IGrpcMetadataProvider>().OfType<TMetadataProvider>().FirstOrDefault() ??
throw new InvalidDataException($"Service {typeof(TMetadataProvider)} is not register");
}
7 changes: 5 additions & 2 deletions src/Sitko.Core.Grpc.Client/IGrpcTokenProviderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ public interface IGrpcTokenProviderFactory<TClient> where TClient : ClientBase<T
public IGrpcTokenProvider GetProvider(IServiceProvider serviceProvider);
}

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

{
public IGrpcTokenProvider GetProvider(IServiceProvider serviceProvider) => serviceProvider.GetService<TTokenProvider>()!;
public IGrpcTokenProvider GetProvider(IServiceProvider serviceProvider) =>
serviceProvider.GetServices<IGrpcTokenProvider>().OfType<TTokenProvider>().FirstOrDefault() ??
throw new InvalidDataException($"Service {typeof(TTokenProvider)} is not register");
}

0 comments on commit 8676141

Please sign in to comment.