/// <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="query">REST call parameters wrapped in FirebaseQuery class</param> /// <returns></returns> public void GetValue(FirebaseParam query) { try { if (Credential != "") { query = new FirebaseParam(query).Auth(Credential); } string url = Endpoint; string param = WWW.EscapeURL(query.Parameter); if (param != "") { url += "?" + param; } root.StartCoroutine(RequestCoroutine(url, null, query.HttpHeader, OnGetSuccess, OnGetFailed)); } catch (WebException webEx) { if (OnGetFailed != null) { OnGetFailed(this, FirebaseError.Create(webEx)); } } catch (Exception ex) { if (OnGetFailed != null) { OnGetFailed(this, new FirebaseError(ex.Message)); } } }
/*** RULES ***/ /// <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 <Firebase, DataSnapshot> OnSuccess, Action <Firebase, FirebaseError> 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, FirebaseError.Create(webEx)); } } catch (Exception ex) { if (OnFailed != null) { OnFailed(this, new FirebaseError(ex.Message)); } } }
/*** SET ***/ /// <summary> /// Set value of a key on Firebase. Calls OnSetSuccess on success, OnSetFailed on failed. /// OnSetSuccess action contains the corresponding Firebase and the response Snapshot /// OnSetFailed action contains the error exception /// </summary> /// <param name="valJson">Set value in json format</param> /// <param name="query">REST call parameters wrapped in FirebaseQuery class</param> /// <returns></returns> void SetValueJson(string valJson, FirebaseParam query) { try { if (Credential != "") { query = new FirebaseParam(query).Auth(Credential); } string url = Endpoint; #pragma warning disable CS0618 // Type or member is obsolete string param = WWW.EscapeURL(query.Parameter); #pragma warning restore CS0618 // Type or member is obsolete if (param != "") { url += "?" + param; } Dictionary <string, string> headers = new Dictionary <string, string>(); foreach (var kv in query.HttpHeader) { headers.Add(kv.Key, kv.Value); } headers.Add("Content-Type", "application/json"); if (!headers.ContainsKey("X-HTTP-Method-Override")) { headers.Add("X-HTTP-Method-Override", "PUT"); } //UTF8Encoding encoding = new UTF8Encoding(); byte[] bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(valJson); root.StartCoroutine(RequestCoroutine(url, bytes, headers, OnSetSuccess, OnSetFailed)); } catch (WebException webEx) { if (OnSetFailed != null) { OnSetFailed(this, FirebaseError.Create(webEx)); } } catch (Exception ex) { if (OnSetFailed != null) { OnSetFailed(this, new FirebaseError(ex.Message)); } } }
/*** DELETE ***/ /// <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="query">REST call parameters wrapped in FirebaseQuery class</param> /// <returns></returns> public void Delete(FirebaseParam query) { try { if (Credential != "") { query = new FirebaseParam(query).Auth(Credential); } string url = Endpoint; string param = WWW.EscapeURL(query.Parameter); if (param != string.Empty) { url += "?" + param; } Dictionary <string, string> headers = new Dictionary <string, string>(); foreach (var kv in query.HttpHeader) { headers.Add(kv.Key, kv.Value); } headers.Add("Content-Type", "application/json"); if (!headers.ContainsKey("X-HTTP-Method-Override")) { 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, FirebaseError.Create(webEx)); } } catch (Exception ex) { if (OnDeleteFailed != null) { OnDeleteFailed(this, new FirebaseError(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 FirebaseError((HttpStatusCode)400, "Invalid data; couldn't parse JSON object. Are you sending a JSON object with valid key names?")); } return; } if (Credential != "") { param = (new FirebaseParam(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, FirebaseError.Create(webEx)); } } catch (Exception ex) { if (OnUpdateFailed != null) { OnUpdateFailed(this, new FirebaseError(ex.Message)); } } }
/// <summary> /// Set value of a key on Firebase. Calls OnUpdateSuccess on success, OnUpdateFailed on failed. /// OnSetSuccess action contains the corresponding Firebase and the response Snapshot /// OnSetFailed 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 SetValue(object _val, string param = "") { try { if (Credential != "") { param = (new FirebaseParam(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", "PUT"); //UTF8Encoding encoding = new UTF8Encoding(); byte[] bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(Json.Serialize(_val)); root.StartCoroutine(RequestCoroutine(url, bytes, headers, OnSetSuccess, OnSetFailed)); } catch (WebException webEx) { if (OnSetFailed != null) { OnSetFailed(this, FirebaseError.Create(webEx)); } } catch (Exception ex) { if (OnSetFailed != null) { OnSetFailed(this, new FirebaseError(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 <Firebase, DataSnapshot> OnSuccess, Action <Firebase, FirebaseError> 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, FirebaseError.Create(webEx)); } } catch (Exception ex) { if (OnFailed != null) { OnFailed(this, new FirebaseError(ex.Message)); } } }
/*** PUSH ***/ /// <summary> /// Push a value (with random new key) on a key in Firebase. Calls OnPushSuccess on success, OnPushFailed on failed. /// OnPushSuccess action contains the corresponding Firebase and the response Snapshot /// OnPushFailed action contains the error exception /// </summary> /// <param name="valJson">Push value in json format</param> /// <param name="query">REST call parameters wrapped in FirebaseQuery class</param> /// <returns></returns> void PushJson(string valJson, FirebaseParam query) { try { if (Credential != "") { query = new FirebaseParam(query).Auth(Credential); } string url = Endpoint; #pragma warning disable CS0618 // Type or member is obsolete string param = WWW.EscapeURL(query.Parameter); #pragma warning restore CS0618 // Type or member is obsolete if (param != string.Empty) { url += "?" + param; } //UTF8Encoding encoding = new UTF8Encoding(); byte[] bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(valJson); root.StartCoroutine(RequestCoroutine(url, bytes, query.HttpHeader, OnPushSuccess, OnPushFailed)); } catch (WebException webEx) { if (OnPushFailed != null) { OnPushFailed(this, FirebaseError.Create(webEx)); } } catch (Exception ex) { if (OnPushFailed != null) { OnPushFailed(this, new FirebaseError(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 FirebaseParam(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, FirebaseError.Create(webEx)); } } catch (Exception ex) { if (OnPushFailed != null) { OnPushFailed(this, new FirebaseError(ex.Message)); } } }