--- /dev/null
+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;
+ }
+
+
+
+
+ }
+}
namespace inbox_net
{
- public class InboxClient
+ public class InboxClient : inbox_net.IInboxClient
{
private string Inbox_API_Url = "https://slack.com/api/";
#endregion
-
#region Tags
public async Task<IEnumerable<Tag>> GetTags()
#endregion
-
#region Threads
public async Task<Thread> GetThread(string thread_id)
#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)
{
--- /dev/null
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace inbox_net
+{
+ class StringConstants
+ {
+ }
+}
--- /dev/null
+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);
+ }
+ }
+}
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
+ <Compile Include="Client.cs" />
<Compile Include="DataModel\CanonicalTags.cs" />
<Compile Include="DataModel\Draft.cs" />
<Compile Include="DataModel\File.cs" />
<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">