Skip to content

Commit

Permalink
Add support for message direction
Browse files Browse the repository at this point in the history
  • Loading branch information
jm-mailosaur committed Oct 13, 2022
1 parent e574fff commit 7841abf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
57 changes: 37 additions & 20 deletions Mailosaur/Operations/Messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Messages(HttpClient client) : base(client) { }
/// </param>
public Message Get(string server, SearchCriteria criteria = null, int timeout = 10000, DateTime? receivedAfter = null)
=> Task.Run(async () => await GetAsync(server, criteria, timeout, receivedAfter)).UnwrapException<Message>();

/// <summary>
/// Retrieve a message using search criteria
/// </summary>
Expand Down Expand Up @@ -65,7 +65,7 @@ public async Task<Message> GetAsync(string server, SearchCriteria criteria = nul

if (server.Length != 8)
throw new MailosaurException("Must provide a valid Server ID.", "invalid_request");

var result = await SearchAsync(server, criteria, 0, 1, timeout, receivedAfter);
return GetById(result.Items[0].Id);
}
Expand Down Expand Up @@ -143,8 +143,12 @@ public Task DeleteAsync(string id)
/// <param name='receivedAfter'>
/// Limits results to only messages received after this date/time.
/// </param>
public MessageListResult List(string server, int? page = default(int?), int? itemsPerPage = default(int?), DateTime? receivedAfter = null)
=> Task.Run(async () => await ListAsync(server, page, itemsPerPage, receivedAfter)).UnwrapException<MessageListResult>();
/// <param name='dir'>
/// Optionally limits results based on the direction (`Sent` or `Received`),
/// with the default being `Received`.
/// </param>
public MessageListResult List(string server, int? page = default(int?), int? itemsPerPage = default(int?), DateTime? receivedAfter = null, string dir = null)
=> Task.Run(async () => await ListAsync(server, page, itemsPerPage, receivedAfter, dir)).UnwrapException<MessageListResult>();

/// <summary>
/// List all messages
Expand All @@ -167,8 +171,12 @@ public Task DeleteAsync(string id)
/// <param name='receivedAfter'>
/// Limits results to only messages received after this date/time.
/// </param>
public Task<MessageListResult> ListAsync(string server, int? page = default(int?), int? itemsPerPage = default(int?), DateTime? receivedAfter = null)
=> ExecuteRequest<MessageListResult>(HttpMethod.Get, PagePath($"api/messages?server={server}", page, itemsPerPage, receivedAfter));
/// <param name='dir'>
/// Optionally limits results based on the direction (`Sent` or `Received`),
/// with the default being `Received`.
/// </param>
public Task<MessageListResult> ListAsync(string server, int? page = default(int?), int? itemsPerPage = default(int?), DateTime? receivedAfter = null, string dir = null)
=> ExecuteRequest<MessageListResult>(HttpMethod.Get, PagePath($"api/messages?server={server}", page, itemsPerPage, receivedAfter, dir));

/// <summary>
/// Delete all messages
Expand All @@ -195,7 +203,7 @@ public void DeleteAll(string server)
/// <param name='server'>
/// The identifier of the server to be emptied.
/// </param>
public Task DeleteAllAsync(string server)
public Task DeleteAllAsync(string server)
=> ExecuteRequest(HttpMethod.Delete, $"api/messages?server={server}");

/// <summary>
Expand Down Expand Up @@ -229,8 +237,12 @@ public Task DeleteAllAsync(string server)
/// When set to false, an error will not be throw if timeout is reached
/// (default: true).
/// </param>
public MessageListResult Search(string server, SearchCriteria criteria, int? page = null, int? itemsPerPage = null, int? timeout = null, DateTime? receivedAfter = null, bool errorOnTimeout = true)
=> Task.Run(async () => await SearchAsync(server, criteria, page, itemsPerPage, timeout, receivedAfter, errorOnTimeout)).UnwrapException<MessageListResult>();
/// <param name='dir'>
/// Optionally limits results based on the direction (`Sent` or `Received`),
/// with the default being `Received`.
/// </param>
public MessageListResult Search(string server, SearchCriteria criteria, int? page = null, int? itemsPerPage = null, int? timeout = null, DateTime? receivedAfter = null, bool errorOnTimeout = true, string dir = null)
=> Task.Run(async () => await SearchAsync(server, criteria, page, itemsPerPage, timeout, receivedAfter, errorOnTimeout, dir)).UnwrapException<MessageListResult>();

/// <summary>
/// Search for messages
Expand Down Expand Up @@ -263,21 +275,25 @@ public MessageListResult Search(string server, SearchCriteria criteria, int? pag
/// When set to false, an error will not be throw if timeout is reached
/// (default: true).
/// </param>
public async Task<MessageListResult> SearchAsync(string server, SearchCriteria criteria, int? page = null, int? itemsPerPage = null, int? timeout = null, DateTime? receivedAfter = null, bool errorOnTimeout = true)
/// <param name='dir'>
/// Optionally limits results based on the direction (`Sent` or `Received`),
/// with the default being `Received`.
/// </param>
public async Task<MessageListResult> SearchAsync(string server, SearchCriteria criteria, int? page = null, int? itemsPerPage = null, int? timeout = null, DateTime? receivedAfter = null, bool errorOnTimeout = true, string dir = null)
{
var pollCount = 0;
var startTime = DateTime.UtcNow;

while(true)
while (true)
{
var result = await ExecuteRequest<MessageListResultWithHeaders>(HttpMethod.Post, PagePath($"api/messages/search?server={server}", page, itemsPerPage, receivedAfter), criteria);
var result = await ExecuteRequest<MessageListResultWithHeaders>(HttpMethod.Post, PagePath($"api/messages/search?server={server}", page, itemsPerPage, receivedAfter, dir), criteria);

if (timeout == null || timeout == 0 || result.MessageListResult.Items.Count != 0)
return result.MessageListResult;

var delayPattern = (string.IsNullOrWhiteSpace(result.DelayHeader) ? "1000" : result.DelayHeader)
.Split(',').Select(x => Int32.Parse(x)).ToArray();

var delay = pollCount >= delayPattern.Length ?
delayPattern[delayPattern.Length - 1] :
delayPattern[pollCount];
Expand All @@ -287,13 +303,14 @@ public async Task<MessageListResult> SearchAsync(string server, SearchCriteria c
// Stop if timeout will be exceeded
if (((int)(DateTime.UtcNow - startTime).TotalMilliseconds) + delay > timeout)
{
if (errorOnTimeout == false) {
if (errorOnTimeout == false)
{
return result.MessageListResult;
}

throw new MailosaurException("No matching messages found in time. By default, only messages received in the last hour are checked (use receivedAfter to override this).", "search_timeout");
}

Task.Delay(delay).Wait();
}
}
Expand Down Expand Up @@ -341,7 +358,7 @@ public Message Create(string server, MessageCreateOptions messageCreateOptions)
/// <return>
/// A response object containing the response body and response headers.
/// </return>
public Task<Message> CreateAsync(string server, MessageCreateOptions messageCreateOptions)
public Task<Message> CreateAsync(string server, MessageCreateOptions messageCreateOptions)
=> ExecuteRequest<Message>(HttpMethod.Post, $"api/messages?server={server}", messageCreateOptions);

/// <summary>
Expand Down Expand Up @@ -383,9 +400,9 @@ public Message Forward(string id, MessageForwardOptions messageForwardOptions)
/// <return>
/// A response object containing the response body and response headers.
/// </return>
public Task<Message> ForwardAsync(string id, MessageForwardOptions messageForwardOptions)
public Task<Message> ForwardAsync(string id, MessageForwardOptions messageForwardOptions)
=> ExecuteRequest<Message>(HttpMethod.Post, $"api/messages/{id}/forward", messageForwardOptions);

/// <summary>
/// Reply to an email
/// </summary>
Expand Down Expand Up @@ -427,7 +444,7 @@ public Message Reply(string id, MessageReplyOptions messageReplyOptions)
/// <return>
/// A response object containing the response body and response headers.
/// </return>
public Task<Message> ReplyAsync(string id, MessageReplyOptions messageReplyOptions)
public Task<Message> ReplyAsync(string id, MessageReplyOptions messageReplyOptions)
=> ExecuteRequest<Message>(HttpMethod.Post, $"api/messages/{id}/reply", messageReplyOptions);
}
}
3 changes: 2 additions & 1 deletion Mailosaur/Operations/OperationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public T HandleAggregateException<T>(Func<T> requestMethod)
}
}

public string PagePath(string path, int? page = null, int? itemsPerPage = null, DateTime? receivedAfter = null)
public string PagePath(string path, int? page = null, int? itemsPerPage = null, DateTime? receivedAfter = null, string dir = null)
{
string isoReceivedAfter = null;
if (receivedAfter != null)
Expand All @@ -133,6 +133,7 @@ public string PagePath(string path, int? page = null, int? itemsPerPage = null,
path += page != null ? $"&page={page}" : "";
path += itemsPerPage != null ? $"&itemsPerPage={itemsPerPage}" : "";
path += receivedAfter != null ? $"&receivedAfter={isoReceivedAfter}" : "";
path += dir != null ? $"&dir={dir}" : "";
return path;
}

Expand Down

0 comments on commit 7841abf

Please sign in to comment.