Пример #1
0
        private void EnqueueRequest <T>(RestRequest restRequest, Action <T> onSuccess, Action <WWWErrorException> onError, string serializedRequestPath = null)
        {
            if (restRequest.RetryIfRequestFails && (serializedRequestPath == null || !File.Exists(serializedRequestPath)))
            {
                var fileName = string.Format("{0}_{1}", DateTimeUtility.GetCurrentPosixMiliseconds(),
                                             Guid.NewGuid());
                fileName = string.Format("{0}.{1}", fileName, CACHED_FILE_EXTENSION);
                serializedRequestPath = Path.Combine(_requestsCacheDirectory.ToString(), fileName);
                SerializeRequest(restRequest, serializedRequestPath);
            }

            var cachedRequest =
                new RequestWrapper(restRequest);

            cachedRequest.OnSuccess = new CallbackWrapper(
                new Action <T>(result =>
            {
                if (onSuccess != null)
                {
                    onSuccess.Invoke(result);
                }

                if (serializedRequestPath != null)
                {
                    DeleteSerializedRequest(serializedRequestPath);
                }
            }));

            cachedRequest.OnFailed = new CallbackWrapper(
                new Action <WWWErrorException>(ex =>
            {
                OnErrorDefaultCallback(ex);

                _requestsQueue.Enqueue(cachedRequest);

                if (onError != null)
                {
                    onError.Invoke(ex);
                }
            }));

            _requestsQueue.Enqueue(cachedRequest);
        }