Ejemplo n.º 1
0
        private IEnumerator DeleteByIDInternal <T>(string id, Action <CallbackResponse> onDeleteCompleted)
            where T : AzureObjectBase
        {
            using (UnityWebRequest www = Utilities.BuildWebRequest
                                             (GetTablesUrl <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);
            }
        }
Ejemplo 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 = Utilities.BuildWebRequest(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);
            }
        }
        public static void BuildResponseObjectOnFailure(CallbackResponse response, UnityWebRequest www)
        {
            if (www.responseCode == 404L)
            {
                response.Status = CallBackResult.NotFound;
            }
            else
            {
                response.Status = CallBackResult.Failure;
            }

            Exception ex = new Exception(www.error ?? Constants.ErrorOccurred);

            response.Exception = ex;
        }
Ejemplo n.º 4
0
        private IEnumerator InsertInternal <T>(T instance, Action <CallbackResponse <T> > onInsertCompleted)
            where T : AzureObjectBase
        {
            string json = JsonUtility.ToJson(instance);

            using (UnityWebRequest www = Utilities.BuildWebRequest(GetTablesUrl <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);
            }
        }
        private IEnumerator SelectFilteredInternal <T>(IQueryable <T> query, Action <CallbackResponse <T[]> > onSelectCompleted)
            where T : AzureObjectBase
        {
            string url = GetTablesUrl <T>();

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

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

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

                if (Utilities.IsWWWError(www))
                {
                    if (DebugFlag)
                    {
                        Debug.Log(www.error);
                    }
                    Utilities.BuildResponseObjectOnFailure(response, www);
                }
                else
                {
                    response.Status = CallBackResult.Success;
                    T[] data = Utilities.DeserializeJsonArray <T>(www.downloadHandler.text);
                    response.Result = data;
                }
                onSelectCompleted(response);
            }
        }
        private IEnumerator CallAPIInternal <T>(string apiname, HttpMethod method, Action <CallbackResponse <T> > onInvokeAPICompleted)
            where T : class
        {
            using (UnityWebRequest www = Utilities.BuildWebRequest(GetApiUrl(apiname),
                                                                   method.ToString(), null, AuthenticationToken))
            {
                yield return(www.Send());

                if (DebugFlag)
                {
                    Debug.Log(www.responseCode);
                }
                CallbackResponse <T> response = new CallbackResponse <T>();
                if (Utilities.IsWWWError(www))
                {
                    if (DebugFlag)
                    {
                        Debug.Log(www.error);
                    }
                    Utilities.BuildResponseObjectOnFailure(response, www);
                }
                else if (www.downloadHandler != null)  //all OK
                {
                    try
                    {
                        //FromJson method does not do well with single quotes...
                        T returnObject = JsonUtility.FromJson <T>(www.downloadHandler.text);
                        response.Status = CallBackResult.Success;
                        response.Result = returnObject;
                    }
                    catch (Exception ex)
                    {
                        response.Status    = CallBackResult.DeserializationFailure;
                        response.Exception = ex;
                    }
                }
                onInvokeAPICompleted(response);
            }
        }
Ejemplo n.º 7
0
        private IEnumerator SelectByIDInternal <T>(string id, Action <CallbackResponse <T> > onSelectByIDCompleted)
            where T : AzureObjectBase
        {
            using (UnityWebRequest www = Utilities.BuildWebRequest
                                             (GetTablesUrl <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);
            }
        }
Ejemplo n.º 8
0
        private IEnumerator SelectFilteredInternal <T>(EasyTableQuery <T> query,
                                                       Action <CallbackResponse <SelectFilteredResult <T> > > onSelectCompleted)
            where T : AzureObjectBase
        {
            string url = GetTablesUrl <T>();

            if (query != null)
            {
                url += "?" + query.ToODataString();
            }
            if (Globals.DebugFlag)
            {
                Debug.Log(url);
            }
            using (UnityWebRequest www = Utilities.BuildWebRequest(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 OSX
                        if (textResponse.Trim() == string.Empty)
                        {
                            throw new Exception("downloadHandler is empty");
                        }

                        SelectFilteredResult <T> selectFilteredResult = null;
                        if (query.RequestTotalCount)
                        {
                            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);
            }
        }