Example #1
0
        /// <summary>
        /// Create a new token instance with empty values.
        /// </summary>
        public Token()
        {
            string json = "{}";

            _dateCreated = DateTime.Now;
            _json = JSONObject.CreateFromString(json);
        }
        /// <summary>
        /// Create a new collection notification instance using a valid json.
        /// </summary>
        /// <param name="json">The json object used to
        /// fill the collection notification data</param>
        public CollectionNotification(JSONObject json)
        {
            // todo: strong type validation
            _json = json;

            // set collection values
            _collection = new Collection(_json.Dictionary["collection"]);
        }
        /// <summary>
        /// Create a new user instance with empty values.
        /// </summary>
        public UserEx()
        {
            string json = "{}";

            _json = JSONObject.CreateFromString(json);
        }
        /// <summary>
        /// Create a new item instance with empty values.
        /// </summary>
        public Item()
        {
            string json = "{}";

            _json = JSONObject.CreateFromString(json);
        }
        /// <summary>
        /// Create a new account balance instance with empty values.
        /// </summary>
        public AccountBalance()
        {
            string json = "{}";

            _json = JSONObject.CreateFromString(json);
        }
        /// <summary>
        /// Create a new collection instance with empty values.
        /// </summary>
        public Collection()
        {
            string json = "{}";

            _json = JSONObject.CreateFromString(json);
        }
        /// <summary>
        /// Create a new amount by transaction type item instance with empty values.
        /// </summary>
        public AmountByTTypeItem()
        {
            string json = "{}";

            _json = JSONObject.CreateFromString(json);
        }
        /// <summary>
        /// Create a new amount by transaction type item instance with empty values.
        /// </summary>
        public AmountByTTypeItem()
        {
            string json = "{}";

            _json = JSONObject.CreateFromString(json);
        }
        /// <summary>
        /// Create a new payment choices instance with empty values.
        /// </summary>
        public PaymentChoices()
        {
            string json = "{}";

            _json = JSONObject.CreateFromString(json);
        }
 /// <summary>
 /// Makes a MercadoLibre API PUT request.
 /// </summary>
 /// <param name="relativePath">The path for the call,
 /// e.g. /username</param>
 /// <param name="json">A json object that
 /// will get passed as the request body.</param>
 /// <param name="contentType">The data format of the json to be written 
 /// in the request body.</param>
 public JSONObject Put(string relativePath, JSONObject json, ContentType contentType = ContentType.JSON)
 {
     return Call(relativePath, HttpVerb.PUT, null, json, contentType);
 }
 private static void RecursiveObjectToString(JSONObject obj, StringBuilder sb)
 {
     if (obj.IsDictionary)
     {
         sb.Append("{");
         RecursiveDictionaryToString(obj, sb);
         if (sb[sb.Length - 1].ToString() == ",")
         {
             sb.Remove(sb.Length - 1, 1);
         }
         sb.Append("}");
     }
     else if (obj.IsArray)
     {
         sb.Append("[");
         foreach (JSONObject o in obj.Array)
         {
             RecursiveObjectToString(o, sb);
             sb.Append(",");
         }
         if (sb[sb.Length - 1].ToString() == ",")
         {
             sb.Remove(sb.Length - 1, 1);
         }
         sb.Append("]");
     }
     else // some sort of scalar value
     {
         if (obj.IsBoolean || obj.IsInteger)
         {
             sb.Append(obj.String);
         }
         else if (obj.IsString)
         {
             sb.Append("\"" + obj.String + "\"");
         }
         else
         {
             sb.Append("null");
         }
     }
 }
 /// <summary>
 /// Recursively deconstructs this JSONObject to string
 /// </summary>
 private static void RecursiveDictionaryToString(JSONObject obj, StringBuilder sb)
 {
     foreach (KeyValuePair<string, JSONObject> kvp in obj.Dictionary)
     {
         sb.Append("\"" + kvp.Key + "\"");
         sb.Append(":");
         RecursiveObjectToString(kvp.Value, sb);
         sb.Append(",");
     }
 }
        /// <summary>
        /// Recursively constructs this JSONObject 
        /// </summary>
        private static JSONObject Create(object o)
        {
            JSONObject obj = new JSONObject();
            if (o is object[])
            {
                object[] objArray = o as object[];
                obj._arrayData = new List<JSONObject>();
                for (int i = 0; i < objArray.Length; ++i)
                {
                    obj._arrayData.Add(Create(objArray[i]));
                }
            }
            else if (o is Dictionary<string, object>)
            {
                obj._dictData = new Dictionary<string, JSONObject>();
                Dictionary<string, object> dict =
                    o as Dictionary<string, object>;
                foreach (string key in dict.Keys)
                {
                    obj._dictData[key] = Create(dict[key]);
                }
            }
            else if (o != null) // o is a scalar
            {
                obj._stringData = o.ToString();
            }

            return obj;
        }
 /// <summary>
 /// Sets a custom class attribute contained in this json object 
 /// </summary>
 public void SetJSONCustomClassAttribute(string attribute, JSONObject value)
 {
     try
     {
         this.Dictionary[attribute] = value;
     }
     catch (KeyNotFoundException)
     {
         this.Dictionary.Add(attribute, value);
     }
     catch (Exception e)
     {
         throw e;
     }
 }
 /// <summary>
 /// Create a new phone instance using a valid json.
 /// </summary>
 /// <param name="json">The json object used to
 /// fill the phone data</param>
 public Phone(JSONObject json)
 {
     // todo: como valido que no me asignen cualquier fruta
     _json = json;
 }
        /// <summary>
        /// Create a new response urls instance with empty values.
        /// </summary>
        public ResponseUrls()
        {
            string json = "{}";

            _json = JSONObject.CreateFromString(json);
        }
 /// <summary>
 /// Create a new credential instance using a valid json.
 /// </summary>
 /// <param name="json">The json object used to
 /// fill the credential data</param>
 public Credential(JSONObject json)
 {
     _json = json;
 }
        /// <summary>
        /// Makes a MercadoLibre API Call.
        /// </summary>
        private JSONObject Call(string relativePath, HttpVerb httpVerb, List<KeyValuePair<string, string>> args, JSONObject body, ContentType contentType = ContentType.JSON)
        {
            Uri url = new Uri(_baseURL, relativePath);

            JSONObject obj = JSONObject.CreateFromString(MakeRequest(url, httpVerb, args, body, contentType));

            return obj;
        }
 /// <summary>
 /// Create a new payment choices instance using a valid json.
 /// </summary>
 /// <param name="json">The json object used to
 /// fill the payment choices data</param>
 public PaymentChoices(JSONObject json)
 {
     // todo: strong type validation
     _json = json;
 }
 /// <summary>
 /// Encode a json body as a json string or a http string.
 /// </summary>
 private string EncodeBody(JSONObject body, ContentType contentType = ContentType.JSON)
 {
     StringBuilder sb = new StringBuilder();
     if (contentType == ContentType.JSON)
     {
         sb.Append(body.ToString());
     }
     else
     {
         foreach (KeyValuePair<string, JSONObject> kvp in body.Dictionary)
         {
             sb.Append(HttpUtility.UrlEncode(kvp.Key));
             sb.Append("=");
             string str = kvp.Value.ToString();
             if (str.Substring(0, 1) == "\"")
             {
                 str = str.Substring(1, str.Length - 2); // rip "
             }
             sb.Append(HttpUtility.UrlEncode(str));
             sb.Append("&");
         }
         sb.Remove(sb.Length - 1, 1); // Remove trailing &
     }
     return sb.ToString();
 }
 /// <summary>
 /// Create a new amount by transaction type item instance using a valid json.
 /// </summary>
 /// <param name="json">The json object used to
 /// fill the amount by transaction type item data</param>
 public AmountByTTypeItem(JSONObject json)
 {
     // todo: strong type validation
     _json = json;
 }
        /// <summary>
        /// Make an HTTP request, with the given query args
        /// </summary>
        private string MakeRequest(Uri url, HttpVerb httpVerb, List<KeyValuePair<string, string>> args, JSONObject body, ContentType contentType = ContentType.JSON)
        {
            // Prepare HTTP url
            url = PrepareUrl(url, AccessToken, args);

            // Set request
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = httpVerb.ToString();

            if ((httpVerb == HttpVerb.POST) || (httpVerb == HttpVerb.PUT))
            {
                // Prepare HTTP body
                string postData = EncodeBody(body, contentType);
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] postDataBytes = encoding.GetBytes(postData);

                // Set content type & length
                if (contentType == ContentType.JSON)
                {
                    request.ContentType = "application/json";
                }
                else
                {
                    request.ContentType = "application/x-www-form-urlencoded";
                }
                request.ContentLength = postDataBytes.Length;

                // Call API
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(postDataBytes, 0, postDataBytes.Length);
                requestStream.Close();
            }

            try
            {
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    // Read response data
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    string responseBody = reader.ReadToEnd();

                    // Understand REST response
                    if (SuccessfulCall(response.StatusCode))
                    {
                        // Ok
                        return responseBody;
                    }
                    else
                    {
                        // Problem
                        try
                        {
                            // Try throwing a well-formed api error
                            JSONObject errorBody = JSONObject.CreateFromString(responseBody);
                            throw new RESTAPIException(errorBody.Dictionary["error"].Dictionary["type"].String, errorBody.Dictionary["error"].Dictionary["message"].String);
                        }
                        catch (RESTAPIException restEx)
                        {
                            throw restEx;  // this is a well-formed error message
                        }
                        catch
                        {
                            throw new RESTAPIException(response.StatusCode.ToString(), responseBody);  // this is not a well-formed message
                        }
                    }
                }
            }
            catch (WebException e)
            {
                throw new RESTAPIException("Server Error", e.Message);
            }
        }
 /// <summary>
 /// Create a new amount by transaction type item instance using a valid json.
 /// </summary>
 /// <param name="json">The json object used to
 /// fill the amount by transaction type item data</param>
 public AmountByTTypeItem(JSONObject json)
 {
     // todo: strong type validation
     _json = json;
 }
Example #24
0
 /// <summary>
 /// Create a new token instance using a valid json.
 /// </summary>
 /// <param name="json">The json object used to
 /// fill the token data</param>
 public Token(JSONObject json)
 {
     _json = json;
 }
 /// <summary>
 /// Create a new collection instance using a valid json.
 /// </summary>
 /// <param name="json">The json object used to
 /// fill the collection data</param>
 public Collection(JSONObject json)
 {
     // todo: strong type validation
     _json = json;
 }
        /// <summary>
        /// Create a new amount by transaction type list instance.
        /// </summary>
        public AmountByTTypeList()
        {
            string json = "[]";

            _json = JSONObject.CreateFromString(json);
        }
 /// <summary>
 /// Create a new account balance instance using a valid json.
 /// </summary>
 /// <param name="json">The json object used to
 /// fill the balance data</param>
 public AccountBalance(JSONObject json)
 {
     // todo: strong type validation
     _json = json;
 }
 /// <summary>
 /// Create a new amount by transaction type list instance using a valid json.
 /// </summary>
 /// <param name="json">The json object used to
 /// fill the amount by transaction type list data</param>
 public AmountByTTypeList(JSONObject json)
 {
     _json = json;
 }
 /// <summary>
 /// Create a new item instance using a valid json.
 /// </summary>
 /// <param name="json">The json object used to
 /// fill the item data</param>
 public Item(JSONObject json)
 {
     // todo: strong type validation
     _json = json;
 }
        /// <summary>
        /// Create a new preference instance with empty values.
        /// </summary>
        public Preference()
        {
            string json = "{}";

            _json = JSONObject.CreateFromString(json);
        }
 /// <summary>
 /// Create a new user instance using a valid json.
 /// </summary>
 /// <param name="json">The json object used to
 /// fill the user data</param>
 public UserEx(JSONObject json)
 {
     // todo: strong type validation
     _json = json;
 }
 /// <summary>
 /// Create a new preference instance using a valid json.
 /// </summary>
 /// <param name="json">The json object used to
 /// fill the preference data</param>
 public Preference(JSONObject json)
 {
     // todo: strong type validation
     _json = json;
 }
 /// <summary>
 /// Create a new response urls instance using a valid json.
 /// </summary>
 /// <param name="json">The json object used to
 /// fill the response urls data</param>
 public ResponseUrls(JSONObject json)
 {
     // todo: strong type validation
     _json = json;
 }
        /// <summary>
        /// Create a credential instance with empty values.
        /// </summary>
        public Credential()
        {
            string json = "{}";

            _json = JSONObject.CreateFromString(json);
        }