Ejemplo n.º 1
0
 public virtual void SendRequest(Stream body, KiiHttpClientProgressPercentageCallback progressCallback, KiiHttpClientCallback callback)
 {
     if (callback == null)
     {
         throw new ArgumentNullException("callback is null");
     }
     this.body = ReadStream(body);
     this.SetHttpMethodOverride();
     this.ExecSendRequest(null, progressCallback, callback);
 }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 3
0
        public void SendRequest(Stream body, KiiHttpClientProgressPercentageCallback 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((float)((blockByte * i) / totalByte));
            }
            progressCallback((float)(totalByte / totalByte));

            SendRequest(callback);
        }
Ejemplo n.º 4
0
 public override void SendRequest(Stream body, KiiHttpClientProgressPercentageCallback 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);
     });
 }
Ejemplo n.º 5
0
        public void SendRequestForDownload(Stream outStream, KiiHttpClientProgressPercentageCallback progressCallback, KiiHttpClientCallback callback)
        {
            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((float)((blockByte * i) / totalByte));
            }
            progressCallback((float)(totalByte / totalByte));
            outStream.Write(bytes, 0, (int)totalByte);

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

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