Ejemplo n.º 1
0
        /// <summary>
        /// Delete a key in Firebase. Calls OnDeleteSuccess on success, OnDeleteFailed on failed.
        /// OnDeleteSuccess action contains the corresponding Firebase and the response Snapshot
        /// OnDeleteFailed action contains the error exception
        /// </summary>
        /// <param name="param">REST call parameters on a string. Example: "auth=ASDF123"</param>
        /// <returns></returns>
        public void Delete(string param = "")
        {
            try
            {
                if (Credential != "")
                {
                    param = (new FirebaseParam(param).Auth(Credential)).Parameter;
                }

                string url = Endpoint;

                if (param != string.Empty)
                {
                    url += "?" + param;
                }

                HttpWebRequest rq = (HttpWebRequest)WebRequest.Create(url);

                FieldInfo headersFieldInfo = rq.GetType().GetField("webHeaders", System.Reflection.BindingFlags.NonPublic
                                                                   | System.Reflection.BindingFlags.Instance
                                                                   | System.Reflection.BindingFlags.GetField);

                CusteredHeaderCollection WssHeaders = new CusteredHeaderCollection(Host);

                headersFieldInfo.SetValue(rq, WssHeaders);

                rq.Proxy = null;

                rq.Method        = "DELETE";
                rq.ContentLength = 0;
                rq.ContentType   = "application/json";

                using (HttpWebResponse resp = (HttpWebResponse)rq.GetResponse())
                {
                    string responseValue = string.Empty;

                    if (resp.StatusCode == HttpStatusCode.NoContent)
                    {
                        DataSnapshot snapshot = new DataSnapshot();
                        if (OnDeleteSuccess != null)
                        {
                            OnDeleteSuccess(this, snapshot);
                        }
                        return;
                    }

                    if (resp.StatusCode != HttpStatusCode.OK)
                    {
                        if (OnDeleteFailed != null)
                        {
                            OnDeleteFailed(this, FirebaseError.Create(resp.StatusCode));
                        }
                        return;
                    }

                    using (Stream responseStream = resp.GetResponseStream())
                    {
                        if (responseStream != null)
                        {
                            using (StreamReader rdr = new StreamReader(responseStream))
                            {
                                responseValue = rdr.ReadToEnd();
                            }
                        }
                    }

                    if (responseValue != "")
                    {
                        DataSnapshot snapshot = new DataSnapshot(responseValue);
                        if (OnDeleteSuccess != null)
                        {
                            OnDeleteSuccess(this, snapshot);
                        }
                    }
                    else
                    {
                        if (OnDeleteFailed != null)
                        {
                            OnDeleteFailed(this, new FirebaseError(resp.StatusCode, "No response received."));
                        }
                    }
                }
            }
            catch (WebException webEx)
            {
                if (OnDeleteFailed != null)
                {
                    OnDeleteFailed(this, FirebaseError.Create(webEx));
                }
            }
            catch (Exception ex)
            {
                if (OnDeleteFailed != null)
                {
                    OnDeleteFailed(this, new FirebaseError(ex.Message));
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Update value of a key on Firebase. Calls OnUpdateSuccess on success, OnUpdateFailed on failed.
        /// OnUpdateSuccess action contains the corresponding Firebase and the response Snapshot
        /// OnUpdateFailed action contains the error exception
        /// </summary>
        /// <param name="_val">Update value</param>
        /// <param name="param">REST call parameters on a string. Example: "auth=ASDF123"</param>
        /// <returns></returns>
        public void SetValue(object _val, string param = "")
        {
            try
            {
                if (Credential != "")
                {
                    param = (new FirebaseParam(param).Auth(Credential)).Parameter;
                }

                string url;

                if (_val is Dictionary <string, object> )
                {
                    url = Endpoint;
                }
                else
                {
                    if (parent == null)
                    {
                        if (OnUpdateFailed != null)
                        {
                            OnUpdateFailed(this, new FirebaseError("Cannot set non-{key:value} object to root Firebase."));
                        }
                        return;
                    }

                    url = parent.Endpoint;

                    Dictionary <string, object> tempDict = new Dictionary <string, object>();
                    tempDict[key] = _val;
                    _val          = tempDict;
                }

                if (param != string.Empty)
                {
                    url += "?" + param;
                }

                HttpWebRequest rq = (HttpWebRequest)WebRequest.Create(url);


                FieldInfo headersFieldInfo = rq.GetType().GetField("webHeaders", System.Reflection.BindingFlags.NonPublic
                                                                   | System.Reflection.BindingFlags.Instance
                                                                   | System.Reflection.BindingFlags.GetField);


                CusteredHeaderCollection WssHeaders = new CusteredHeaderCollection(Host);

                headersFieldInfo.SetValue(rq, WssHeaders);

                rq.Proxy = null;

                rq.Method        = "PATCH";
                rq.ContentLength = 0;
                rq.ContentType   = "application/json";



                //UTF8Encoding encoding = new UTF8Encoding();
                byte[] bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(Json.Serialize(_val));
                rq.ContentLength = bytes.Length;

                using (Stream writeStream = rq.GetRequestStream())
                {
                    writeStream.Write(bytes, 0, bytes.Length);
                }

                using (HttpWebResponse resp = (HttpWebResponse)rq.GetResponse())
                {
                    string responseValue = string.Empty;

                    if (resp.StatusCode == HttpStatusCode.NoContent)
                    {
                        DataSnapshot snapshot = new DataSnapshot();
                        if (OnUpdateSuccess != null)
                        {
                            OnUpdateSuccess(this, snapshot);
                        }
                        return;
                    }

                    if (resp.StatusCode != HttpStatusCode.OK)
                    {
                        if (OnUpdateFailed != null)
                        {
                            OnUpdateFailed(this, FirebaseError.Create(resp.StatusCode));
                        }
                        return;
                    }

                    using (Stream responseStream = resp.GetResponseStream())
                    {
                        if (responseStream != null)
                        {
                            using (StreamReader rdr = new StreamReader(responseStream))
                            {
                                responseValue = rdr.ReadToEnd();
                            }
                        }
                    }

                    if (responseValue != "")
                    {
                        DataSnapshot snapshot = new DataSnapshot(responseValue);
                        if (OnUpdateSuccess != null)
                        {
                            OnUpdateSuccess(this, snapshot);
                        }
                    }
                    else
                    {
                        if (OnUpdateFailed != null)
                        {
                            OnUpdateFailed(this, new FirebaseError(resp.StatusCode, "No response received."));
                        }
                    }
                }
            }
            catch (WebException webEx)
            {
                if (OnUpdateFailed != null)
                {
                    OnUpdateFailed(this, FirebaseError.Create(webEx));
                }
            }
            catch (Exception ex)
            {
                if (OnUpdateFailed != null)
                {
                    OnUpdateFailed(this, new FirebaseError(ex.Message));
                }
            }
        }