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

            _dateCreated = DateTime.Now;
            _json        = JSONObject.CreateFromString(json);
        }
        /// <summary>
        /// Returns a datetime attribute contained in this json object
        /// </summary>
        public void SetJSONDateTimeAttribute(string attribute, DateTime?value)
        {
            string newValue;

            if (value != null)
            {
                newValue = value.Value.ToString("yyyy-MM-ddThh:mm:ss.fffzzzz");
            }
            else
            {
                newValue = "null";
            }
            try
            {
                this.Dictionary[attribute] = JSONObject.CreateFromString(newValue);
            }
            catch (KeyNotFoundException)
            {
                this.Dictionary.Add(attribute, JSONObject.CreateFromString(newValue));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// Returns a string attribute contained in this json object
        /// </summary>
        public void SetJSONStringAttribute(string attribute, String value)
        {
            string newValue;

            if (value != null)
            {
                newValue = value.ToString();
            }
            else
            {
                newValue = "null";
            }
            try
            {
                this.Dictionary[attribute] = JSONObject.CreateFromString(newValue);
            }
            catch (KeyNotFoundException)
            {
                this.Dictionary.Add(attribute, JSONObject.CreateFromString(newValue));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <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>
 /// Returns a custom class attribute contained in this json object
 /// </summary>
 public JSONObject GetJSONCustomClassAttribute(string attribute)
 {
     try
     {
         return(this.Dictionary[attribute]);
     }
     catch
     {
         return(JSONObject.CreateFromString("{}"));
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// List item override.
 /// </summary>
 public string this[int index]
 {
     get
     {
         return(_json.Array[index].ToString());
     }
     set
     {
         _json.Array[index] = JSONObject.CreateFromString(value);
     }
 }
Exemplo n.º 7
0
        /// <summary>
        /// Create a new response urls instance with empty values.
        /// </summary>
        public ResponseUrls()
        {
            string json = "{}";

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

            _json = JSONObject.CreateFromString(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();
            }

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

                    // Throw the API call event
                    APICallEventArgs apiCallEvent = new APICallEventArgs();
                    if (body != null)
                    {
                        apiCallEvent.Body = body.ToString();
                    }
                    apiCallEvent.Response = responseBody;
                    apiCallEvent.Url      = url.ToString();
                    OnAPICall(apiCallEvent);

                    // Return API response body
                    return(responseBody);
                }
            }
            catch (WebException e)
            {
                JSONObject response = null;
                try
                {
                    // Try throwing a well-formed api error
                    Stream       stream = e.Response.GetResponseStream();
                    StreamReader reader = new StreamReader(stream);
                    response = JSONObject.CreateFromString(reader.ReadToEnd().Trim());
                    int    status  = Convert.ToInt16(response.GetJSONStringAttribute("status"));
                    string error   = response.GetJSONStringAttribute("error");
                    string message = response.GetJSONStringAttribute("message");
                    // optional: cause
                    string cause = "";
                    try
                    {
                        cause = response.Dictionary["cause"].Dictionary["message"].String;
                    }
                    catch
                    { }
                    throw new RESTAPIException(status, error, message, cause);
                }
                catch (RESTAPIException restEx)
                {
                    throw restEx;  // this is a well-formed error message
                }
                catch
                {
                    throw new RESTAPIException(999, e.Status.ToString(), e.Message);  // this is not a well-formed message
                }
            }
        }
        /// <summary>
        /// Create a new amount by reason item instance with empty values.
        /// </summary>
        public AmountByReasonItem()
        {
            string json = "{}";

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

            _json = JSONObject.CreateFromString(json);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Create a new item list instance.
        /// </summary>
        public ItemList()
        {
            string json = "[]";

            _json = JSONObject.CreateFromString(json);
        }
Exemplo n.º 13
0
 /// <summary>
 /// Add method.
 /// </summary>
 public void Add(string id)
 {
     _json.Array.Add(JSONObject.CreateFromString("{id: \"" + id + "\"}"));
 }
        /// <summary>
        /// Create a new account balance instance with empty values.
        /// </summary>
        public AccountBalance()
        {
            string json = "{}";

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

            _json = JSONObject.CreateFromString(json);
        }
Exemplo n.º 16
0
        /// <summary>
        /// Create a new user instance with empty values.
        /// </summary>
        public UserEx()
        {
            string json = "{}";

            _json = JSONObject.CreateFromString(json);
        }
        /// <summary>
        /// Create a credential instance with empty values.
        /// </summary>
        public Credential()
        {
            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>
        /// Create a new amount by transaction type list instance.
        /// </summary>
        public AmountByTTypeList()
        {
            string json = "[]";

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

            _json = JSONObject.CreateFromString(json);
        }
Exemplo n.º 21
0
        /// <summary>
        /// Create a new phone instance with empty values.
        /// </summary>
        public Phone()
        {
            string json = "{}";

            _json = JSONObject.CreateFromString(json);
        }