]> khamidou — GitHub mirrors - inbox.net.git/commitdiff
Added new Client object based on other Inbox clients
authorGSonofNun <sonofnun@outlook.com>
Tue, 23 Sep 2014 20:32:55 +0000 (16:32 -0400)
committerGSonofNun <sonofnun@outlook.com>
Tue, 23 Sep 2014 20:32:55 +0000 (16:32 -0400)
inbox_net/Client.cs [new file with mode: 0644]
inbox_net/InboxClient.cs
inbox_net/StringConstants.cs [new file with mode: 0644]
inbox_net/Utilities.cs [new file with mode: 0644]
inbox_net/inbox_net.csproj

diff --git a/inbox_net/Client.cs b/inbox_net/Client.cs
new file mode 100644 (file)
index 0000000..84bcc9d
--- /dev/null
@@ -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<KeyValuePair<string, string>> 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<string, string> args = new Dictionary<string, string>();
+            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<string> Token_For_Code(string code)
+        {
+            List<KeyValuePair<string, string>> args = new List<KeyValuePair<string, string>>();
+            args.Add(new KeyValuePair<string, string>("client_id", this.app_id));
+            args.Add(new KeyValuePair<string, string>("client_secret", this.app_secret));
+            args.Add(new KeyValuePair<string, string>("grant_type", "authorization_code"));
+            args.Add(new KeyValuePair<string, string>("code", code));
+
+            Dictionary<string, string> headers = new Dictionary<string, string>();
+            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;
+        }
+
+
+
+        
+    }
+}
index 27960a9af4d126a789fa855efab88e3879c38e6f..25e312c58e3dbc61d9e0dc930ecc2c20611569e8 100644 (file)
@@ -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<IEnumerable<Tag>> GetTags()
@@ -94,7 +93,6 @@ namespace inbox_net
 
 #endregion
 
-
 #region Threads
 
         public async Task<Thread> GetThread(string thread_id)
@@ -188,7 +186,26 @@ namespace inbox_net
 
 #endregion
 
-#region HttpHelperMethods
+#region Drafts
+
+        public async Task<IEnumerable<Draft>> 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<KeyValuePair<string, string>> 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<IEnumerable<Draft>>(json);
+        }
+
+        public async Task<Draft> GetDraft(string draft_id)
+        {
+            string json = await HttpGet(GenerateUri(string.Format(MethodURIs.Draft, this.Namespace, draft_id)));
+            return await DeserializeObjectAsync<Draft>(json);
+        }
+
+#endregion
+
+        #region HttpHelperMethods
 
         private async Task<string> HttpGet(string callUri)
         {
diff --git a/inbox_net/StringConstants.cs b/inbox_net/StringConstants.cs
new file mode 100644 (file)
index 0000000..2968ed5
--- /dev/null
@@ -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 (file)
index 0000000..f442ea8
--- /dev/null
@@ -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<string, string> 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);
+        }
+    }
+}
index 62941c77551ccf398802b0a0a6333f31c92b6662..fc8a6180d6836f38e5498908c20fdce14ac051df 100644 (file)
@@ -35,6 +35,7 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Compile Include="Client.cs" />
     <Compile Include="DataModel\CanonicalTags.cs" />
     <Compile Include="DataModel\Draft.cs" />
     <Compile Include="DataModel\File.cs" />
@@ -47,6 +48,8 @@
     <Compile Include="DataModel\Thread.cs" />
     <Compile Include="InboxClient.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="StringConstants.cs" />
+    <Compile Include="Utilities.cs" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="Newtonsoft.Json">