private static void CreateRoutineObject()
 {
     if (mRoutineObject != null)
     {
         GameObject.Destroy(mRoutineObject.gameObject);
     }
     mRoutineObject = new GameObject("WebRoutineHelper").AddComponent <WebCallRoutineObject>();
 }
        public IEnumerator GETRoutine(WebCallRoutineObject routineObject)
        {
            IsDone = false;

            bool isTimeout = false;

            if (Input.GetKey(KeyCode.Q))
            {
                isTimeout = true;
            }

            if (!isTimeout)
            {
                // Everything is fine, prepare to perform the call
                if (Backend.PrintDebug)
                {
                    Debug.Log("=> " + URL + "\nHeaders: " + Json.Serialize(Headers));
                }

                // Execute the request
                mWWW = new WWW(URL, null, Headers);

                float timeout = Time.time + TIME_OUT;

                // Wait until the request is done or has timed out
                while (!mWWW.isDone && Time.time < timeout)
                {
                    yield return(null);
                }

                isTimeout = !mWWW.isDone;
            }

            IsDone = true;

            // If the routine object is null the system has reset, probably because of a log out
            // Ignore whatever result should have occured then
            if (routineObject != null)
            {
                if (isTimeout)
                {
                    HandleTimeout();
                }
                else
                {
                    HandleResult();
                }
            }

            if (isTimeout)
            {
                PoorConnectionIndicator.ReportTimeout();
            }
            else
            {
                PoorConnectionIndicator.ReportSuccess();
            }
        }
 public void ExecuteCall(WebCallRoutineObject routineObject)
 {
     if (PostDictionary != null)
     {
         routineObject.StartCoroutine(POSTRoutine(routineObject));
     }
     else
     {
         routineObject.StartCoroutine(GETRoutine(routineObject));
     }
 }
        public IEnumerator POSTRoutine(WebCallRoutineObject routineObject)
        {
            IsDone = false;

            #region An image is supposed to be included, first upload the image
            string imageUUID = null;

            if (PostDictionary != null && PostDictionary.ContainsKey(KEY_POST_IMAGE))
            {
                imageUUID = PostDictionary[KEY_POST_IMAGE] as string;
            }

            if (string.IsNullOrEmpty(imageUUID) && !string.IsNullOrEmpty(ImageData))
            {
                bool imageIsDone = false;

                Backend.UploadImage(ImageData, ImageName,
                                    x =>
                {
                    imageUUID   = x;
                    imageIsDone = true;
                });

                while (!imageIsDone)
                {
                    yield return(null);
                }
            }
            #endregion

            // If there should be an image but the upload failed...
            bool isImageFail = !string.IsNullOrEmpty(ImageData) && string.IsNullOrEmpty(imageUUID);
            bool isTimeout   = false;

            if (Input.GetKey(KeyCode.Q))
            {
                isTimeout = true;
            }

            // ... then don't perform the remainder of the post
            if (!isImageFail && !isTimeout)
            {
                // Everything is fine, prepare to perform the call

                // If there's post data construct a byte[] to post
                byte[] postData = null;

                if (PostDictionary != null)
                {
                    // Add the uploaded image to the data
                    if (!string.IsNullOrEmpty(imageUUID))
                    {
                        PostDictionary[KEY_POST_IMAGE] = imageUUID;
                    }

                    PostJSON = Json.Serialize(PostDictionary);
                    postData = System.Text.Encoding.UTF8.GetBytes(PostJSON);
                }

                if (Backend.PrintDebug)
                {
                    Debug.Log("=> " + URL + "\nHeaders: " + Json.Serialize(Headers) + "\nData:" + SafePostJSON);
                }

                // Execute the request
                mWWW = new WWW(URL, postData, Headers);

                float timeout = Time.time + TIME_OUT;

                // Wait until the request is done or has timed out
                while (!mWWW.isDone && Time.time < timeout)
                {
                    yield return(null);
                }

                isTimeout = !mWWW.isDone;
            }

            IsDone = true;

            // If the routine object is null the system has reset, probably because of a log out
            // Ignore whatever result should have occured then
            if (routineObject != null)
            {
                if (isImageFail)
                {
                    HandleImageFail();
                }
                else if (isTimeout)
                {
                    HandleTimeout();
                }
                else
                {
                    HandleResult();
                }
            }

            if (isTimeout)
            {
                PoorConnectionIndicator.ReportTimeout();
            }
            else
            {
                PoorConnectionIndicator.ReportSuccess();
            }

            // If this was a failiure and should be retried, add it to the queue
            if (mShouldRetry && (isImageFail || isTimeout || !IsSuccess))
            {
                RetryQueue.Add(this);
            }
        }