From 643e60d616b7d0bb36f7de0696523fcbf3692f97 Mon Sep 17 00:00:00 2001 From: GSonofNun Date: Tue, 16 Sep 2014 17:13:14 -0400 Subject: [PATCH] Added Message methods --- .../Test_WinRT.Windows/MainPage.xaml.cs | 21 +- inbox_net/DataModel/Message.cs | 5 +- inbox_net/InboxClient.cs | 244 +++++++++++++----- 3 files changed, 200 insertions(+), 70 deletions(-) diff --git a/Test_WinRT/Test_WinRT.Windows/MainPage.xaml.cs b/Test_WinRT/Test_WinRT.Windows/MainPage.xaml.cs index 2a9a198..4e6c9cd 100644 --- a/Test_WinRT/Test_WinRT.Windows/MainPage.xaml.cs +++ b/Test_WinRT/Test_WinRT.Windows/MainPage.xaml.cs @@ -38,12 +38,23 @@ namespace Test_WinRT List tags = new List(await client.GetTags()); //Tag tag = await client.NewCustomTag("TestInboxApp"); //Tag tag = await client.GetTag("InboxApp"); - Tag tag = tags.Where(t => t.Name == "TestInboxApp").First(); + //Tag tag = tags.Where(t => t.Name == "TestInboxApp").First(); //Tag tag2 = await client.RenameCustomTag(tag.ID, "Other Test"); - List threads = new List(await client.GetThreads(tag: CanonicalTags.Inbox)); - string[] add_tags = { tag.Name }; - string[] remove_tags = { threads[0].Tags[0].Name }; - await client.UpdateThreadTags(threads[0].ID, add_tags); + //List threads = new List(await client.GetThreads(tag: CanonicalTags.Inbox)); + //string[] add_tags = { tag.Name }; + //string[] remove_tags = { threads[0].Tags[0].Name }; + //await client.UpdateThreadTags(threads[0].ID, "test", "test2"); + IEnumerable messages = await client.GetMessages(tag: CanonicalTags.Inbox, limit: 5); + Message message = null; + Message mess = messages.First(); + if (mess.Unread) + { + message = await client.MarkMessageAsRead(mess.ID); + } + else + { + message = await client.MarkMessageAsUnread(mess.ID); + } } diff --git a/inbox_net/DataModel/Message.cs b/inbox_net/DataModel/Message.cs index 73ab9d9..88b1894 100644 --- a/inbox_net/DataModel/Message.cs +++ b/inbox_net/DataModel/Message.cs @@ -38,7 +38,7 @@ namespace inbox_net.DataModel [JsonProperty("date")] public int Date { get; set; } - [JsonProperty("thread")] + [JsonProperty("thread_id")] public string Thread_ID { get; set; } [JsonProperty("files")] @@ -50,6 +50,9 @@ namespace inbox_net.DataModel [JsonProperty("body")] public string Body { get; set; } + [JsonProperty("unread")] + public bool Unread { get; set; } + public override string ToString() { return Subject; diff --git a/inbox_net/InboxClient.cs b/inbox_net/InboxClient.cs index d965d60..27960a9 100644 --- a/inbox_net/InboxClient.cs +++ b/inbox_net/InboxClient.cs @@ -112,7 +112,7 @@ namespace inbox_net return await DeserializeObjectAsync>(json); } - public async Task UpdateThreadTags(string thread_id, string[] add_tags) + public async Task UpdateThreadTags(string thread_id, params string[] add_tags) { List> parameters = new List>(); parameters.Add(new KeyValuePair("add_tags", StringsToJsonArray(add_tags))); @@ -137,80 +137,58 @@ namespace inbox_net private async Task SendUpdateTagsRequest(string thread_id, List> parameters) { - string json = await HttpPut(GenerateUri(string.Format(MethodURIs.Thread, this.Namespace, thread_id)), GenerateRequestBody(parameters)); + string json = await HttpPut(GenerateUri(string.Format(MethodURIs.Thread, this.Namespace, thread_id)), GenerateRequestBody(parameters, false)); return await DeserializeObjectAsync(json); } - private List> ValidateGetThreadsParams(string subject = null, string any_email = null, string to = null, string from = null, string cc = null, - string bcc = null, string tag = null, string filename = null, int limit = 100, int offset = 0, int last_message_before = 0, + + +#endregion + +#region Messages + + public async Task GetMessage(string message_id) + { + string json = await HttpGet(GenerateUri(string.Format(MethodURIs.Message, this.Namespace, message_id))); + return await DeserializeObjectAsync(json); + } + + public async Task> GetMessages(string thread_id = null, string subject = null, string any_email = null, string to = null, string from = null, string cc = null, + string bcc = null, string tag = null, string filename = null, int limit = 100, int offset = 0, int last_message_before = 0, int last_message_after = 0, int started_before = 0, int started_after = 0) + { + List> parameters = ValidateGetMessagesParams(thread_id, subject, any_email, to, from, cc, bcc, tag, filename, limit, offset, last_message_before, last_message_after, started_before, started_after); + string json = await HttpGet(GenerateUri(string.Format(MethodURIs.Messages, this.Namespace), parameters)); + return await DeserializeObjectAsync>(json); + } + + public async Task MarkMessageAsRead(string message_id) { List> parameters = new List>(); - if (subject != null) - { - parameters.Add(new KeyValuePair("subject", subject)); - } - if (any_email != null) - { - parameters.Add(new KeyValuePair("any_email", any_email)); - } - if (to != null) - { - parameters.Add(new KeyValuePair("to", to)); - } - if (from != null) - { - parameters.Add(new KeyValuePair("from", from)); - } - if (cc != null) - { - parameters.Add(new KeyValuePair("cc", cc)); - } - if (bcc != null) - { - parameters.Add(new KeyValuePair("bcc", bcc)); - } - if (tag != null) - { - parameters.Add(new KeyValuePair("tag", tag)); - } - if (filename != null) - { - parameters.Add(new KeyValuePair("filename", filename)); - } - if (limit != 100) - { - parameters.Add(new KeyValuePair("limit", limit.ToString())); - } - if (offset != 0) - { - parameters.Add(new KeyValuePair("offset", offset.ToString())); - } - if (last_message_before != 0) - { - parameters.Add(new KeyValuePair("last_message_before", last_message_before.ToString())); - } - if (last_message_after != 0) - { - parameters.Add(new KeyValuePair("last_message_after", last_message_after.ToString())); - } - if (started_before != 0) - { - parameters.Add(new KeyValuePair("started_before", started_before.ToString())); - } - if (started_after != 0) - { - parameters.Add(new KeyValuePair("started_after", started_after.ToString())); - } - return parameters; + parameters.Add(new KeyValuePair("unread", "false")); + return await SendUpdateStatusRequest(message_id, parameters); } -#endregion + public async Task MarkMessageAsUnread(string message_id) + { + List> parameters = new List>(); + parameters.Add(new KeyValuePair("unread", "true")); + return await SendUpdateStatusRequest(message_id, parameters); + } + + private async Task SendUpdateStatusRequest(string message_id, List> parameters) + { + string json = await HttpPut(GenerateUri(string.Format(MethodURIs.Message, this.Namespace, message_id)), GenerateRequestBody(parameters, false)); + return await DeserializeObjectAsync(json); + } +#endregion + +#region HttpHelperMethods private async Task HttpGet(string callUri) { @@ -287,7 +265,7 @@ namespace inbox_net return sb.ToString(); } - private string GenerateRequestBody(IEnumerable> parameters) + private string GenerateRequestBody(IEnumerable> parameters, bool withQuotesAroundValue = true) { StringBuilder sb = new StringBuilder(); sb.AppendLine("{"); @@ -295,7 +273,7 @@ namespace inbox_net { foreach (var item in parameters) { - if (item.Value[0] == '[' && item.Value[item.Value.Length - 1] == ']') + if (!withQuotesAroundValue) { sb.AppendLine(string.Format(" \"{0}\": {1},", item.Key, item.Value)); } @@ -311,6 +289,142 @@ namespace inbox_net return sb.ToString(); } + private List> ValidateGetThreadsParams(string subject = null, string any_email = null, string to = null, string from = null, string cc = null, + string bcc = null, string tag = null, string filename = null, int limit = 100, int offset = 0, int last_message_before = 0, + int last_message_after = 0, int started_before = 0, int started_after = 0) + { + List> parameters = new List>(); + if (subject != null) + { + parameters.Add(new KeyValuePair("subject", subject)); + } + if (any_email != null) + { + parameters.Add(new KeyValuePair("any_email", any_email)); + } + if (to != null) + { + parameters.Add(new KeyValuePair("to", to)); + } + if (from != null) + { + parameters.Add(new KeyValuePair("from", from)); + } + if (cc != null) + { + parameters.Add(new KeyValuePair("cc", cc)); + } + if (bcc != null) + { + parameters.Add(new KeyValuePair("bcc", bcc)); + } + if (tag != null) + { + parameters.Add(new KeyValuePair("tag", tag)); + } + if (filename != null) + { + parameters.Add(new KeyValuePair("filename", filename)); + } + if (limit != 100) + { + parameters.Add(new KeyValuePair("limit", limit.ToString())); + } + if (offset != 0) + { + parameters.Add(new KeyValuePair("offset", offset.ToString())); + } + if (last_message_before != 0) + { + parameters.Add(new KeyValuePair("last_message_before", last_message_before.ToString())); + } + if (last_message_after != 0) + { + parameters.Add(new KeyValuePair("last_message_after", last_message_after.ToString())); + } + if (started_before != 0) + { + parameters.Add(new KeyValuePair("started_before", started_before.ToString())); + } + if (started_after != 0) + { + parameters.Add(new KeyValuePair("started_after", started_after.ToString())); + } + return parameters; + } + + private List> ValidateGetMessagesParams(string thread_id = null, string subject = null, string any_email = null, string to = null, string from = null, string cc = null, + string bcc = null, string tag = null, string filename = null, int limit = 100, int offset = 0, int last_message_before = 0, + int last_message_after = 0, int started_before = 0, int started_after = 0) + { + List> parameters = new List>(); + if (thread_id != null) + { + parameters.Add(new KeyValuePair("thread_id", thread_id)); + } + if (subject != null) + { + parameters.Add(new KeyValuePair("subject", subject)); + } + if (any_email != null) + { + parameters.Add(new KeyValuePair("any_email", any_email)); + } + if (to != null) + { + parameters.Add(new KeyValuePair("to", to)); + } + if (from != null) + { + parameters.Add(new KeyValuePair("from", from)); + } + if (cc != null) + { + parameters.Add(new KeyValuePair("cc", cc)); + } + if (bcc != null) + { + parameters.Add(new KeyValuePair("bcc", bcc)); + } + if (tag != null) + { + parameters.Add(new KeyValuePair("tag", tag)); + } + if (filename != null) + { + parameters.Add(new KeyValuePair("filename", filename)); + } + if (limit != 100) + { + parameters.Add(new KeyValuePair("limit", limit.ToString())); + } + if (offset != 0) + { + parameters.Add(new KeyValuePair("offset", offset.ToString())); + } + if (last_message_before != 0) + { + parameters.Add(new KeyValuePair("last_message_before", last_message_before.ToString())); + } + if (last_message_after != 0) + { + parameters.Add(new KeyValuePair("last_message_after", last_message_after.ToString())); + } + if (started_before != 0) + { + parameters.Add(new KeyValuePair("started_before", started_before.ToString())); + } + if (started_after != 0) + { + parameters.Add(new KeyValuePair("started_after", started_after.ToString())); + } + return parameters; + } + +#endregion + +#region Json Helper Methods + private string StringsToJsonArray(string[] strings) { StringBuilder sb = new StringBuilder("["); @@ -332,5 +446,7 @@ namespace inbox_net return default(T); } +#endregion + } } -- 2.43.0