aboutsummaryrefslogtreecommitdiff
path: root/lib/Emails.Transactional.Extensions/src
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Emails.Transactional.Extensions/src')
-rw-r--r--lib/Emails.Transactional.Extensions/src/Emails.Transactional.Client.Extensions.csproj42
-rw-r--r--lib/Emails.Transactional.Extensions/src/TEmailConfig.cs207
-rw-r--r--lib/Emails.Transactional.Extensions/src/TransactionalEmailExtensions.cs53
3 files changed, 0 insertions, 302 deletions
diff --git a/lib/Emails.Transactional.Extensions/src/Emails.Transactional.Client.Extensions.csproj b/lib/Emails.Transactional.Extensions/src/Emails.Transactional.Client.Extensions.csproj
deleted file mode 100644
index be0104b..0000000
--- a/lib/Emails.Transactional.Extensions/src/Emails.Transactional.Client.Extensions.csproj
+++ /dev/null
@@ -1,42 +0,0 @@
-<Project Sdk="Microsoft.NET.Sdk">
-
- <PropertyGroup>
- <ImplicitUsings>enable</ImplicitUsings>
- <TargetFramework>net6.0</TargetFramework>
- <RootNamespace>Emails.Transactional.Client.Extensions</RootNamespace>
- <AssemblyName>Emails.Transactional.Client.Extensions</AssemblyName>
- <NeutralLanguage>en-US</NeutralLanguage>
- <Nullable>enable</Nullable>
- <GenerateDocumentationFile>True</GenerateDocumentationFile>
- <AnalysisLevel>latest-all</AnalysisLevel>
- </PropertyGroup>
-
- <PropertyGroup>
- <PackageId>Emails.Transactional.Client.Extensions</PackageId>
- <Authors>Vaughn Nugent</Authors>
- <Company>Vaughn Nugent</Company>
- <Product>VNLib HTTP Library</Product>
- <Description>
- An extension library for building transactional email configurations to send programmable emails against a transactional email server
- </Description>
- <Copyright>Copyright © 2023 Vaughn Nugent</Copyright>
- <PackageProjectUrl>https://www.vaughnnugent.com/resources/software/modules/Emails.Transactional</PackageProjectUrl>
- <RepositoryUrl>https://github.com/VnUgE/Emails.Transactional/tree/master/lib/Emails.Transactional.Extensions</RepositoryUrl>
- </PropertyGroup>
-
-
- <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
- <Deterministic>False</Deterministic>
- </PropertyGroup>
-
- <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
- <Deterministic>False</Deterministic>
- </PropertyGroup>
-
- <ItemGroup>
- <ProjectReference Include="..\..\..\..\..\core\lib\Net.Rest.Client\src\VNLib.Net.Rest.Client.csproj" />
- <ProjectReference Include="..\..\..\..\Extensions\lib\VNLib.Plugins.Extensions.Loading\src\VNLib.Plugins.Extensions.Loading.csproj" />
- <ProjectReference Include="..\..\Emails.Transactional.Client\src\Emails.Transactional.Client.csproj" />
- </ItemGroup>
-
-</Project>
diff --git a/lib/Emails.Transactional.Extensions/src/TEmailConfig.cs b/lib/Emails.Transactional.Extensions/src/TEmailConfig.cs
deleted file mode 100644
index 7642283..0000000
--- a/lib/Emails.Transactional.Extensions/src/TEmailConfig.cs
+++ /dev/null
@@ -1,207 +0,0 @@
-/*
-* Copyright (c) 2023 Vaughn Nugent
-*
-* Library: VNLib
-* Package: Emails.Transactional.Client.Extensions
-* File: TEmailConfig.cs
-*
-* TEmailConfig.cs is part of Emails.Transactional.Client.Extensions which is part of the larger
-* VNLib collection of libraries and utilities.
-*
-* Emails.Transactional.Client.Extensions is free software: you can redistribute it and/or modify
-* it under the terms of the GNU Affero General Public License as
-* published by the Free Software Foundation, either version 3 of the
-* License, or (at your option) any later version.
-*
-* Emails.Transactional.Client.Extensions is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU Affero General Public License for more details.
-*
-* You should have received a copy of the GNU Affero General Public License
-* along with this program. If not, see https://www.gnu.org/licenses/.
-*/
-
-using System.Text;
-using System.Text.Json.Serialization;
-
-using RestSharp;
-
-using VNLib.Utils.Logging;
-using VNLib.Plugins;
-using VNLib.Net.Rest.Client;
-using VNLib.Net.Rest.Client.OAuth2;
-using VNLib.Plugins.Extensions.Loading;
-
-namespace Emails.Transactional.Client.Extensions
-{
- /// <summary>
- /// 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, IDisposable
- {
- const string DEFAULT_USER_AGENT = "VN Transactional Email System";
-
- public const string EMAIL_CONFIG_KEY = "emails";
- public const string REQUIRED_EMAIL_TEMPALTE_CONFIG_KEY = "required_email_templates";
-
- public const int DEFAULT_MAX_CLIENTS = 5;
- public const int DEFAULT_CLIENT_TIMEOUT_MS = 10000;
-
- private bool disposedValue;
-
- private readonly OAuth2Authenticator _authenticator;
-
- /// <summary>
- /// A shared <see cref="VNLib.Net.Rest.Client.RestClientPool"/> for renting configuraed
- /// <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 readonly IAsyncLazy<string> _clientId;
- private readonly IAsyncLazy<string> _clientSecret;
-
-
- /// <summary>
- /// Intialzies the singleton using the <see cref="LoadingExtensions"/>
- /// loading system
- /// </summary>
- /// <param name="plugin">The plugin loading the instance</param>
- /// <param name="config">The configuration scope for the instance</param>
- public TEmailConfig(PluginBase plugin, IConfigScope config)
- {
- //The current config scope should be the required data, so we can deserialze it
- JsonConfig mailConfig = config.DeserialzeAndValidate<JsonConfig>();
-
- //Get required templates
- Dictionary<string, string> templates = plugin.GetConfig(REQUIRED_EMAIL_TEMPALTE_CONFIG_KEY).Deserialze<Dictionary<string, string>>();
-
- EmailFromName = mailConfig.EmailFromName!;
- EmailFromAddress = mailConfig.EmailFromAddress!;
-
- if (plugin.IsDebug())
- {
- plugin.Log.Debug("Required email templates {t}", templates);
- }
-
- //Options for auth token endpoint
- RestClientOptions clientOptions = new()
- {
- AllowMultipleDefaultParametersWithSameName = true,
- //Server supports compression
- AutomaticDecompression = System.Net.DecompressionMethods.All,
- PreAuthenticate = false,
- Encoding = Encoding.UTF8,
- MaxTimeout = mailConfig.TimeoutMs,
- UserAgent = mailConfig.UserAgent,
- //Server should not redirect
- FollowRedirects = false
- };
-
- //Init Oauth authenticator
- _authenticator = new(clientOptions, OAuthCredentialGet, mailConfig.TokenServerUri.ToString());
-
- //Create client pool
- RestClientPool = new(mailConfig.MaxClients, clientOptions, authenticator: _authenticator);
-
- //Init templates and service url
- WithTemplates(templates)
- .WithUrl(mailConfig.ServiceUri);
-
- //Load oauth secrets
- _clientId = plugin.GetSecretAsync("email_client_id")
- .ToLazy(static s => s.Result.ToString());
-
- _clientSecret = plugin.GetSecretAsync("email_client_secret")
- .ToLazy(static s => s.Result.ToString());
- }
-
- private Credential OAuthCredentialGet()
- {
- //Create new credential
- return Credential.Create(_clientId.Value, _clientSecret.Value);
- }
-
- ///<inheritdoc/>
- protected virtual void Dispose(bool disposing)
- {
- if (!disposedValue)
- {
- if (disposing)
- {
- _authenticator.Dispose();
- RestClientPool.Dispose();
- }
-
- disposedValue = true;
- }
- }
-
- void IDisposable.Dispose()
- {
- // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
- Dispose(disposing: true);
- GC.SuppressFinalize(this);
- }
-
- private sealed class JsonConfig : IOnConfigValidation
- {
- [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;
-
-
- [JsonIgnore]
- public Uri ServiceUri => new(ServiceUrl!);
-
- [JsonIgnore]
- public Uri TokenServerUri => new(AuthUrl!);
-
- void IOnConfigValidation.Validate()
- {
- _ = EmailFromName ?? throw new KeyNotFoundException("Missing required mail configuration key 'from_name'");
- _ = EmailFromAddress ?? throw new KeyNotFoundException("Missing required mail configuration key 'from_address'");
- _ = ServiceUri ?? throw new KeyNotFoundException("Missing required mail configuration key 'email_url'");
- _ = AuthUrl ?? throw new KeyNotFoundException("Missing required mail configuration key 'auth_url'");
-
- if (MaxClients < 1)
- {
- throw new ArgumentOutOfRangeException("max_client", "Max clients config variable must be a non-zero positive integer");
- }
-
- if (TimeoutMs < 100)
- {
- throw new ArgumentOutOfRangeException("timeout_ms", "Client timeout should be a value greater than 100ms");
- }
- }
- }
- }
-} \ No newline at end of file
diff --git a/lib/Emails.Transactional.Extensions/src/TransactionalEmailExtensions.cs b/lib/Emails.Transactional.Extensions/src/TransactionalEmailExtensions.cs
deleted file mode 100644
index e6cf3ed..0000000
--- a/lib/Emails.Transactional.Extensions/src/TransactionalEmailExtensions.cs
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
-* Copyright (c) 2023 Vaughn Nugent
-*
-* Library: VNLib
-* Package: Emails.Transactional.Client.Extensions
-* File: TransactionalEmailExtensions.cs
-*
-* TransactionalEmailExtensions.cs is part of Emails.Transactional.Client.Extensions which
-* is part of the larger VNLib collection of libraries and utilities.
-*
-* Emails.Transactional.Client.Extensions is free software: you can redistribute it and/or modify
-* it under the terms of the GNU Affero General Public License as
-* published by the Free Software Foundation, either version 3 of the
-* License, or (at your option) any later version.
-*
-* Emails.Transactional.Client.Extensions is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU Affero General Public License for more details.
-*
-* You should have received a copy of the GNU Affero General Public License
-* along with this program. If not, see https://www.gnu.org/licenses/.
-*/
-
-
-using VNLib.Net.Rest.Client;
-
-
-namespace Emails.Transactional.Client.Extensions
-{
-
- /// <summary>
- /// Contains extension methods for implementing templated
- /// transactional emails
- /// </summary>
- public static class TransactionalEmailExtensions
- {
-
- /// <summary>
- /// Sends an <see cref="EmailTransactionRequest"/> on the current configuration resource pool
- /// </summary>
- /// <param name="config"></param>
- /// <param name="request">The <see cref="EmailTransactionRequest"/> request to send to the server</param>
- /// <returns>A task the resolves the <see cref="TransactionResult"/> of the request</returns>
- public static async Task<TransactionResult> SendEmailAsync(this TransactionalEmailConfig config, EmailTransactionRequest request)
- {
- //Get a new client contract from the configuration's pool assuming its a EmailSystemConfig class
- using ClientContract client = ((TEmailConfig)config).RestClientPool.Lease();
- //Send the email and await the result before releasing the client
- return await client.Resource.SendEmailAsync(request);
- }
- }
-} \ No newline at end of file