using Azure.Core; using Azure.Identity; using Microsoft.Extensions.Options; using Net.Sdk.Web.Extensions.Azure.Options; using System.Core.Extensions; using System.Extensions; namespace Net.Sdk.Web.Extensions.Azure; public static class WebApplicationBuilderExtensions { public static WebApplicationBuilder ConfigureAzureClientSecretCredentials(this WebApplicationBuilder builder) { builder.ThrowIfNull() .ConfigureExtended() .Services.AddSingleton(sp => { var options = sp.GetRequiredService>().Value; return new ClientSecretCredential(options.TenantId, options.ClientId, options.ClientSecret); }); return builder; } public static WebApplicationBuilder ConfigureAzureClientSecretCredentials(this WebApplicationBuilder builder) where TOptions : class, IAzureClientSecretCredentialOptions, new() { builder.ThrowIfNull() .ConfigureExtended() .Services.AddSingleton(sp => { var options = sp.GetRequiredService>().Value; return new ClientSecretCredential(options.TenantId, options.ClientId, options.ClientSecret); }); return builder; } public static WebApplicationBuilder WithTableClientSingleton(this WebApplicationBuilder builder) where TTableOptions : class, IAzureTableStorageOptions, new() where TAccountOptions : class, IStorageAccountOptions, new() { builder.Services.ThrowIfNull() .AddSingleton(sp => { var tokenCredential = sp.GetRequiredService(); var storageOptions = sp.GetRequiredService>(); var clientOptions = sp.GetRequiredService>(); var logger = sp.GetRequiredService>>(); return new NamedTableClient(logger, new Uri($"https://{storageOptions.Value.AccountName}.table.core.windows.net"), clientOptions.Value.TableName, tokenCredential, default); }); return builder; } public static WebApplicationBuilder WithTableClientSingleton(this WebApplicationBuilder builder, string tableName) where TAccountOptions : class, IStorageAccountOptions, new() { tableName.ThrowIfNull(); builder.Services.ThrowIfNull() .AddSingleton(sp => { var tokenCredential = sp.GetRequiredService(); var storageOptions = sp.GetRequiredService>(); var logger = sp.GetRequiredService>>(); return new NamedTableClient(logger, new Uri($"https://{storageOptions.Value.AccountName}.table.core.windows.net"), tableName, tokenCredential, default); }); return builder; } public static WebApplicationBuilder WithTableClientSingleton(this WebApplicationBuilder builder, string accountName, string tableName) { tableName.ThrowIfNull(); accountName.ThrowIfNull(); builder.Services.ThrowIfNull() .AddSingleton(sp => { var tokenCredential = sp.GetRequiredService(); var logger = sp.GetRequiredService>>(); return new NamedTableClient(logger, new Uri($"https://{accountName}.table.core.windows.net"), tableName, tokenCredential, default); }); return builder; } public static WebApplicationBuilder WithTableClientScoped(this WebApplicationBuilder builder) where TTableOptions : class, IAzureTableStorageOptions, new() where TAccountOptions : class, IStorageAccountOptions, new() { builder.Services.ThrowIfNull() .AddScoped(sp => { var tokenCredential = sp.GetRequiredService(); var storageOptions = sp.GetRequiredService>(); var clientOptions = sp.GetRequiredService>(); var logger = sp.GetRequiredService>>(); return new NamedTableClient(logger, new Uri($"https://{storageOptions.Value.AccountName}.table.core.windows.net"), clientOptions.Value.TableName, tokenCredential, default); }); return builder; } public static WebApplicationBuilder WithTableClientScoped(this WebApplicationBuilder builder, string tableName) where TAccountOptions : class, IStorageAccountOptions, new() { tableName.ThrowIfNull(); builder.Services.ThrowIfNull() .AddScoped(sp => { var tokenCredential = sp.GetRequiredService(); var storageOptions = sp.GetRequiredService>(); var logger = sp.GetRequiredService>>(); return new NamedTableClient(logger, new Uri($"https://{storageOptions.Value.AccountName}.table.core.windows.net"), tableName, tokenCredential, default); }); return builder; } public static WebApplicationBuilder WithTableClientScoped(this WebApplicationBuilder builder, string accountName, string tableName) { tableName.ThrowIfNull(); accountName.ThrowIfNull(); builder.Services.ThrowIfNull() .AddScoped(sp => { var tokenCredential = sp.GetRequiredService(); var logger = sp.GetRequiredService>>(); return new NamedTableClient(logger, new Uri($"https://{accountName}.table.core.windows.net"), tableName, tokenCredential, default); }); return builder; } public static WebApplicationBuilder WithBlobContainerClientSingleton(this WebApplicationBuilder builder) where TBlobContainerOptions : class, IAzureBlobStorageOptions, new() where TAccountOptions : class, IStorageAccountOptions, new() { builder.ThrowIfNull() .Services.AddSingleton(sp => { var tokenCredential = sp.GetRequiredService(); var storageOptions = sp.GetRequiredService>(); var clientOptions = sp.GetRequiredService>(); return new NamedBlobContainerClient(new Uri($"https://{storageOptions.Value.AccountName}.blob.core.windows.net/{clientOptions.Value.ContainerName}"), tokenCredential, default); }); return builder; } public static WebApplicationBuilder WithBlobContainerClientSingleton(this WebApplicationBuilder builder, string containerName) where TAccountOptions : class, IStorageAccountOptions, new() { containerName.ThrowIfNull(); builder.ThrowIfNull() .Services.AddSingleton(sp => { var tokenCredential = sp.GetRequiredService(); var storageOptions = sp.GetRequiredService>(); return new NamedBlobContainerClient(new Uri($"https://{storageOptions.Value.AccountName}.blob.core.windows.net/{containerName}"), tokenCredential, default); }); return builder; } public static WebApplicationBuilder WithBlobContainerClientSingleton(this WebApplicationBuilder builder, string accountName, string containerName) { accountName.ThrowIfNull(); containerName.ThrowIfNull(); builder.ThrowIfNull() .Services.AddSingleton(sp => { var tokenCredential = sp.GetRequiredService(); return new NamedBlobContainerClient(new Uri($"https://{accountName}.blob.core.windows.net/{containerName}"), tokenCredential, default); }); return builder; } public static WebApplicationBuilder WithBlobContainerClientScoped(this WebApplicationBuilder builder) where TBlobContainerOptions : class, IAzureBlobStorageOptions, new() where TAccountOptions : class, IStorageAccountOptions, new() { builder.ThrowIfNull() .Services.AddScoped(sp => { var tokenCredential = sp.GetRequiredService(); var storageOptions = sp.GetRequiredService>(); var clientOptions = sp.GetRequiredService>(); return new NamedBlobContainerClient(new Uri($"https://{storageOptions.Value.AccountName}.blob.core.windows.net/{clientOptions.Value.ContainerName}"), tokenCredential, default); }); return builder; } public static WebApplicationBuilder WithBlobContainerClientScoped(this WebApplicationBuilder builder, string containerName) where TAccountOptions : class, IStorageAccountOptions, new() { containerName.ThrowIfNull(); builder.ThrowIfNull() .Services.AddScoped(sp => { var tokenCredential = sp.GetRequiredService(); var storageOptions = sp.GetRequiredService>(); return new NamedBlobContainerClient(new Uri($"https://{storageOptions.Value.AccountName}.blob.core.windows.net/{containerName}"), tokenCredential, default); }); return builder; } public static WebApplicationBuilder WithBlobContainerClientScoped(this WebApplicationBuilder builder, string accountName, string containerName) { accountName.ThrowIfNull(); containerName.ThrowIfNull(); builder.ThrowIfNull() .Services.AddScoped(sp => { var tokenCredential = sp.GetRequiredService(); return new NamedBlobContainerClient(new Uri($"https://{accountName}.blob.core.windows.net/{containerName}"), tokenCredential, default); }); return builder; } }