public static void BuildResponseObjectOnFailure(CallbackResponse response, UnityWebRequest www)
        {
            if (www.responseCode == 404L)
            {
                response.Status = CallBackResult.NotFound;
            }
            else if (www.responseCode == 409L)
            {
                response.Status = CallBackResult.ResourceExists;
            }
            else
            {
                response.Status = CallBackResult.Failure;
            }

            string errorMessage = www.error;

            if (errorMessage == null && www.downloadHandler != null && !string.IsNullOrEmpty(www.downloadHandler.text))
            {
                errorMessage = www.downloadHandler.text;
            }
            else
            {
                errorMessage = Globals.ErrorOccurred;
            }

            Exception ex = new Exception(errorMessage);

            response.Exception = ex;
        }
Beispiel #2
0
        public IEnumerator CreateTableIfNotExistsInternal(string tableName, Action <CallbackResponse> onCreateTableCompleted)
        {
            string url = Url + "Tables()";

            string json = string.Format("{{\"TableName\":\"{0}\"}}", tableName);

            using (UnityWebRequest www = StorageUtilities.BuildStorageWebRequest(url, HttpMethod.Post.ToString(), accountName, json))

            {
                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 ?? "Error " + www.responseCode);
                    }
                    Utilities.BuildResponseObjectOnFailure(response, www);
                }
                else
                {
                    response.Status = CallBackResult.Success;
                }
                onCreateTableCompleted(response);
            }
        }
Beispiel #3
0
        private IEnumerator InsertInternal <T>(T instance, string tableName, Action <CallbackResponse> onInsertCompleted)
            where T : TableEntity
        {
            string url = string.Format("{0}{1}()", Url, tableName);

            string json = JsonUtility.ToJson(instance);

            using (UnityWebRequest www =
                       StorageUtilities.BuildStorageWebRequest(url, HttpMethod.Post.ToString(), accountName, json))
            {
                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 ?? "Error " + www.responseCode);
                    }
                    Utilities.BuildResponseObjectOnFailure(response, www);
                }

                else if (www.downloadHandler != null)  //all OK
                {
                    //let's get the header for the new object that was created

                    string dataServiceId = www.GetResponseHeader("Location");
                    if (Globals.DebugFlag)
                    {
                        Debug.Log("new object ID is " + dataServiceId);
                    }
                    response.Status = CallBackResult.Success;
                }
                onInsertCompleted(response);
            }
        }
Beispiel #4
0
        private IEnumerator QueryTableInternal <T>(TableQuery query, string tableName, Action <CallbackResponse <T[]> > onQueryTableCompleted)
            where T : TableEntity
        {
            string url = string.Format("{0}{1}(){2}", Url, tableName, query.ToString());


            using (UnityWebRequest www =
                       StorageUtilities.BuildStorageWebRequest(url, HttpMethod.Get.ToString(), accountName, string.Empty))
            {
                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 ?? "Error " + www.responseCode);
                    }
                    Utilities.BuildResponseObjectOnFailure(response, www);
                }
                else if (www.downloadHandler != null)  //all OK
                {
                    //let's get the header for the new object that was created
                    T[] data = JsonHelper.GetJsonArrayFromTableStorage <T>(www.downloadHandler.text);
                    if (Globals.DebugFlag)
                    {
                        Debug.Log("Received " + data.Length + " objects");
                    }

                    response.Result = data;
                    response.Status = CallBackResult.Success;
                }
                onQueryTableCompleted(response);
            }
        }
Beispiel #5
0
        private IEnumerator InsertOrMergeInternal <T>(T instance, string tableName, Action <CallbackResponse> onInsertOrMergeCompleted)
            where T : TableEntity
        {
            string url = string.Format("{0}{1}(PartitionKey='{2}', RowKey='{3}')", Url, tableName, instance.PartitionKey, instance.RowKey);

            string json = JsonUtility.ToJson(instance);

            using (UnityWebRequest www =
                       StorageUtilities.BuildStorageWebRequest(url, HttpMethod.Merge.ToString(), accountName, json))
            {
                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 ?? "Error " + www.responseCode);
                    }
                    Utilities.BuildResponseObjectOnFailure(response, www);
                }

                else if (www.downloadHandler != null)  //all OK
                {
                    if (Globals.DebugFlag)
                    {
                        Debug.Log("successfully inserted or merged object");
                    }
                    response.Status = CallBackResult.Success;
                }
                onInsertOrMergeCompleted(response);
            }
        }
 public static void BuildResponseObjectOnException(CallbackResponse response, Exception ex)
 {
     response.Status    = CallBackResult.LocalException;
     response.Exception = ex;
 }