public T create(SortedList <String, Object> param) { Type typeParameterType = typeof(T); T entity = (T)Activator.CreateInstance(typeParameterType, api); // Build Post Data OAuth.Request request = new OAuth.Request(); // Set Values request.oauth_consumer_key = api.ConsumerKey; request.oauth_consumer_secret = api.ConsumerSecret; request.oauth_token = api.OAuthToken; request.oauth_token_secret = api.OAuthTokenSecret; // Build custom parameters for this OAuth Request SortedList <String, String> parameters = new SortedList <string, string>(); // Add parameters parameters.Add("ws.op", "create"); foreach (String key in param.Keys) { parameters.Add(key, param[key].ToString()); } request.Build(parameters, self_link, "POST"); WebClient client = new WebClient(); client.Headers["Content-type"] = "application/x-www-form-urlencoded"; String response = String.Empty; try { // Get response response = client.UploadString(self_link, request.Parameters); } catch (WebException ex) { // Throw Error Codes back to client // Client responsibility to handle them throw ex; } String newUrl = client.ResponseHeaders["Location"]; (entity as Entity.Base).load_from_url(newUrl); return(entity); }
/// <summary> /// Will delete this subscriber from the list and account /// </summary> /// <returns></returns> public bool delete() { bool success = false; // Build Post Data OAuth.Request request = api.BuildRequest(); String url = self_link; // Build custom parameters for this OAuth Request SortedList <String, String> parameters = new SortedList <string, string>(); // Build request request.Build(parameters, url, "DELETE"); try { WebRequest webRequest = WebRequest.Create(url + "?" + request.Parameters); webRequest.ContentType = "application/json"; webRequest.Method = "DELETE"; using (WebResponse response = webRequest.GetResponse()) { } success = true; } catch (Exception ex) { throw ex; } return(success); }
/// <summary> /// Will save or create the subscriber /// depending on whether it is a new subscriber or one retrived /// </summary> /// <returns></returns> public bool Save() { /// Did not put this in Base as many entities do no posses the ability to save themselves. /// This could possibily be split into another base class if there are more entities added to this sdk that support saving. bool success = false; // Build Post Data OAuth.Request request = api.BuildRequest(); String url = self_link; // Build custom parameters for this OAuth Request SortedList <String, String> parameters = new SortedList <string, string>(); // Build request request.Build(parameters, url, "PATCH"); // custom built json, otherwise serializing the entity will result in all fields been serialized // and we are only doing a PATCH (updating only fields that have changed) String json = "{"; bool first = true; // Build new subscriber entity only with modified entities foreach (String dirtyField in get_dirty()) { String pre = ","; if (first) { first = false; pre = String.Empty; } json += String.Format("{2}\"{0}\":\"{1}\"", dirtyField, Convert.ToString(this.GetType().GetProperty(dirtyField).GetValue(this, null)), pre); } json += "}"; WebClient client = new WebClient(); try { // Make the PATCH request WebRequest webRequest = WebRequest.Create(url + "?" + request.Parameters); webRequest.ContentType = "application/json"; webRequest.Method = "PATCH"; Stream dataStream = webRequest.GetRequestStream(); byte[] byteArray = System.Text.UTF8Encoding.UTF8.GetBytes(json); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = webRequest.GetResponse(); success = true; } catch (Exception ex) { throw ex; } return(success); }