aboutsummaryrefslogtreecommitdiff
path: root/lib/Emails.Transactional.Client/src/ClientExtensions.cs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Emails.Transactional.Client/src/ClientExtensions.cs')
-rw-r--r--lib/Emails.Transactional.Client/src/ClientExtensions.cs79
1 files changed, 79 insertions, 0 deletions
diff --git a/lib/Emails.Transactional.Client/src/ClientExtensions.cs b/lib/Emails.Transactional.Client/src/ClientExtensions.cs
new file mode 100644
index 0000000..5c6fb12
--- /dev/null
+++ b/lib/Emails.Transactional.Client/src/ClientExtensions.cs
@@ -0,0 +1,79 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: Emails.Transactional.Client
+* File: ClientExtensions.cs
+*
+* ClientExtensions.cs is part of Emails.Transactional.Client which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* Emails.Transactional.Client is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published
+* by the Free Software Foundation, either version 2 of the License,
+* or (at your option) any later version.
+*
+* Emails.Transactional.Client 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
+* General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with Emails.Transactional.Client. If not, see http://www.gnu.org/licenses/.
+*/
+
+using System;
+
+using System.Net;
+using System.Threading;
+using System.Threading.Tasks;
+
+using Emails.Transactional.Client.Exceptions;
+
+using RestSharp;
+
+namespace Emails.Transactional.Client
+{
+ /// <summary>
+ /// Contains extension methods to send emails on remote transational email servers
+ /// </summary>
+ public static class ClientExtensions
+ {
+ private const Method DEFAULT_SEND_METHOD = Method.Post;
+
+ /// <summary>
+ /// Asynchronously begins an email transaction against the mail server with the specified
+ /// transaction request.
+ /// </summary>
+ /// <param name="client"></param>
+ /// <param name="transaction">The <see cref="EmailTransactionRequest"/> to submit</param>
+ /// <param name="token">A cancelaion token to cancel the operation</param>
+ /// <returns>A task that represents the async send operation</returns>
+ /// <exception cref="ValidationFailedException"></exception>
+ /// <exception cref="InvalidAuthorizationException"></exception>
+ /// <exception cref="InvalidTransactionRequestException"></exception>
+ /// <exception cref="InvalidTransactionResponseException"></exception>
+ public static async Task<TransactionResult?> SendEmailAsync(this RestClient client, EmailTransactionRequest transaction, CancellationToken token = default)
+ {
+ //Init the new request
+ RestRequest request = new(transaction.Endpoint, DEFAULT_SEND_METHOD)
+ {
+ //Json request
+ RequestFormat = DataFormat.Json
+ };
+ //add/serialze the transacion request
+ request.AddJsonBody(transaction);
+ //Exec the tranasction on the client
+ RestResponse<TransactionResult> response = await client.ExecuteAsync<TransactionResult>(request, token);
+ //parse the response body
+ return response.StatusCode switch
+ {
+ HttpStatusCode.Unauthorized or HttpStatusCode.Forbidden => throw new InvalidAuthorizationException("The server did not accept the current authorization", response),
+ HttpStatusCode.BadRequest => throw new InvalidTransactionRequestException(response),
+ HttpStatusCode.UnprocessableEntity => throw new ValidationFailedException(response.Data!),
+ HttpStatusCode.OK => response.Data,
+ _ => throw new InvalidTransactionResponseException("Unhandled status code", response),
+ };
+ }
+ }
+}