Skip to content

Commit

Permalink
Merge pull request #79 from wildbit/task/add-message-stream-to-sendin…
Browse files Browse the repository at this point in the history
…g-api

Task/add message stream to sending api
  • Loading branch information
vladsandu committed Mar 24, 2020
2 parents f4f47e6 + 6832111 commit d2060e0
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
20 changes: 20 additions & 0 deletions src/Postmark.Tests/ClientSendingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PostmarkDotNet.Exceptions;

namespace Postmark.Tests
{
Expand Down Expand Up @@ -89,6 +90,25 @@ public async void Client_CanSendAPostmarkMessageWithEmptyTrackLinks()
Assert.NotEqual(Guid.Empty, result.MessageID);
}

[Fact]
public async void UnknownMessageStream_ThrowsException()
{
var inboundAddress = (await _client.GetServerAsync()).InboundAddress;
var message = ConstructMessage(inboundAddress, 0, null);
message.MessageStream = "outbound";

// Testing the default transactional stream
var result = await _client.SendMessageAsync(message);
Assert.Equal(PostmarkStatus.Success, result.Status);
Assert.Equal(0, result.ErrorCode);
Assert.NotEqual(Guid.Empty, result.MessageID);

// Testing an invalid non-existing stream
message.MessageStream = "unknown-stream";

await Assert.ThrowsAsync<PostmarkValidationException>(() => _client.SendMessageAsync(message));
}

private PostmarkMessage ConstructMessage(string inboundAddress, int number = 0, LinkTrackingOptions? trackLinks = LinkTrackingOptions.HtmlAndText)
{
var message = new PostmarkMessage()
Expand Down
5 changes: 3 additions & 2 deletions src/Postmark/Model/PostmarkMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public PostmarkMessage()
/// <param name="htmlBody">The HTML Body to be used for the message, this may be null if TextBody is set.</param>
/// <param name = "headers">A collection of additional mail headers to send with the message. (optional)</param>
/// <param name = "metadata">A dictionary of metadata to send with the message. (optional)</param>
/// <param name="messageStream">The message stream used to send this message. If not provided, the default transactional stream "outbound" will be used. (optional)</param>
public PostmarkMessage(string from, string to, string subject, string textBody, string htmlBody,
HeaderCollection headers = null,
IDictionary<string, string> metadata = null): base()
HeaderCollection headers = null, IDictionary<string, string> metadata = null, string messageStream = null) : base()
{
From = from;
To = to;
Expand All @@ -33,6 +33,7 @@ public PostmarkMessage(string from, string to, string subject, string textBody,
HtmlBody = htmlBody;
Headers = headers ?? new HeaderCollection();
Metadata = metadata;
MessageStream = messageStream;
}

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions src/Postmark/Model/PostmarkMessageBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public PostmarkMessageBase()
TrackLinks = LinkTrackingOptions.None;
}

/// <summary>
/// The message stream used to send this message.
/// </summary>
public string MessageStream { get; set; }

/// <summary>
/// The sender's email address.
/// </summary>
Expand Down
8 changes: 6 additions & 2 deletions src/Postmark/PostmarkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -863,10 +863,11 @@ public async Task<PostmarkResponse> SendEmailWithTemplateAsync<T>(string templat
bool? trackOpens = null,
IDictionary<string, string> headers = null,
IDictionary<string, string> metadata = null,
string messageStream = null,
params PostmarkMessageAttachment[] attachments)
{
return await InternalSendEmailWithTemplateAsync(templateAlias, templateModel, to, from, inlineCss, cc,
bcc, replyTo, trackOpens, headers, metadata, attachments);
bcc, replyTo, trackOpens, headers, metadata, messageStream, attachments);
}

public async Task<PostmarkResponse> SendEmailWithTemplateAsync<T>(long templateId, T templateModel,
Expand All @@ -876,10 +877,11 @@ public async Task<PostmarkResponse> SendEmailWithTemplateAsync<T>(long templateI
bool? trackOpens = null,
IDictionary<string, string> headers = null,
IDictionary<string, string> metadata = null,
string messageStream = null,
params PostmarkMessageAttachment[] attachments)
{
return await InternalSendEmailWithTemplateAsync(templateId, templateModel, to, from, inlineCss, cc,
bcc, replyTo, trackOpens, headers, metadata, attachments);
bcc, replyTo, trackOpens, headers, metadata, messageStream, attachments);
}

private async Task<PostmarkResponse> InternalSendEmailWithTemplateAsync<T>(object templateReference, T templateModel,
Expand All @@ -889,6 +891,7 @@ private async Task<PostmarkResponse> InternalSendEmailWithTemplateAsync<T>(objec
bool? trackOpens = null,
IDictionary<string, string> headers = null,
IDictionary<string, string> metadata = null,
string messageStream = null,
params PostmarkMessageAttachment[] attachments)
{

Expand All @@ -902,6 +905,7 @@ private async Task<PostmarkResponse> InternalSendEmailWithTemplateAsync<T>(objec
email.TemplateAlias = (string)templateReference;
}
email.TemplateModel = templateModel;
email.MessageStream = messageStream;
email.To = to;
email.From = from;
if (inlineCss.HasValue)
Expand Down
11 changes: 6 additions & 5 deletions src/Postmark/PostmarkClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace PostmarkDotNet
/// </summary>
public static class PostmarkClientExtensions
{

/// <summary>
/// Sends a message through the Postmark API.
/// All email addresses must be valid, and the sender must be
Expand All @@ -25,14 +24,16 @@ public static class PostmarkClientExtensions
/// <param name="textBody">The Plain Text Body to be used for the message, this may be null if HtmlBody is set.</param>
/// <param name="htmlBody">The HTML Body to be used for the message, this may be null if TextBody is set.</param>
/// <param name="headers">A collection of additional mail headers to send with the message.</param>
/// <param name="messageStream">The message stream used to send this message.</param>
/// <returns>A <see cref = "PostmarkResponse" /> with details about the transaction.</returns>
public static async Task<PostmarkResponse> SendMessageAsync(this PostmarkClient client,
string from, string to, string subject, string textBody, string htmlBody,
IDictionary<string, string> headers = null,
IDictionary<string, string> metadata = null)
IDictionary<string, string> headers = null,
IDictionary<string, string> metadata = null,
string messageStream = null)
{
var message = new PostmarkMessage(from, to, subject, textBody, htmlBody,
new HeaderCollection(headers), metadata);
var message = new PostmarkMessage(from, to, subject, textBody, htmlBody,
new HeaderCollection(headers), metadata, messageStream);
return await client.SendMessageAsync(message);
}

Expand Down

0 comments on commit d2060e0

Please sign in to comment.