using Microsoft.Extensions.Logging; using Dapr.Client; using MaksIT.Results; using MaksIT.Core.Extensions; namespace MaksIT.Dapr.Services; /// /// Dapr secret store operations with outcomes. /// public interface IDaprSecretService { /// /// Gets a secret by name. /// Task>> GetAsync( string storeName, string key, IReadOnlyDictionary? metadata = null, CancellationToken cancellationToken = default); /// /// Gets all secrets from a store. /// Task>>> GetBulkAsync( string storeName, IReadOnlyDictionary? metadata = null, CancellationToken cancellationToken = default); } /// /// Default . /// public class DaprSecretService( ILogger logger, DaprClient client ) : IDaprSecretService { private const string ErrorMessage = "MaksIT.Dapr - Secret error"; /// public async Task>> GetAsync( string storeName, string key, IReadOnlyDictionary? metadata = null, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(storeName) || string.IsNullOrWhiteSpace(key)) return Result>.BadRequest(default!, "storeName and key are required."); try { var secrets = await client.GetSecretAsync(storeName, key, metadata, cancellationToken); return Result>.Ok(secrets); } catch (OperationCanceledException) { throw; } catch (Exception ex) { logger.LogError(ex, ErrorMessage); return Result>.InternalServerError(default!, [ErrorMessage, .. ex.ExtractMessages()]); } } /// public async Task>>> GetBulkAsync( string storeName, IReadOnlyDictionary? metadata = null, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(storeName)) return Result>>.BadRequest(default!, "storeName is required."); try { var secrets = await client.GetBulkSecretAsync(storeName, metadata, cancellationToken); return Result>>.Ok(secrets); } catch (OperationCanceledException) { throw; } catch (Exception ex) { logger.LogError(ex, ErrorMessage); return Result>>.InternalServerError(default!, [ErrorMessage, .. ex.ExtractMessages()]); } } }