From: GSonofNun Date: Wed, 24 Sep 2014 20:19:58 +0000 (-0400) Subject: Added base InboxAPIObject class and RestfulModelCollection class X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;p=inbox.net.git Added base InboxAPIObject class and RestfulModelCollection class --- diff --git a/inbox_net/Client.cs b/inbox_net/Client.cs index e2a2f6b..37b28c6 100644 --- a/inbox_net/Client.cs +++ b/inbox_net/Client.cs @@ -5,6 +5,9 @@ using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; +using inbox_net.DataModel; +using Newtonsoft.Json; +using System.Net; namespace inbox_net { @@ -35,9 +38,11 @@ namespace inbox_net this.access_token_url = AUTH_SERVER + "/oauth/token"; this.client = new HttpClient(); this.client.DefaultRequestHeaders.Add("X-Inbox-API-Wrapper", "dotNET"); - + this.Namespaces = new RestfulModelCollection(this); } + public RestfulModelCollection Namespaces { get; set; } + public string Access_Token { get @@ -56,7 +61,7 @@ namespace inbox_net public string authenticationUrl(string redirect_uri, string login_hint = "") { - Dictionary args = new Dictionary(); + Dictionary args = new Dictionary(); args.Add("redirect_uri", redirect_uri); args.Add("client_id", this.app_id); args.Add("response_type", "code"); @@ -88,8 +93,122 @@ namespace inbox_net return this.auth_token; } + private async Task ValidateResponse(HttpResponseMessage response) + { + Dictionary status_code_to_exc = new Dictionary(); + status_code_to_exc.Add(HttpStatusCode.BadRequest, "APIError"); + status_code_to_exc.Add(HttpStatusCode.NotFound, "NotFoundError"); + status_code_to_exc.Add(HttpStatusCode.Conflict, "ConflictError"); + var request = response.RequestMessage; + string url = request.RequestUri.ToString(); + var status_code = response.StatusCode; + string data = await response.Content.ReadAsStringAsync(); + JObject obj = null; + try + { + obj = data != null ? JObject.Parse(data) : null; + } + catch (Exception) + { + + } + + if (status_code == HttpStatusCode.OK) + { + return response; + } + else if (status_code == HttpStatusCode.Unauthorized) + { + throw new Exception("Not Authorized"); + } + else if (status_code == HttpStatusCode.BadRequest || status_code == HttpStatusCode.NotFound || status_code == HttpStatusCode.Conflict) + { + string errorMessage = status_code_to_exc[status_code]; + try + { + JObject responseJSON = JObject.Parse(data); + if (responseJSON["message"] != null) + { + throw new InboxException(url, status_code, data, responseJSON["message"].ToString()); + } + else + { + throw new InboxException(url, status_code, data, "N/A"); + } + } + catch (Exception) + { + throw new InboxException(url, status_code, data, "Malformed"); + } + throw new Exception("Error - Your fault...probably"); + } + else if (status_code == HttpStatusCode.InternalServerError) + { + throw new InboxException(url, status_code, data, "ServerError"); + } + else + { + throw new InboxException(url, status_code, data, "Unknown status code"); + } + } + + public async Task> GetResources(string _namespace, RestfulModelCollection cls, Dictionary filters) + { + string prefix = string.Format("/n/{0}", _namespace != null ? _namespace : ""); + string url = string.Format("{0}{1}/{2}", this.api_server, prefix, cls.CollectionName); + url = Utilities.Url_Concat(url, filters); + + string json = await (await ValidateResponse(await client.GetAsync(url))).Content.ReadAsStringAsync(); + return await Task.Factory.StartNew(() => JsonConvert.DeserializeObject>(json)); + } + + public async Task GetRawResource(string _namespace, string objectType, string id, Dictionary filters) + { + // Get an indivitual REST resource + string extra = null; + if (filters["extra"] != null) + { + extra = filters["extra"].ToString(); + filters.Remove("extra"); + } + string prefix = string.Format("/n/{0}", _namespace != null ? _namespace : ""); + string postfix = string.Format("/{0}", extra != null ? extra : ""); + string url = string.Format("{0}{1}/{2}/{3}{4}", this.api_server, prefix, objectType, id, postfix); + url = Utilities.Url_Concat(url, filters); + return await (await ValidateResponse(await client.GetAsync(url))).Content.ReadAsStringAsync(); + } + + public async Task GetResource(string _namespace, string objectType, string id, Dictionary filters) + { + + + return default(T); + } + + + - + internal T CreateResource(Namespace p1, object p2, string p3) + { + throw new NotImplementedException(); + } + + internal T UpdateResource(Namespace _namespace, object cls, string id, string json) + { + throw new NotImplementedException(); + } + + + + internal object UpdateResource(Namespace p1, Type type, string p2, string p3) + { + throw new NotImplementedException(); + } + + internal object CreateResource(Namespace p1, Type type, string p2) + { + throw new NotImplementedException(); + } } } diff --git a/inbox_net/DataModel/InboxAPIObject.cs b/inbox_net/DataModel/InboxAPIObject.cs new file mode 100644 index 0000000..49b068a --- /dev/null +++ b/inbox_net/DataModel/InboxAPIObject.cs @@ -0,0 +1,63 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace inbox_net.DataModel +{ + public class InboxAPIObject + { + public string ID { get; set; } + public Type CLS { get; set; } + public Client API { get; set; } + public Namespace Namespace { get; set; } + + private InboxAPIObject(Type cls, Client api, Namespace _namespace) + { + this.ID = null; + this.CLS = cls; + this.API = api; + this.Namespace = _namespace; + } + + public InboxAPIObject Create(Type cls, Client api, Namespace _namespace) + { + var obj = Activator.CreateInstance(this.CLS, this.API, this.Namespace); + (obj as InboxAPIObject).CLS = cls; + return obj as InboxAPIObject; + } + + public async Task AsJson() + { + return await Task.Factory.StartNew(() => JsonConvert.SerializeObject(this)); + } + + public RestfulModelCollection ChildCollection(Dictionary filters = null) + { + return new RestfulModelCollection(this.API, this.Namespace, filters); + } + + public async void Save() + { + var newObj = Activator.CreateInstance(this.CLS); + if (this.ID != null) + { + newObj = this.API.UpdateResource(this.Namespace, this.CLS, this.ID, await this.AsJson()); + } + else + { + newObj = this.API.CreateResource(this.Namespace, this.CLS, await this.AsJson()); + } + this.CLS = newObj.GetType(); + } + + public async void Update() + { + var newObj = Activator.CreateInstance(this.CLS); + newObj = this.API.UpdateResource(this.Namespace, this.CLS, this.ID, await this.AsJson()); + this.CLS = newObj.GetType(); + } + } +} diff --git a/inbox_net/DataModel/RestfulModelCollection.cs b/inbox_net/DataModel/RestfulModelCollection.cs new file mode 100644 index 0000000..9892ee5 --- /dev/null +++ b/inbox_net/DataModel/RestfulModelCollection.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace inbox_net.DataModel +{ + public class RestfulModelCollection : ObservableCollection + { + const int CHUNK_SIZE = 50; + const int MAX_ITEMS = Convert.ToInt32(Math.Pow(2,64)) - 1; + + private Namespace Namespace; + private Client api; + private Dictionary filters; + private int offset; + private int limit; + public string CollectionName { get; set; } + + public RestfulModelCollection(Client _api, Namespace _namespace, Dictionary filter = null, int _offset = 0, int _limit = 0) : base() + { + if (filter == null) + { + this.filters = new Dictionary(); + } + else + { + this.filters = filter; + } + filters.Add("offset", 0); + filters.Add("limit", MAX_ITEMS); + this.Namespace = _namespace; + this.api = _api; + this.offset = _offset; + if (_limit == 0) + { + this.limit = MAX_ITEMS; + } + else + { + this.limit = _limit; + } + } + } +} diff --git a/inbox_net/InboxException.cs b/inbox_net/InboxException.cs new file mode 100644 index 0000000..0a1b687 --- /dev/null +++ b/inbox_net/InboxException.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace inbox_net +{ + public class InboxException : Exception + { + public string Url { get; set; } + public HttpStatusCode StatusCode { get; set; } + public string Data { get; set; } + public string Message { get; set; } + + public InboxException(string url, HttpStatusCode statusCode, string data, string message) + { + Url = url; + StatusCode = statusCode; + Data = data; + Message = message; + } + + public InboxException() + { + + } + } +} diff --git a/inbox_net/Utilities.cs b/inbox_net/Utilities.cs index f442ea8..35205a8 100644 --- a/inbox_net/Utilities.cs +++ b/inbox_net/Utilities.cs @@ -9,7 +9,7 @@ namespace inbox_net { public static class Utilities { - public static string Url_Concat(string url, Dictionary args, string fragments = null) + public static string Url_Concat(string url, Dictionary args, string fragments = null) { if (args == null && fragments == null) { @@ -35,7 +35,7 @@ namespace inbox_net { args_tail += url.Contains("?") ? "&" : "?"; } - args_tail += string.Format("{0}={1}", item.Key, WebUtility.UrlEncode(item.Value)); + args_tail += string.Format("{0}={1}", item.Key, WebUtility.UrlEncode(item.Value.ToString())); } } diff --git a/inbox_net/inbox_net.csproj b/inbox_net/inbox_net.csproj index fc8a618..b213fec 100644 --- a/inbox_net/inbox_net.csproj +++ b/inbox_net/inbox_net.csproj @@ -39,14 +39,17 @@ + + +