Skip to content

Commit 2a838cb

Browse files
committed
update and fix database reference
1 parent 56c91df commit 2a838cb

File tree

2 files changed

+37
-61
lines changed

2 files changed

+37
-61
lines changed

.DS_Store

0 Bytes
Binary file not shown.

Assets/FirebaseREST/Database/DatabaseReference.cs

Lines changed: 37 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -481,17 +481,32 @@ List<string> GetQueries()
481481

482482
public override void GetValueAsync(int timeout, Action<Response<DataSnapshot>> OnComplete)
483483
{
484-
FetchFirebaseDatabase(this.ReferenceUrl, timeout, GetQueries(), OnComplete);
485-
}
484+
List<string> query = GetQueries();
485+
string url = this.ReferenceUrl;
486+
if (query != null)
487+
{
488+
url = url + "?" + string.Join("&", query.ToArray());
489+
}
486490

487-
public void Push(Dictionary<string, object> data, int timeout, Action<Response<string>> OnComplete)
488-
{
489-
PushFirebaseData(this.ReferenceUrl, Json.Serialize(data), timeout, OnComplete);
491+
UnityWebRequest webReq = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET);
492+
webReq.downloadHandler = new DownloadHandlerBuffer();
493+
webReq.timeout = timeout;
494+
495+
if (FirebaseAuth.Instance.IsSignedIn)
496+
{
497+
string sign = query == null ? "?" : "&";
498+
webReq.url = webReq.url + sign + "auth=" + FirebaseAuth.Instance.AccessToken;
499+
}
500+
UnityWebRequestAsyncOperation op = webReq.SendWebRequest();
501+
op.completed += ((ao) => HandleFirebaseDatabaseResponse(op, (res) =>
502+
{
503+
if (OnComplete != null)
504+
OnComplete(new Response<DataSnapshot>(null, true, (int)ResponseCode.SUCCESS, new FirebaseDataSnapshot(this, Json.Deserialize(res.data))));
505+
}));
490506
}
491507

492508
public void Push(object data, int timeout, Action<Response<string>> OnComplete)
493509
{
494-
ValidateValue(data);
495510
PushFirebaseData(this.ReferenceUrl, Json.Serialize(data), timeout, OnComplete);
496511
}
497512

@@ -514,8 +529,15 @@ public void SetRawJsonValueAsync(string json, int timeout, Action<Response> OnCo
514529

515530
public void SetValueAsync(object data, int timeout, Action<Response> OnComplete)
516531
{
517-
ValidateValue(data);
518-
WriteFirebaseData(this.ReferenceUrl, data, timeout, "PUT", OnComplete);
532+
try
533+
{
534+
data = Json.Serialize(data);
535+
WriteFirebaseData(this.ReferenceUrl, data, timeout, "PUT", OnComplete);
536+
}
537+
catch
538+
{
539+
throw new NotSupportedException("Not supported data types");
540+
}
519541
}
520542

521543
public void UpdateChildAsync(Dictionary<string, object> data, int timeout, Action<Response> OnComplete)
@@ -536,31 +558,6 @@ public void RemoveValueAsync(int timeout, Action<Response> OnComplete)
536558
op.completed += ((ao) => HandleFirebaseDatabaseResponse(op, OnComplete));
537559
}
538560

539-
object ValidateValue(object data)
540-
{
541-
int intValue;
542-
double doubleValue;
543-
bool boolValue;
544-
long longValue;
545-
if (data is string) return "\"" + data + "\"";
546-
else if (int.TryParse(data.ToString(), out intValue)) return data;
547-
else if (double.TryParse(data.ToString(), out doubleValue)) return data;
548-
else if (bool.TryParse(data.ToString(), out boolValue)) return data;
549-
else if (long.TryParse(data.ToString(), out longValue)) return data;
550-
else throw new NotSupportedException("Not supported data types");
551-
}
552-
553-
void SetValue(string dbpath, object data, int timeout, Action<Response> OnComplete)
554-
{
555-
data = ValidateValue(data);
556-
string[] arr = dbpath.Split('/');
557-
string keyToSet = arr[arr.Length - 1];
558-
dbpath = dbpath.Replace(keyToSet, string.Empty);
559-
Dictionary<string, object> dataToSave = new Dictionary<string, object>();
560-
dataToSave.Add(keyToSet, data);
561-
WriteFirebaseData(dbpath, data, timeout, "PUT", OnComplete);
562-
}
563-
564561
void PushFirebaseData(string dbpath, string rawData, int timeout, Action<Response<string>> OnComplete)
565562
{
566563
UnityWebRequest webReq = new UnityWebRequest(this.ReferenceUrl, "POST");
@@ -575,10 +572,14 @@ void PushFirebaseData(string dbpath, string rawData, int timeout, Action<Respons
575572
UnityWebRequestAsyncOperation op = webReq.SendWebRequest();
576573
op.completed += ((ao) => HandleFirebaseDatabaseResponse(op, (res) =>
577574
{
578-
Dictionary<string, object> data = Json.Deserialize(res.data) as Dictionary<string, object>;
579-
string pushedId = data["name"].ToString();
575+
string pushedId = null;
576+
if (res.success)
577+
{
578+
Dictionary<string, object> data = Json.Deserialize(res.data) as Dictionary<string, object>;
579+
pushedId = data["name"].ToString();
580+
}
580581
if (OnComplete != null)
581-
OnComplete(new Response<string>("success", true, (int)ResponseCode.SUCCESS, pushedId));
582+
OnComplete(new Response<string>(res.message, res.success, res.code, pushedId));
582583
}));
583584
}
584585

@@ -597,31 +598,6 @@ void WriteFirebaseData(string dbpath, object data, int timeout, string requestMe
597598
op.completed += ((ao) => HandleFirebaseDatabaseResponse(op, OnComplete));
598599
}
599600

600-
void FetchFirebaseDatabase(string dbpath, int timeout, List<string> query, Action<Response<DataSnapshot>> OnComplete)
601-
{
602-
string url = this.ReferenceUrl;
603-
if (query != null)
604-
{
605-
url = url + "?" + string.Join("&", query.ToArray());
606-
}
607-
608-
UnityWebRequest webReq = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET);
609-
webReq.downloadHandler = new DownloadHandlerBuffer();
610-
webReq.timeout = timeout;
611-
612-
if (FirebaseAuth.Instance.IsSignedIn)
613-
{
614-
string sign = query == null ? "?" : "&";
615-
webReq.url = webReq.url + sign + "auth=" + FirebaseAuth.Instance.AccessToken;
616-
}
617-
UnityWebRequestAsyncOperation op = webReq.SendWebRequest();
618-
op.completed += ((ao) => HandleFirebaseDatabaseResponse(op, (res) =>
619-
{
620-
if (OnComplete != null)
621-
OnComplete(new Response<DataSnapshot>(null, true, (int)ResponseCode.SUCCESS, new FirebaseDataSnapshot(this, Json.Deserialize(res.data))));
622-
}));
623-
}
624-
625601
void HandleFirebaseDatabaseResponse(UnityWebRequestAsyncOperation webReqOp, Action<Response> OnComplete)
626602
{
627603
if (webReqOp.webRequest.isNetworkError)

0 commit comments

Comments
 (0)