From 7841abfe7631c9ebf4c3b0f8fecd287aae34557f Mon Sep 17 00:00:00 2001 From: Jon M Date: Thu, 13 Oct 2022 11:17:15 +0100 Subject: [PATCH] Add support for message direction --- Mailosaur/Operations/Messages.cs | 57 +++++++++++++++++---------- Mailosaur/Operations/OperationBase.cs | 3 +- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/Mailosaur/Operations/Messages.cs b/Mailosaur/Operations/Messages.cs index 2b1a9d9..3a0ef16 100644 --- a/Mailosaur/Operations/Messages.cs +++ b/Mailosaur/Operations/Messages.cs @@ -37,7 +37,7 @@ public Messages(HttpClient client) : base(client) { } /// public Message Get(string server, SearchCriteria criteria = null, int timeout = 10000, DateTime? receivedAfter = null) => Task.Run(async () => await GetAsync(server, criteria, timeout, receivedAfter)).UnwrapException(); - + /// /// Retrieve a message using search criteria /// @@ -65,7 +65,7 @@ public async Task 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); } @@ -143,8 +143,12 @@ public Task DeleteAsync(string id) /// /// Limits results to only messages received after this date/time. /// - 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(); + /// + /// Optionally limits results based on the direction (`Sent` or `Received`), + /// with the default being `Received`. + /// + 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(); /// /// List all messages @@ -167,8 +171,12 @@ public Task DeleteAsync(string id) /// /// Limits results to only messages received after this date/time. /// - public Task ListAsync(string server, int? page = default(int?), int? itemsPerPage = default(int?), DateTime? receivedAfter = null) - => ExecuteRequest(HttpMethod.Get, PagePath($"api/messages?server={server}", page, itemsPerPage, receivedAfter)); + /// + /// Optionally limits results based on the direction (`Sent` or `Received`), + /// with the default being `Received`. + /// + public Task ListAsync(string server, int? page = default(int?), int? itemsPerPage = default(int?), DateTime? receivedAfter = null, string dir = null) + => ExecuteRequest(HttpMethod.Get, PagePath($"api/messages?server={server}", page, itemsPerPage, receivedAfter, dir)); /// /// Delete all messages @@ -195,7 +203,7 @@ public void DeleteAll(string server) /// /// The identifier of the server to be emptied. /// - public Task DeleteAllAsync(string server) + public Task DeleteAllAsync(string server) => ExecuteRequest(HttpMethod.Delete, $"api/messages?server={server}"); /// @@ -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). /// - 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(); + /// + /// Optionally limits results based on the direction (`Sent` or `Received`), + /// with the default being `Received`. + /// + 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(); /// /// Search for messages @@ -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). /// - public async Task SearchAsync(string server, SearchCriteria criteria, int? page = null, int? itemsPerPage = null, int? timeout = null, DateTime? receivedAfter = null, bool errorOnTimeout = true) + /// + /// Optionally limits results based on the direction (`Sent` or `Received`), + /// with the default being `Received`. + /// + public async Task 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(HttpMethod.Post, PagePath($"api/messages/search?server={server}", page, itemsPerPage, receivedAfter), criteria); + var result = await ExecuteRequest(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]; @@ -287,13 +303,14 @@ public async Task 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(); } } @@ -341,7 +358,7 @@ public Message Create(string server, MessageCreateOptions messageCreateOptions) /// /// A response object containing the response body and response headers. /// - public Task CreateAsync(string server, MessageCreateOptions messageCreateOptions) + public Task CreateAsync(string server, MessageCreateOptions messageCreateOptions) => ExecuteRequest(HttpMethod.Post, $"api/messages?server={server}", messageCreateOptions); /// @@ -383,9 +400,9 @@ public Message Forward(string id, MessageForwardOptions messageForwardOptions) /// /// A response object containing the response body and response headers. /// - public Task ForwardAsync(string id, MessageForwardOptions messageForwardOptions) + public Task ForwardAsync(string id, MessageForwardOptions messageForwardOptions) => ExecuteRequest(HttpMethod.Post, $"api/messages/{id}/forward", messageForwardOptions); - + /// /// Reply to an email /// @@ -427,7 +444,7 @@ public Message Reply(string id, MessageReplyOptions messageReplyOptions) /// /// A response object containing the response body and response headers. /// - public Task ReplyAsync(string id, MessageReplyOptions messageReplyOptions) + public Task ReplyAsync(string id, MessageReplyOptions messageReplyOptions) => ExecuteRequest(HttpMethod.Post, $"api/messages/{id}/reply", messageReplyOptions); } } diff --git a/Mailosaur/Operations/OperationBase.cs b/Mailosaur/Operations/OperationBase.cs index 4794e34..bbdb1c5 100644 --- a/Mailosaur/Operations/OperationBase.cs +++ b/Mailosaur/Operations/OperationBase.cs @@ -122,7 +122,7 @@ public T HandleAggregateException(Func 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) @@ -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; }