Example #1
0
 public virtual void SendRequest(Stream body, KiiHttpClientProgressCallback progressCallback, KiiHttpClientCallback callback)
 {
     if (callback == null)
     {
         throw new ArgumentNullException("callback is null");
     }
     this.body = ReadStream(body);
     this.SetHttpMethodOverride();
     this.ExecSendRequest(progressCallback, null, callback);
 }
Example #2
0
        private void ExecSendRequest(KiiHttpClientProgressCallback progressCallback, KiiHttpClientProgressPercentageCallback progressPercentageCallback, KiiHttpClientCallback callback)
        {
            if (callback == null)
            {
                throw new ArgumentNullException("callback is null");
            }
            this.SetSDKClientInfo();
            WWW www = new WWW(this.url, this.body, this.headers.GetHeadersAsDictionary());

            try
            {
                float timeout = Time.realtimeSinceStartup + Timeout;
                while (!www.isDone)
                {
                    if (timeout < Time.realtimeSinceStartup)
                    {
                        callback(null, new NetworkException(new TimeoutException("Connection timeout. (did not finish within " + Timeout + " seconds)")));
                        return;
                    }
                    Thread.Sleep(500);
                    if (progressCallback != null)
                    {
                        progressCallback((long)(this.body.Length * www.uploadProgress), this.body.Length);
                    }
                    if (progressPercentageCallback != null)
                    {
                        progressPercentageCallback(www.progress);
                    }
                }
                Exception e = this.CheckHttpError(www);
                if (e != null)
                {
                    callback(null, e);
                    return;
                }
                ApiResponse response = new ApiResponse();
                Dictionary <string, string> responseHeaders = WWWUtils.LowerCaseHeaders(www);
                this.CopyHttpHeaders(responseHeaders, response);

                response.Status      = WWWUtils.GetStatusCode(responseHeaders, www.bytes == null ? 204 : 200);
                response.ContentType = WWWUtils.GetHeader(responseHeaders, "Content-Type");
                response.ETag        = WWWUtils.GetHeader(responseHeaders, "ETag");

                if (www.bytes != null)
                {
                    response.Body = this.GetString(www.bytes);
                }
                callback(response, null);
            }
            finally
            {
                DisposeWWW(www);
            }
        }
Example #3
0
        public void SendRequest(Stream body, KiiHttpClientProgressCallback progressCallback, KiiHttpClientCallback callback)
        {
            // simulate calling progress callback
            long totalByte = body.Length;
            int  count     = numProgressQueue.Dequeue();
            long blockByte = totalByte / count;

            for (int i = 1; i < count; ++i)
            {
                progressCallback(blockByte * i, totalByte);
            }
            progressCallback(totalByte, totalByte);

            SendRequest(callback);
        }
Example #4
0
 public override void SendRequest(Stream body, KiiHttpClientProgressCallback progressCallback, KiiHttpClientCallback callback)
 {
     if (callback == null)
     {
         throw new ArgumentNullException("callback is null");
     }
     this.body = ReadStream(body);
     this.SetHttpMethodOverride();
     WWWRequestLooper.RunOnMainThread(() => {
         // TODO:If huge file will be uploaded, this way is bad performance.
         // Try to Upload an Object Body in Multiple Pieces
         // See:http://documentation.kii.com/en/guides/rest/managing-data/object-storages/uploading/
         this.ExecSendRequest(new ProgressCallbackHelper(progressCallback), callback);
     });
 }
        public void SendRequestForDownload(Stream outStream, KiiHttpClientProgressCallback progressCallback, KiiHttpClientCallback callback)
        {
            SetSDKClientInfo();
            ApiResponse response = new ApiResponse();

            if (responseQueue.Count == 0)
            {
                response.Status = 200;
                response.Body   = "{}";
                callback(response, null);
                return;
            }
            MockResponse mockResponse = responseQueue.Dequeue();

            Exception e = mockResponse.Ex;

            if (e != null)
            {
                callback(null, e);
                return;
            }
            byte[] bytes = mockResponse.BytesBody;

            // simulate calling progress callback
            long totalByte = bytes.Length;
            int  count     = numProgressQueue.Dequeue();
            long blockByte = totalByte / count;

            for (int i = 1; i < count; ++i)
            {
                progressCallback(blockByte * i, totalByte);
            }
            progressCallback(totalByte, totalByte);
            outStream.Write(bytes, 0, (int)totalByte);

            response.Status      = mockResponse.Status;
            response.ContentType = mockResponse.BodyContentType;
            response.ETag        = mockResponse.ETag;

            callback(response, null);
        }
Example #6
0
 public ProgressCallbackHelper(KiiHttpClientProgressPercentageCallback callback)
 {
     this.progressCallback           = null;
     this.progressPercentageCallback = callback;
 }
Example #7
0
 public override void SendRequestForDownload(Stream outStream, KiiHttpClientProgressCallback progressCallback, KiiHttpClientCallback callback)
 {
     System.Threading.ThreadPool.QueueUserWorkItem((object stateInfo) => {
         base.SendRequestForDownload(outStream, progressCallback, callback);
     });
 }
Example #8
0
 public virtual void SendRequestForDownload(Stream outStream, KiiHttpClientProgressCallback progressCallback, KiiHttpClientCallback callback)
 {
     this.ExecSendRequestForDownload(outStream, progressCallback, null, callback);
 }
Example #9
0
 public override void SendRequestForDownload(Stream outStream, KiiHttpClientProgressCallback progressCallback, KiiHttpClientCallback callback)
 {
     this.ExecSendRequestForDownload(outStream, new ProgressCallbackHelper(progressCallback), callback);
 }
 public virtual void SendRequestForDownload(Stream outStream, KiiHttpClientProgressCallback progressCallback, KiiHttpClientCallback callback)
 {
     SendRequestForDownload(outStream, new ProgressCallbackHelper(progressCallback), callback);
 }
 public virtual void SendRequest(Stream body, KiiHttpClientProgressCallback progressCallback, KiiHttpClientCallback callback)
 {
     SendRequest(body, new ProgressCallbackHelper(progressCallback), callback);
 }