aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2023-06-22 21:13:35 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2023-06-22 21:13:35 -0400
commitce3b1dc1e88db23cfb99e135f7d57a08194560c7 (patch)
treef3b5c8db42c6800431e296487cb5b689aca5bc0d
parentb8499ea2f5e31dba0da10692cd1fd1bba83073bb (diff)
Extensions async updates
-rw-r--r--lib/Emails.Transactional.Extensions/src/TEmailConfig.cs37
-rw-r--r--lib/Emails.Transactional.Plugin/src/Api Endpoints/SendEndpoint.cs15
-rw-r--r--lib/Emails.Transactional.Plugin/src/Transactions/TransactionStore.cs2
3 files changed, 29 insertions, 25 deletions
diff --git a/lib/Emails.Transactional.Extensions/src/TEmailConfig.cs b/lib/Emails.Transactional.Extensions/src/TEmailConfig.cs
index 7905982..7642283 100644
--- a/lib/Emails.Transactional.Extensions/src/TEmailConfig.cs
+++ b/lib/Emails.Transactional.Extensions/src/TEmailConfig.cs
@@ -39,7 +39,7 @@ namespace Emails.Transactional.Client.Extensions
/// Provides transactional emails based on templates and variable complilation. Should be loaded as a singleton for a plugin
/// </summary>
[ConfigurationName(EMAIL_CONFIG_KEY)]
- public class TEmailConfig : TransactionalEmailConfig, IAsyncConfigurable, IDisposable
+ public class TEmailConfig : TransactionalEmailConfig, IDisposable
{
const string DEFAULT_USER_AGENT = "VN Transactional Email System";
@@ -58,17 +58,19 @@ namespace Emails.Transactional.Client.Extensions
/// <see cref="RestClient"/>
/// </summary>
public RestClientPool RestClientPool { get; }
+
/// <summary>
/// A global from email address name
/// </summary>
public string EmailFromName { get; }
+
/// <summary>
/// A global from email address
/// </summary>
public string EmailFromAddress { get; }
- private SecretResult? _clientId;
- private SecretResult? _clientSecret;
+ private readonly IAsyncLazy<string> _clientId;
+ private readonly IAsyncLazy<string> _clientSecret;
/// <summary>
@@ -116,22 +118,19 @@ namespace Emails.Transactional.Client.Extensions
//Init templates and service url
WithTemplates(templates)
.WithUrl(mailConfig.ServiceUri);
- }
- private Credential OAuthCredentialGet()
- {
- _ = _clientId ?? throw new InvalidOperationException("Oauth2 client id was not loaded");
- _ = _clientSecret ?? throw new InvalidOperationException("Oauth2 client secret was not loaded");
+ //Load oauth secrets
+ _clientId = plugin.GetSecretAsync("email_client_id")
+ .ToLazy(static s => s.Result.ToString());
- //Create new credential
- return Credential.Create(_clientId.Result, _clientSecret.Result);
+ _clientSecret = plugin.GetSecretAsync("email_client_secret")
+ .ToLazy(static s => s.Result.ToString());
}
- async Task IAsyncConfigurable.ConfigureServiceAsync(PluginBase plugin)
+ private Credential OAuthCredentialGet()
{
- //Load oauth secrets
- _clientId = await plugin.TryGetSecretAsync("email_client_id");
- _clientSecret = await plugin.TryGetSecretAsync("email_client_secret");
+ //Create new credential
+ return Credential.Create(_clientId.Value, _clientSecret.Value);
}
///<inheritdoc/>
@@ -141,12 +140,10 @@ namespace Emails.Transactional.Client.Extensions
{
if (disposing)
{
- _clientId?.Dispose();
- _clientSecret?.Dispose();
-
_authenticator.Dispose();
RestClientPool.Dispose();
}
+
disposedValue = true;
}
}
@@ -162,16 +159,22 @@ namespace Emails.Transactional.Client.Extensions
{
[JsonPropertyName("from_name")]
public string? EmailFromName { get; set; }
+
[JsonPropertyName("from_address")]
public string? EmailFromAddress { get; set; }
+
[JsonPropertyName("email_url")]
public string? ServiceUrl { get; set; }
+
[JsonPropertyName("auth_url")]
public string? AuthUrl { get; set; }
+
[JsonPropertyName("user_agent")]
public string UserAgent { get; set; } = DEFAULT_USER_AGENT;
+
[JsonPropertyName("timeout_ms")]
public int TimeoutMs { get; set; } = DEFAULT_CLIENT_TIMEOUT_MS;
+
[JsonPropertyName("max_clients")]
public int MaxClients { get; set; } = DEFAULT_MAX_CLIENTS;
diff --git a/lib/Emails.Transactional.Plugin/src/Api Endpoints/SendEndpoint.cs b/lib/Emails.Transactional.Plugin/src/Api Endpoints/SendEndpoint.cs
index e5ca88b..16c0d9a 100644
--- a/lib/Emails.Transactional.Plugin/src/Api Endpoints/SendEndpoint.cs
+++ b/lib/Emails.Transactional.Plugin/src/Api Endpoints/SendEndpoint.cs
@@ -112,9 +112,12 @@ namespace Emails.Transactional.Endpoints
//Load SMTP
_ = plugin.ObserveWork(async () =>
{
- using SecretResult? password = await plugin.TryGetSecretAsync("smtp_password") ?? throw new KeyNotFoundException("Missing required 'smtp_password' in secrets");
+ //Get the password from the secret store
+ string password = await plugin.GetSecretAsync("smtp_password").ToLazy(static r => r.Result.ToString());
+
//Copy the secre to the network credential
- NetworkCredential cred = new(username, password.Result.ToString());
+ NetworkCredential cred = new(username, password);
+
//Init email service
EmailService = new(serverUri, cred, timeout);
});
@@ -130,13 +133,11 @@ namespace Emails.Transactional.Endpoints
//Load the client when the secret finishes loading
_ = plugin.ObserveWork(async () =>
{
- using SecretResult? secret = await plugin.TryGetSecretAsync("s3_secret") ?? throw new KeyNotFoundException("Missing required s3 client secret in config");
-
+ string s3Secret = await plugin.GetSecretAsync("s3_secret").ToLazy(static r => r.Result.ToString());
Client.WithEndpoint(S3Config.ServerAddress)
- .WithCredentials(S3Config.ClientId, secret.Result.ToString());
-
- Client.WithSSL(S3Config.UseSsl.HasValue && S3Config.UseSsl.Value);
+ .WithCredentials(S3Config.ClientId, s3Secret)
+ .WithSSL(S3Config.UseSsl.HasValue && S3Config.UseSsl.Value);
//Accept optional region
if (!string.IsNullOrWhiteSpace(S3Config.Region))
diff --git a/lib/Emails.Transactional.Plugin/src/Transactions/TransactionStore.cs b/lib/Emails.Transactional.Plugin/src/Transactions/TransactionStore.cs
index a6bd4b9..47db7b1 100644
--- a/lib/Emails.Transactional.Plugin/src/Transactions/TransactionStore.cs
+++ b/lib/Emails.Transactional.Plugin/src/Transactions/TransactionStore.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2022 Vaughn Nugent
+* Copyright (c) 2023 Vaughn Nugent
*
* Library: VNLib
* Package: Transactional Emails