/// <summary> /// Returns the enum SiteConf.Model based on object type. /// </summary> /// <param name="obj"></param> /// <returns></returns> private static SiteConf.Model GetModelFromObj(ObjectAbstract obj) { Type type = obj.GetType(); if (type == typeof(SiteConf.DiseaseTag.Object)) return Model.diseasetag; if (type == typeof(SiteConf.Gene.Object)) return Model.gene; if (type == typeof(SiteConf.HVPTran.Object)) return Model.hvptran; if (type == typeof(SiteConf.OrgSite.Object)) return Model.orgsite; if (type == typeof(SiteConf.Upload.Object)) return Model.upload; else throw new Exception("Can not find object within SiteConf.Model"); }
/// <summary> /// Serializes standard object to JObject /// </summary> /// <param name="obj"></param> /// <returns></returns> private static JObject JObjectSerializer(ObjectAbstract obj) { JObject jObj = new JObject(); Type type = obj.GetType(); IList<System.Reflection.PropertyInfo> props = new List<System.Reflection.PropertyInfo>(type.GetProperties()); foreach (System.Reflection.PropertyInfo prop in props) { string propName = prop.Name; // ignore "ID" and "resource_uri" fields if (propName == "ID" || propName == "resource_uri") continue; else { object propValue = prop.GetValue(obj, null); // do not append to json if value is null if (propValue != null) jObj.Add(propName, propValue.ToString()); } } return jObj; }
/// <summary> /// Saves a new Object via Rest POST /// </summary> /// <param name="obj"></param> public static void SaveNewRestObject(ObjectAbstract obj) { JObject jObj = JObjectSerializer(obj); WebClient client = new WebClient(); client.Headers[HttpRequestHeader.ContentType] = "application/json"; client.Proxy = _proxy; try { client.UploadString(RestAPI.URL + RestAPI.API + GetModelFromObj(obj) + "/" + RestAPI.api_key, "POST", jObj.ToString()); } catch (WebException ex) { // failed to save data } }
/// <summary> /// Delete an existing Object via Rest DELETE /// </summary> /// <param name="obj"></param> public static void DeleteRestObject(ObjectAbstract obj) { RestClient client = new RestClient(RestAPI.URL); client.Proxy = _proxy; // setup request params RestRequest request = DataLoader.RestRequest(Method.DELETE, GetModelFromObj(obj)); request.AddParameter("ID", obj.ID.ToString()); client.Execute(request); }