Esempio n. 1
0
        private IEnumerator DeleteByIDInternal <T>(string id, Action <CallbackResponse> onDeleteCompleted)
            where T : AppServiceObjectBase
        {
            using (UnityWebRequest www = AppServiceUtilities.BuildAppServiceWebRequest
                                             (GetEasyTablesUrl <T>() + "/" + WWW.EscapeURL(id), HttpMethod.Delete.ToString(), null, AuthenticationToken))
            {
                yield return(www.Send());

                if (Globals.DebugFlag)
                {
                    Debug.Log(www.responseCode);
                }

                CallbackResponse response = new CallbackResponse();

                if (Utilities.IsWWWError(www))
                {
                    if (Globals.DebugFlag)
                    {
                        Debug.Log(www.error);
                    }
                    Utilities.BuildResponseObjectOnFailure(response, www);
                }
                else
                {
                    response.Status = CallBackResult.Success;
                }
                onDeleteCompleted(response);
            }
        }
Esempio n. 2
0
        private IEnumerator CallAPIInternal <T, P>(string apiname,
                                                   HttpMethod method, Action <CallbackResponse <T[]> > onInvokeAPICompleted, EasyAPIReturnType returnType, P instance = null)
            where T : class
            where P : class
        {
            string json = null;

            if (instance != null)
            {
                json = JsonUtility.ToJson(instance);
            }

            using (UnityWebRequest www = AppServiceUtilities.BuildAppServiceWebRequest(GetApiUrl(apiname),
                                                                                       method.ToString(), json, AuthenticationToken))
            {
                yield return(www.Send());

                if (Globals.DebugFlag)
                {
                    Debug.Log(www.responseCode);
                }
                CallbackResponse <T[]> response = new CallbackResponse <T[]>();
                if (Utilities.IsWWWError(www))
                {
                    if (Globals.DebugFlag)
                    {
                        Debug.Log(www.error);
                    }
                    Utilities.BuildResponseObjectOnFailure(response, www);
                }
                else if (www.downloadHandler != null)  //all OK
                {
                    try
                    {
                        T[] returnObject = null;
                        if (returnType == EasyAPIReturnType.MultipleResults)
                        {
                            returnObject = JsonHelper.GetJsonArray <T>(www.downloadHandler.text);
                        }
                        else if (returnType == EasyAPIReturnType.SingleResult)
                        {
                            var returnObjectSingle = JsonUtility.FromJson <T>(www.downloadHandler.text);
                            returnObject = new T[] { returnObjectSingle };
                        }
                        response.Status = CallBackResult.Success;
                        response.Result = returnObject;
                    }
                    catch (Exception ex)
                    {
                        response.Status    = CallBackResult.DeserializationFailure;
                        response.Exception = ex;
                    }
                }
                onInvokeAPICompleted(response);
            }
        }
Esempio n. 3
0
        private IEnumerator InsertInternal <T>(T instance, Action <CallbackResponse <T> > onInsertCompleted)
            where T : AppServiceObjectBase
        {
            string json = JsonUtility.ToJson(instance);

            using (UnityWebRequest www = AppServiceUtilities.BuildAppServiceWebRequest(GetEasyTablesUrl <T>(),
                                                                                       HttpMethod.Post.ToString(), json, AuthenticationToken))
            {
                yield return(www.Send());

                if (Globals.DebugFlag)
                {
                    Debug.Log(www.responseCode);
                }

                CallbackResponse <T> response = new CallbackResponse <T>();

                if (Utilities.IsWWWError(www))
                {
                    if (Globals.DebugFlag)
                    {
                        Debug.Log(www.error);
                    }
                    Utilities.BuildResponseObjectOnFailure(response, www);
                }

                else if (www.downloadHandler != null)  //all OK
                {
                    //let's get the new object that was created
                    try
                    {
                        T newObject = JsonUtility.FromJson <T>(www.downloadHandler.text);
                        if (Globals.DebugFlag)
                        {
                            Debug.Log("new object ID is " + newObject.id);
                        }
                        response.Status = CallBackResult.Success;
                        response.Result = newObject;
                    }
                    catch (Exception ex)
                    {
                        response.Status    = CallBackResult.DeserializationFailure;
                        response.Exception = ex;
                    }
                }
                onInsertCompleted(response);
            }
        }
Esempio n. 4
0
        private IEnumerator SelectByIDInternal <T>(string id, Action <CallbackResponse <T> > onSelectByIDCompleted)
            where T : AppServiceObjectBase
        {
            using (UnityWebRequest www = AppServiceUtilities.BuildAppServiceWebRequest
                                             (GetEasyTablesUrl <T>() + "/" + WWW.EscapeURL(id), HttpMethod.Get.ToString(), null, AuthenticationToken))
            {
                yield return(www.Send());

                if (Globals.DebugFlag)
                {
                    Debug.Log(www.responseCode);
                }
                CallbackResponse <T> response = new CallbackResponse <T>();
                if (Utilities.IsWWWError(www))
                {
                    if (Globals.DebugFlag)
                    {
                        Debug.Log(www.error);
                    }
                    Utilities.BuildResponseObjectOnFailure(response, www);
                }
                else
                {
                    try
                    {
                        T data = JsonUtility.FromJson <T>(www.downloadHandler.text);
                        response.Result = data;
                        response.Status = CallBackResult.Success;
                    }
                    catch (Exception ex)
                    {
                        response.Status    = CallBackResult.DeserializationFailure;
                        response.Exception = ex;
                    }
                }
                onSelectByIDCompleted(response);
            }
        }
Esempio n. 5
0
        private IEnumerator SelectFilteredInternal <T>(TableQuery query,
                                                       Action <CallbackResponse <SelectFilteredResult <T> > > onSelectCompleted)
            where T : AppServiceObjectBase
        {
            string url = GetEasyTablesUrl <T>();

            if (query != null)
            {
                url += query.ToString();
            }
            if (Globals.DebugFlag)
            {
                Debug.Log(url);
            }
            using (UnityWebRequest www = AppServiceUtilities.BuildAppServiceWebRequest(url,
                                                                                       HttpMethod.Get.ToString(), null, AuthenticationToken))
            {
                yield return(www.Send());

                if (Globals.DebugFlag)
                {
                    Debug.Log(www.responseCode);
                }

                CallbackResponse <SelectFilteredResult <T> > response = new CallbackResponse <SelectFilteredResult <T> >();

                if (Utilities.IsWWWError(www))
                {
                    if (Globals.DebugFlag)
                    {
                        Debug.Log(www.error);
                    }
                    Utilities.BuildResponseObjectOnFailure(response, www);
                }
                else
                {
                    try
                    {
                        response.Status = CallBackResult.Success;

                        //reported issue when fetching many rows
                        string textResponse = www.downloadHandler.text.Replace("\n", "");

                        //reported issue on macOS
                        if (textResponse.Trim() == string.Empty)
                        {
                            throw new Exception("downloadHandler is empty");
                        }

                        SelectFilteredResult <T> selectFilteredResult = null;
                        if (query.inlineCount)
                        {
                            selectFilteredResult = JsonUtility.FromJson <SelectFilteredResult <T> >(textResponse);
                        }
                        else
                        {
                            selectFilteredResult = new SelectFilteredResult <T>();
                            T[] data = JsonHelper.GetJsonArray <T>(www.downloadHandler.text);
                            selectFilteredResult.results = data;
                            selectFilteredResult.count   = -1;
                        }
                        response.Result = selectFilteredResult;
                    }
                    catch (Exception ex)
                    {
                        Utilities.BuildResponseObjectOnException(response, ex);
                    }
                }
                onSelectCompleted(response);
            }
        }