/// <summary>
        /// Gets Firebase Rules. Returned value is treated the same as returned value on Get request, packaged in DataSnapshot. Please note that FIREBASE_SECRET is required. If secret parameter is not set, it will use the Credential that has been set when CreateNew called.
        /// </summary>
        /// <param name="OnSuccess">On success callback.</param>
        /// <param name="OnFailed">On failed callback.</param>
        /// <param name="secret">Firebase Secret.</param>
        public void GetRules(Action <SimpleFirebase, SimpleDataSnapshot> OnSuccess, Action <SimpleFirebase, SimpleFirebaseError> OnFailed, string secret = "")
        {
            try
            {
                if (string.IsNullOrEmpty(secret))
                {
                    if (!string.IsNullOrEmpty(Credential))
                    {
                        secret = Credential;
                    }
                }

                string url = RulesEndpoint;

                url += "?auth=" + secret;

                root.StartCoroutine(RequestCoroutine(url, null, null, OnSuccess, OnFailed));
            }
            catch (WebException webEx)
            {
                if (OnFailed != null)
                {
                    OnFailed(this, SimpleFirebaseError.Create(webEx));
                }
            }
            catch (Exception ex)
            {
                if (OnFailed != null)
                {
                    OnFailed(this, new SimpleFirebaseError(ex.Message));
                }
            }
        }
        /// <summary>
        /// Fetch data from Firebase. Calls OnGetSuccess on success, OnGetFailed on failed.
        /// OnGetSuccess action contains the corresponding Firebase and the fetched Snapshot
        /// OnGetFailed action contains the error exception
        /// </summary>
        /// <param name="param">REST call parameters on a string. Example: &quot;orderBy=&#92;"$key&#92;"&quot;print=pretty&quot;shallow=true"></param>
        /// <returns></returns>
        public void GetValue(string param = "")
        {
            try
            {
                if (Credential != "")
                {
                    param = (new SimpleFirebaseParam(param).Auth(Credential)).Parameter;
                }

                string url = Endpoint;

                param = WWW.EscapeURL(param);

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

                root.StartCoroutine(RequestCoroutine(url, null, null, OnGetSuccess, OnGetFailed));
            }
            catch (WebException webEx)
            {
                if (OnGetFailed != null)
                {
                    OnGetFailed(this, SimpleFirebaseError.Create(webEx));
                }
            }
            catch (Exception ex)
            {
                if (OnGetFailed != null)
                {
                    OnGetFailed(this, new SimpleFirebaseError(ex.Message));
                }
            }
        }
        /// <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">Set value</param>
        /// <param name="param">REST call parameters on a string. Example: "auth=ASDF123"</param>
        /// <returns></returns>
        public void UpdateValue(object _val, string param = "")
        {
            try
            {
                if (!(_val is Dictionary <string, object>))
                {
                    if (OnUpdateFailed != null)
                    {
                        OnUpdateFailed(this, new SimpleFirebaseError((HttpStatusCode)400, "Invalid data; couldn't parse JSON object. Are you sending a JSON object with valid key names?"));
                    }

                    return;
                }

                if (Credential != "")
                {
                    param = (new SimpleFirebaseParam(param).Auth(Credential)).Parameter;
                }

                string url = Endpoint;

                param = WWW.EscapeURL(param);

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

                Dictionary <string, string> headers = new Dictionary <string, string>();
                headers.Add("Content-Type", "application/json");
                headers.Add("X-HTTP-Method-Override", "PATCH");

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

                root.StartCoroutine(RequestCoroutine(url, bytes, headers, OnUpdateSuccess, OnUpdateFailed));
            }
            catch (WebException webEx)
            {
                if (OnUpdateFailed != null)
                {
                    OnUpdateFailed(this, SimpleFirebaseError.Create(webEx));
                }
            }
            catch (Exception ex)
            {
                if (OnUpdateFailed != null)
                {
                    OnUpdateFailed(this, new SimpleFirebaseError(ex.Message));
                }
            }
        }
        /// <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 SimpleFirebaseParam(param).Auth(Credential)).Parameter;
                }

                string url = Endpoint;

                param = WWW.EscapeURL(param);

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

                Dictionary <string, string> headers = new Dictionary <string, string>();
                headers.Add("Content-Type", "application/json");
                headers.Add("X-HTTP-Method-Override", "DELETE");

                //UTF8Encoding encoding = new UTF8Encoding();
                byte[] bytes = Encoding.GetEncoding("iso-8859-1").GetBytes("{ \"dummy\" : \"dummies\"}");

                root.StartCoroutine(RequestCoroutine(url, bytes, headers, OnDeleteSuccess, OnDeleteFailed));
            }
            catch (WebException webEx)
            {
                if (OnDeleteFailed != null)
                {
                    OnDeleteFailed(this, SimpleFirebaseError.Create(webEx));
                }
            }
            catch (Exception ex)
            {
                if (OnDeleteFailed != null)
                {
                    OnDeleteFailed(this, new SimpleFirebaseError(ex.Message));
                }
            }
        }
        /// <summary>
        /// Sets Firebase Rules. Returned value is treated the same as returned value on Set request, packaged in DataSnapshot.Please note that FIREBASE_SECRET is required. If secret parameter is not set, it will use the Credential that has been set when CreateNew called.
        /// </summary>
        /// <param name="json">Valid rules Json.</param>
        /// <param name="OnSuccess">On success callback.</param>
        /// <param name="OnFailed">On failed callback.</param>
        /// <param name="secret">Firebase Secret.</param>
        public void SetRules(string json, Action <SimpleFirebase, SimpleDataSnapshot> OnSuccess, Action <SimpleFirebase, SimpleFirebaseError> OnFailed, string secret = "")
        {
            try
            {
                if (string.IsNullOrEmpty(secret))
                {
                    if (!string.IsNullOrEmpty(Credential))
                    {
                        secret = Credential;
                    }
                }

                string url = RulesEndpoint;

                url += "?auth=" + secret;

                Dictionary <string, string> headers = new Dictionary <string, string>();
                headers.Add("Content-Type", "application/json");
                headers.Add("X-HTTP-Method-Override", "PUT");

                //UTF8Encoding encoding = new UTF8Encoding();
                byte[] bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(json);

                root.StartCoroutine(RequestCoroutine(url, bytes, headers, OnSuccess, OnFailed));
            }
            catch (WebException webEx)
            {
                if (OnFailed != null)
                {
                    OnFailed(this, SimpleFirebaseError.Create(webEx));
                }
            }
            catch (Exception ex)
            {
                if (OnFailed != null)
                {
                    OnFailed(this, new SimpleFirebaseError(ex.Message));
                }
            }
        }
        /// <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">New value</param>
        /// <param name="param">REST call parameters on a string. Example: "auth=ASDF123"</param>
        /// <returns></returns>
        public void Push(object _val, string param = "")
        {
            try
            {
                if (Credential != "")
                {
                    param = (new SimpleFirebaseParam(param).Auth(Credential)).Parameter;
                }

                string url = Endpoint;

                param = WWW.EscapeURL(param);

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


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

                root.StartCoroutine(RequestCoroutine(url, bytes, null, OnPushSuccess, OnPushFailed));
            }
            catch (WebException webEx)
            {
                if (OnPushFailed != null)
                {
                    OnPushFailed(this, SimpleFirebaseError.Create(webEx));
                }
            }
            catch (Exception ex)
            {
                if (OnPushFailed != null)
                {
                    OnPushFailed(this, new SimpleFirebaseError(ex.Message));
                }
            }
        }
Esempio n. 7
0
 protected void OnFailed(SimpleFirebase sender, SimpleFirebaseError err)
 {
     --count;
     StartNextCommand();
     ClearCallbacks(sender);
 }