Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
Temp commit to debug
42 lines
1.6 KiB
C#
42 lines
1.6 KiB
C#
using System;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
using Microsoft.Extensions.Options;
|
|
using StellaOps.AdvisoryAI.Abstractions;
|
|
using StellaOps.AdvisoryAI.Providers;
|
|
using StellaOps.AdvisoryAI.Retrievers;
|
|
|
|
namespace StellaOps.AdvisoryAI.DependencyInjection;
|
|
|
|
public static class SbomContextServiceCollectionExtensions
|
|
{
|
|
public static IServiceCollection AddSbomContext(this IServiceCollection services, Action<SbomContextClientOptions>? configure = null)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(services);
|
|
|
|
var optionsBuilder = services.AddOptions<SbomContextClientOptions>();
|
|
if (configure is not null)
|
|
{
|
|
optionsBuilder.Configure(configure);
|
|
}
|
|
|
|
services.AddHttpClient<ISbomContextClient, SbomContextHttpClient>((serviceProvider, client) =>
|
|
{
|
|
var options = serviceProvider.GetRequiredService<IOptions<SbomContextClientOptions>>().Value;
|
|
if (options.BaseAddress is not null)
|
|
{
|
|
client.BaseAddress = options.BaseAddress;
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(options.Tenant) && !string.IsNullOrWhiteSpace(options.TenantHeaderName))
|
|
{
|
|
client.DefaultRequestHeaders.Remove(options.TenantHeaderName);
|
|
client.DefaultRequestHeaders.Add(options.TenantHeaderName, options.Tenant);
|
|
}
|
|
});
|
|
|
|
services.TryAddSingleton<ISbomContextRetriever, SbomContextRetriever>();
|
|
return services;
|
|
}
|
|
}
|