Files
TriunniaLabs.Arch.Sample/src/TriunniaLabs.Arch.Sample.Server/Controllers/EmailApiSampleController.cs
T
2026-05-23 18:07:01 -04:00

59 lines
3.1 KiB
C#

using TriunniaLabs.Arch.Api.Email.Client;
using TriunniaLabs.Arch.Microservice;
using TriunniaLabs.Memory.Protected;
namespace TriunniaLabs.Arch.Sample.Server
{
/// <summary>
/// A controller that shows how to work the Email Api.
/// </summary>
public class EmailApiSampleController : ArchControllerBase
{
// Services
private readonly IEmailApiClient _emailApiClient;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="microserviceContext">Reference to the microservice context.</param>
/// <param name="emailApiClient">Reference to the Tokens Api client.</param>
public EmailApiSampleController(IMicroserviceContext microserviceContext, IEmailApiClient emailApiClient) : base(microserviceContext)
{
_emailApiClient = emailApiClient ?? throw new ArgumentNullException(nameof(emailApiClient));
}
/// <summary>
/// A sample method that shows how to work with the Email Api.
/// </summary>
internal async Task SampleAsync()
{
var encryptionKey = "SampleEncryptionKey".AsProtected(); // Per-row encryption key for the email address
var emailAddress = "sample@email.address".AsProtected(encryptionKey.GetDecryptedValue()); // Per-row encrypted email address
var ipAddress = IpAddress.AsProtected(); // User's ip address
var verifyToken = Guid.NewGuid().AsProtected(); // A verify token from the verification email
// Create a new email address in the system
var emailId = await _emailApiClient.CreateEmailAddressAsync(emailAddress, encryptionKey);
// Delete the specified email address
await _emailApiClient.DeleteEmailAddressAsync(emailId);
// Check whether the specified email address already exists in the system
var emailExists = _emailApiClient.HasEmailAddressAsync(emailAddress.Hash);
// Get the email address and associated metadata from the Email Api
var emailResponse = await _emailApiClient.GetEmailAddressAsync(emailAddress.Hash, encryptionKey); // Get by hash
emailResponse = await _emailApiClient.GetEmailAddressAsync(emailId, encryptionKey); // Get by Id
// Email verification procedure. This will send an email to the user with the verify token. Record its verification or abuse with the other methods.
await _emailApiClient.InitEmailVerifyAsync(emailId, encryptionKey, ipAddress);
var result = await _emailApiClient.CommitEmailVerifyAsync(verifyToken, ipAddress);
result = await _emailApiClient.ReportAbuseAsync(verifyToken);
// When using Postmark, receive their webhooks and forward to the Email Api with this call.
// The Email Api will take care of ban and bounce handling.
await _emailApiClient.RelayPostmarkWebhookAsync(new PostmarkWebhookRequest { });
}
}
}