From ae08def2173b35c03832c9c37873aca763efd6e6 Mon Sep 17 00:00:00 2001 From: GSonofNun Date: Tue, 23 Sep 2014 16:32:55 -0400 Subject: [PATCH] Added new Client object based on other Inbox clients --- inbox_net/Client.cs | 96 ++++++++++++++++++++++++++++++++++++ inbox_net/InboxClient.cs | 25 ++++++++-- inbox_net/StringConstants.cs | 12 +++++ inbox_net/Utilities.cs | 77 +++++++++++++++++++++++++++++ inbox_net/inbox_net.csproj | 3 ++ 5 files changed, 209 insertions(+), 4 deletions(-) create mode 100644 inbox_net/Client.cs create mode 100644 inbox_net/StringConstants.cs create mode 100644 inbox_net/Utilities.cs diff --git a/inbox_net/Client.cs b/inbox_net/Client.cs new file mode 100644 index 0000000..84bcc9d --- /dev/null +++ b/inbox_net/Client.cs @@ -0,0 +1,96 @@ +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; + +namespace inbox_net +{ + + public class Client + { + private string api_server = "https://api.inboxapp.com"; + private string auth_server = "https://www.inboxapp.com"; + private string access_token_url = string.Empty; + private string auth_token = ""; + private HttpClient client; + private List> session_headers; + private string access_token; + private string app_secret; + private string app_id; + + public Client(string INBOX_APP_ID, + string INBOX_APP_SECRET, + string INBOX_ACCESS_TOKEN = null, + string API_SERVER = "https://api.inboxapp.com", + string AUTH_SERVER = "https://www.inboxapp.com") + { + if (!API_SERVER.Contains("://")) + { + throw new Exception("When overriding the Inbox API server address, you must include https://"); + } + this.api_server = API_SERVER; + this.auth_server = AUTH_SERVER + "/oauth/authorize"; + this.access_token_url = AUTH_SERVER + "/oauth/token"; + this.client = new HttpClient(); + this.client.DefaultRequestHeaders.Add("X-Inbox-API-Wrapper", "dotNET"); + + } + + public string Access_Token + { + get + { + return this.access_token; + } + set + { + access_token = value; + if (value != null) + { + client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic " + Convert.ToBase64String(Utilities.GetBytes(value + ":"))); + } + } + } + + public string authenticationUrl(string redirect_uri, string login_hint = "") + { + Dictionary args = new Dictionary(); + args.Add("redirect_uri", redirect_uri); + args.Add("client_id", this.app_id); + args.Add("response_type", "code"); + args.Add("scope", "email"); + args.Add("login_hint", login_hint); + args.Add("state", Utilities.generate_id(10)); + + return Utilities.Url_Concat(this.auth_server, args); + } + + public async Task Token_For_Code(string code) + { + List> args = new List>(); + args.Add(new KeyValuePair("client_id", this.app_id)); + args.Add(new KeyValuePair("client_secret", this.app_secret)); + args.Add(new KeyValuePair("grant_type", "authorization_code")); + args.Add(new KeyValuePair("code", code)); + + Dictionary headers = new Dictionary(); + headers.Add("Content-type", "application/x-www-form-urlencoded"); + headers.Add("Accept", "text/plain"); + + client.DefaultRequestHeaders.Add("Content-type", "application/x-www-form-urlencoded"); + client.DefaultRequestHeaders.Add("Accept", "text/plain"); + string response = await (await client.PostAsync(this.access_token_url, new FormUrlEncodedContent(args))).Content.ReadAsStringAsync(); + + JObject obj = JObject.Parse(response); + this.auth_token = obj["access_token"].ToString(); + return this.auth_token; + } + + + + + } +} diff --git a/inbox_net/InboxClient.cs b/inbox_net/InboxClient.cs index 27960a9..25e312c 100644 --- a/inbox_net/InboxClient.cs +++ b/inbox_net/InboxClient.cs @@ -10,7 +10,7 @@ using System.Threading.Tasks; namespace inbox_net { - public class InboxClient + public class InboxClient : inbox_net.IInboxClient { private string Inbox_API_Url = "https://slack.com/api/"; @@ -61,7 +61,6 @@ namespace inbox_net #endregion - #region Tags public async Task> GetTags() @@ -94,7 +93,6 @@ namespace inbox_net #endregion - #region Threads public async Task GetThread(string thread_id) @@ -188,7 +186,26 @@ namespace inbox_net #endregion -#region HttpHelperMethods +#region Drafts + + public async Task> GetDrafts(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.Drafts, this.Namespace), parameters)); + return await DeserializeObjectAsync>(json); + } + + public async Task GetDraft(string draft_id) + { + string json = await HttpGet(GenerateUri(string.Format(MethodURIs.Draft, this.Namespace, draft_id))); + return await DeserializeObjectAsync(json); + } + +#endregion + + #region HttpHelperMethods private async Task HttpGet(string callUri) { diff --git a/inbox_net/StringConstants.cs b/inbox_net/StringConstants.cs new file mode 100644 index 0000000..2968ed5 --- /dev/null +++ b/inbox_net/StringConstants.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace inbox_net +{ + class StringConstants + { + } +} diff --git a/inbox_net/Utilities.cs b/inbox_net/Utilities.cs new file mode 100644 index 0000000..f442ea8 --- /dev/null +++ b/inbox_net/Utilities.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace inbox_net +{ + public static class Utilities + { + public static string Url_Concat(string url, Dictionary args, string fragments = null) + { + if (args == null && fragments == null) + { + return url; + } + + // Strip off hashes + char[] trimChars = {'#'}; + url = url.TrimEnd(trimChars); + + string fragment_tail = ""; + if (fragments != null) + { + fragment_tail = "#" + WebUtility.UrlEncode(fragments); + } + + string args_tail = ""; + if (args != null) + { + foreach (var item in args) + { + if (url[url.Length - 1] != '?' || url[url.Length - 1] != '&') + { + args_tail += url.Contains("?") ? "&" : "?"; + } + args_tail += string.Format("{0}={1}", item.Key, WebUtility.UrlEncode(item.Value)); + } + } + + return url + args_tail + fragment_tail; + } + + public static string generate_id(int length) + { + string guidResult = string.Empty; + + while (guidResult.Length < length) + { + // Get the GUID. + guidResult += Guid.NewGuid().ToString().GetHashCode().ToString("x"); + } + + // Make sure length is valid. + if (length <= 0 || length > guidResult.Length) + throw new ArgumentException("Length must be between 1 and " + guidResult.Length); + + // Return the first length bytes. + return guidResult.Substring(0, length); + } + + public static byte[] GetBytes(string str) + { + byte[] bytes = new byte[str.Length * sizeof(char)]; + System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); + return bytes; + } + + public static string GetString(byte[] bytes) + { + char[] chars = new char[bytes.Length / sizeof(char)]; + System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length); + return new string(chars); + } + } +} diff --git a/inbox_net/inbox_net.csproj b/inbox_net/inbox_net.csproj index 62941c7..fc8a618 100644 --- a/inbox_net/inbox_net.csproj +++ b/inbox_net/inbox_net.csproj @@ -35,6 +35,7 @@ 4 + @@ -47,6 +48,8 @@ + + -- 2.43.0