move dispatchers into their own folder+namespace
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
namespace Finch.Mails;
|
namespace Finch.Mails.Dispatchers;
|
||||||
|
|
||||||
public interface IMailDispatcher : IDisposable
|
public interface IMailDispatcher : IDisposable
|
||||||
{
|
{
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace Finch.Mails;
|
namespace Finch.Mails.Dispatchers;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Default implementation of an IMailSender which sends the mail to the attached logger
|
/// Default implementation of an IMailSender which sends the mail to the attached logger
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
namespace Finch.Mails.Dispatchers;
|
||||||
|
|
||||||
|
public class MailDispatcherResolver(IEnumerable<IMailDispatcher> dispatchers) : IMailDispatcherResolver
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Task<IMailDispatcher> Resolve()
|
||||||
|
{
|
||||||
|
IMailDispatcher dispatcher = dispatchers
|
||||||
|
.OrderByDescending(x => x.Priority)
|
||||||
|
.FirstOrDefault(dispatcher => dispatcher.CanSend());
|
||||||
|
|
||||||
|
return Task.FromResult(dispatcher);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public interface IMailDispatcherResolver
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Resolves a mail dispatcher to use for sending emails
|
||||||
|
/// </summary>
|
||||||
|
Task<IMailDispatcher> Resolve();
|
||||||
|
}
|
||||||
+2
-2
@@ -4,13 +4,13 @@ using Microsoft.Extensions.Options;
|
|||||||
using PostmarkDotNet;
|
using PostmarkDotNet;
|
||||||
using PostmarkDotNet.Model;
|
using PostmarkDotNet.Model;
|
||||||
|
|
||||||
namespace Finch.Mails;
|
namespace Finch.Mails.Dispatchers.Postmark;
|
||||||
|
|
||||||
public class PostmarkDispatcher : IMailDispatcher
|
public class PostmarkDispatcher : IMailDispatcher
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public int Priority { get; } = 3;
|
public int Priority { get; } = 3;
|
||||||
|
|
||||||
protected PostmarkClient Postmark { get; set; }
|
protected PostmarkClient Postmark { get; set; }
|
||||||
|
|
||||||
protected PostmarkAdminClient PostmarkAdmin { get; set; }
|
protected PostmarkAdminClient PostmarkAdmin { get; set; }
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
namespace Finch.Mails;
|
namespace Finch.Mails.Dispatchers.Postmark;
|
||||||
|
|
||||||
public class PostmarkOptions
|
public class PostmarkOptions
|
||||||
{
|
{
|
||||||
+2
-2
@@ -9,13 +9,13 @@ using Microsoft.Extensions.Logging;
|
|||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Attachment = System.Net.Mail.Attachment;
|
using Attachment = System.Net.Mail.Attachment;
|
||||||
|
|
||||||
namespace Finch.Mails.Scaleway;
|
namespace Finch.Mails.Dispatchers.Scaleway;
|
||||||
|
|
||||||
public class ScalewayDispatcher : IMailDispatcher
|
public class ScalewayDispatcher : IMailDispatcher
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public int Priority { get; } = 3;
|
public int Priority { get; } = 3;
|
||||||
|
|
||||||
protected Queue<Mail> Queue { get; } = new();
|
protected Queue<Mail> Queue { get; } = new();
|
||||||
|
|
||||||
protected MailOptions Options { get; set; }
|
protected MailOptions Options { get; set; }
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
namespace Finch.Mails.Scaleway;
|
namespace Finch.Mails.Dispatchers.Scaleway;
|
||||||
|
|
||||||
public class ScalewayOptions
|
public class ScalewayOptions
|
||||||
{
|
{
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace Finch.Mails.Scaleway;
|
namespace Finch.Mails.Dispatchers.Scaleway;
|
||||||
|
|
||||||
public class ScalewayRequest
|
public class ScalewayRequest
|
||||||
{
|
{
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
namespace Finch.Mails.Scaleway;
|
namespace Finch.Mails.Dispatchers.Scaleway;
|
||||||
|
|
||||||
public class ScalewayResponse
|
public class ScalewayResponse
|
||||||
{
|
{
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
using Finch.Mails.Scaleway;
|
using Finch.Mails.Dispatchers;
|
||||||
|
using Finch.Mails.Dispatchers.Postmark;
|
||||||
|
using Finch.Mails.Dispatchers.Scaleway;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
@@ -10,6 +12,7 @@ internal class FinchMailModule : FinchModule
|
|||||||
{
|
{
|
||||||
services.AddHttpClient<ScalewayDispatcher>().RemoveAllLoggers();
|
services.AddHttpClient<ScalewayDispatcher>().RemoveAllLoggers();
|
||||||
services.AddScoped<IMailProvider, MailProvider>();
|
services.AddScoped<IMailProvider, MailProvider>();
|
||||||
|
services.AddScoped<IMailDispatcherResolver, MailDispatcherResolver>();
|
||||||
services.AddScoped<IMailDispatcher, LoggerMailDispatcher>();
|
services.AddScoped<IMailDispatcher, LoggerMailDispatcher>();
|
||||||
services.AddScoped<IMailDispatcher, PostmarkDispatcher>();
|
services.AddScoped<IMailDispatcher, PostmarkDispatcher>();
|
||||||
services.AddScoped<IMailDispatcher, ScalewayDispatcher>();
|
services.AddScoped<IMailDispatcher, ScalewayDispatcher>();
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
using Microsoft.Extensions.Options;
|
|
||||||
|
|
||||||
namespace Finch.Mails;
|
|
||||||
|
|
||||||
public class MailDispatcherResolver(IEnumerable<IMailDispatcher> dispatchers, IOptionsMonitor<MailOptions> options)
|
|
||||||
{
|
|
||||||
/// <inheritdoc />
|
|
||||||
public IMailDispatcher Resolve()
|
|
||||||
{
|
|
||||||
return dispatchers
|
|
||||||
.OrderByDescending(x => x.Priority)
|
|
||||||
.FirstOrDefault(dispatcher => dispatcher.CanSend());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +1,10 @@
|
|||||||
namespace Finch.Mails;
|
using Finch.Mails.Dispatchers.Postmark;
|
||||||
|
using Finch.Mails.Dispatchers.Scaleway;
|
||||||
|
|
||||||
|
namespace Finch.Mails;
|
||||||
|
|
||||||
public class MailOptions
|
public class MailOptions
|
||||||
{
|
{
|
||||||
//public string Host { get; set; }
|
|
||||||
|
|
||||||
//public int Port { get; set; }
|
|
||||||
|
|
||||||
//public string Username { get; set; }
|
|
||||||
|
|
||||||
//public string Password { get; set; }
|
|
||||||
|
|
||||||
public string From { get; set; }
|
public string From { get; set; }
|
||||||
|
|
||||||
public string FromName { get; set; }
|
public string FromName { get; set; }
|
||||||
@@ -28,7 +23,7 @@ public class MailOptions
|
|||||||
|
|
||||||
public PostmarkOptions Postmark { get; set; }
|
public PostmarkOptions Postmark { get; set; }
|
||||||
|
|
||||||
public Scaleway.ScalewayOptions Scaleway { get; set; }
|
public ScalewayOptions Scaleway { get; set; }
|
||||||
|
|
||||||
public Func<Mail, string> BuildViewPath { get; set; }
|
public Func<Mail, string> BuildViewPath { get; set; }
|
||||||
}
|
}
|
||||||
@@ -2,17 +2,18 @@
|
|||||||
using System.Net.Mail;
|
using System.Net.Mail;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using Finch.Mails.Dispatchers;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
namespace Finch.Mails;
|
namespace Finch.Mails;
|
||||||
|
|
||||||
public class MailProvider(IFinchContext finch, IOptionsMonitor<MailOptions> mailOptions, ILogger<IMailProvider> logger, IEnumerable<IMailDispatcher> dispatchers, IRazorRenderer renderer) : IMailProvider
|
public class MailProvider(IFinchContext finch, IOptionsMonitor<MailOptions> mailOptions, ILogger<IMailProvider> logger, IMailDispatcherResolver dispatcherResolver, IRazorRenderer renderer) : IMailProvider
|
||||||
{
|
{
|
||||||
protected ILogger<IMailProvider> Logger { get; set; } = logger;
|
protected ILogger<IMailProvider> Logger { get; set; } = logger;
|
||||||
|
|
||||||
protected IFinchContext Finch { get; set; } = finch;
|
protected IFinchContext Finch { get; set; } = finch;
|
||||||
|
|
||||||
protected IEnumerable<IMailDispatcher> Dispatchers { get; set; } = dispatchers;
|
protected IMailDispatcherResolver DispatcherResolver { get; set; } = dispatcherResolver;
|
||||||
|
|
||||||
protected IRazorRenderer Renderer { get; set; } = renderer;
|
protected IRazorRenderer Renderer { get; set; } = renderer;
|
||||||
|
|
||||||
@@ -24,7 +25,8 @@ public class MailProvider(IFinchContext finch, IOptionsMonitor<MailOptions> mail
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public virtual async Task Send(Mail message, CancellationToken token = default)
|
public virtual async Task Send(Mail message, CancellationToken token = default)
|
||||||
{
|
{
|
||||||
await Send(message, GetDispatcher(), token);
|
IMailDispatcher dispatcher = await DispatcherResolver.Resolve();
|
||||||
|
await Send(message, dispatcher, token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -88,15 +90,6 @@ public class MailProvider(IFinchContext finch, IOptionsMonitor<MailOptions> mail
|
|||||||
message.IsRendered = true;
|
message.IsRendered = true;
|
||||||
return message.Body;
|
return message.Body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected IMailDispatcher GetDispatcher()
|
|
||||||
{
|
|
||||||
return Dispatchers
|
|
||||||
.OrderByDescending(x => x.Priority)
|
|
||||||
.FirstOrDefault(dispatcher => dispatcher.CanSend());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user