using TriunniaLabs.Arch.Api.Email.Client;
using TriunniaLabs.Arch.Microservice;
using TriunniaLabs.Memory.Protected;
namespace TriunniaLabs.Arch.Sample.Server
{
///
/// A controller that shows how to work the Email Api.
///
public class EmailApiSampleController : ArchControllerBase
{
// Services
private readonly IEmailApiClient _emailApiClient;
///
/// Constructor.
///
/// Reference to the microservice context.
/// Reference to the Tokens Api client.
public EmailApiSampleController(IMicroserviceContext microserviceContext, IEmailApiClient emailApiClient) : base(microserviceContext)
{
_emailApiClient = emailApiClient ?? throw new ArgumentNullException(nameof(emailApiClient));
}
///
/// A sample method that shows how to work with the Email Api.
///
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 { });
}
}
}