public async Task <JObject> Exec()
        {
            var s = new NCMBSignature(NCMBRequest._ncmb.ApplicationKey, NCMBRequest._ncmb.ClientKey);

            s.Method   = Method;
            s.Name     = Name;
            s.ObjectId = ObjectId;
            s.Queries  = Queries;
            s.Path     = Path;
            var signature = s.Generate();

            var headers = GetHeader(s.Time, signature);
            var client  = new HttpClient();
            var request = new HttpRequestMessage(GetMethod(Method), s.Url());

            foreach (string key in headers.Keys)
            {
                // Console.WriteLine($"{key} {headers[key].ToString()}");
                request.Headers.Add(key, headers[key].ToString());
            }
            if (!(Data is null))
            {
                // File upload
                var multipart = new MultipartFormDataContent();
                var file      = new ByteArrayContent(Data);
                file.Headers.ContentType = new MediaTypeHeaderValue(MimeType);
                multipart.Add(file, "file", ObjectId);
                if (Fields != null && Fields.ContainsKey("acl") && Fields["acl"] != null)
                {
                    multipart.Add(new StringContent(Fields["acl"].ToString()), "acl");
                }
                request.Content = multipart;
            }
Exemple #2
0
        public JObject Exec(string method, string name, JObject fields = null, string objectId = null, JObject queries = null, string path = null)
        {
            var s    = new NCMBSignature(_ncmb.ApplicationKey, _ncmb.ClientKey);
            var time = DateTime.Now;

            if (fields != null)
            {
                foreach (var key in new string[] { "objectId", "createDate", "updateDate" })
                {
                    if (fields.ContainsKey(key))
                    {
                        fields.Remove(key);
                    }
                }
            }
            if (fields != null)
            {
                foreach (KeyValuePair <string, JToken> key in fields)
                {
                    if (key.Value.Type == JTokenType.Date)
                    {
                        var date = new JObject();
                        date["__type"]  = "Date";
                        date["iso"]     = ((DateTime)key.Value).ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
                        fields[key.Key] = date;
                    }
                }
            }

            var signature = s.Generate(method, name, time, objectId, queries, path);
            var url       = s.Url(name, objectId, queries, path);
            var headers   = new Hashtable()
            {
                { "X-NCMB-Application-Key", _ncmb.ApplicationKey },
                { "X-NCMB-Timestamp", time.ToString("yyyy-MM-ddTHH:mm:ss.fffZ") },
                { "X-NCMB-Signature", signature },
                { "Content-Type", "application/json" }
            };

            if (_ncmb.SessionToken != null)
            {
                headers.Add("X-NCMB-Apps-Session-Token", _ncmb.SessionToken);
            }

            var client = new WebClient();

            foreach (string key in headers.Keys)
            {
                client.Headers[key] = headers[key].ToString();
            }
            client.Encoding = Encoding.UTF8;

            Console.WriteLine(queries);
            var response = method == "GET" ? System.Text.Encoding.UTF8.GetString(client.DownloadData(url)) : client.UploadString(url, method, fields != null ? fields.ToString(): "");

            if (method == "DELETE" && response == "")
            {
                return(new JObject());
            }
            return(JObject.Parse(response));
        }