コード例 #1
0
ファイル: DropBoxUploader.cs プロジェクト: crasycode/nfx
        public static JSONDataMap ChunkUpload(DropBoxRequest request, int numberOfAttempts, CancellationToken token)
        {
            using (HttpClient httpClient = DropBoxHttpFactory.Create(request))
            {
                byte[] buffer = new byte[DropBoxHttpRequestSettings.DiskDataBlockSizek];
                JSONDataMap chunkUploadResult = null;

                using (FileStream fileStream = new FileStream(request.ContentSourcePath, FileMode.Open, FileAccess.Read))
                {
                    int numberOfBytes;
                    while ((numberOfBytes = fileStream.Read(buffer, 0, DropBoxHttpRequestSettings.DiskDataBlockSizek)) > 0)
                    {
                        request.Content = new MemoryStream();
                        request.Content.Write(buffer, 0, numberOfBytes);

                        if (chunkUploadResult != null)
                        {
                            request.ChageParameter("offset", chunkUploadResult["offset"].ToString());
                            request.ChageParameter("upload_id", chunkUploadResult["upload_id"].ToString());
                        }

                        chunkUploadResult = SendFile(httpClient, request, numberOfAttempts, token);
                    }
                    return chunkUploadResult;
                }
            }
        }
コード例 #2
0
ファイル: DropBoxHttpExecuter.cs プロジェクト: crasycode/nfx
 public static JSONDataMap ExecuteAsync(DropBoxRequest request, int numberOfAttempts, CancellationToken token)
 {
     using (HttpClient httpClient = DropBoxHttpFactory.Create(request))
     {
         return httpClient.RetryExecute(request, numberOfAttempts, token, ExecuteAction);
     }
 }
コード例 #3
0
ファイル: DropBoxUploader.cs プロジェクト: crasycode/nfx
 public static JSONDataMap Upload(DropBoxRequest request, int numberOfAttempts, CancellationToken token)
 {
     numberOfAttempts = numberOfAttempts <= 0 ? DropBoxHttpRequestSettings.DefaultNumberOfAttempts : numberOfAttempts;
     using (HttpClient httpClient = DropBoxHttpFactory.Create(request))
     {
         return httpClient.RetryExecute(request, numberOfAttempts, token, ExecuteUploadAction);
     }
 }
コード例 #4
0
ファイル: DropBoxHttpFactory.cs プロジェクト: crasycode/nfx
 public static HttpClient Create(DropBoxRequest request)
 {
     HttpClient client = new HttpClient();
     client.DefaultRequestHeaders.Clear();
     client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", request.AuthorizationToken);
     client.Timeout = new TimeSpan(0, 0, 0, request.RequestTimeout);
     return client;
 }
コード例 #5
0
ファイル: DropBoxLoader.cs プロジェクト: crasycode/nfx
 public static MemoryStream Load(DropBoxRequest request, int numberOfAttempts, CancellationToken token)
 {
     using (HttpClient httpClient = DropBoxHttpFactory.Create(request))
     {
         HttpResponseMessage message = httpClient.RetryExecute(request, numberOfAttempts, token, ExecuteAction);
         return message.Content.ReadToMemory();
     }
 }
コード例 #6
0
        public static NFXException Throw(HttpStatusCode code, Exception originalException, DropBoxRequest request)
        {
            StringBuilder additionallyMessage = new StringBuilder();
            additionallyMessage.Append(GetDescriptionByCode(code));
            additionallyMessage.AppendFormat(" Url: {0}; MethodName: {1}."
                                             , request.CreateHttpRequestMessage().RequestUri
                                             , request.MethodName);
            if (originalException is HttpRequestException)
                originalException = new HttpRequestException(originalException.Message, originalException);

            return new NFXException(additionallyMessage.ToString(), originalException);
        }
コード例 #7
0
        public DropBoxRequest CreateDeleteRequest(string root, string path, string locale = "")
        {
            DropBoxRequest request = new DropBoxRequest
            {
                RequestUrl = DropBoxUrls.DeleteUrl,
                MethodName = new HttpMethod("POST"),
                RequestTimeout = _authenticationOptions.Timeout,
                AuthorizationToken = _authenticationOptions.Token
            };
            request.AddParameter("root", root);
            request.AddParameter("path", path);
            request.AddParameter("locale", locale);

            return request;
        }
コード例 #8
0
        public static JSONDataMap Execute(DropBoxRequest request, int numberOfAttempts, CancellationToken token = new CancellationToken())
        {
            try
            {
                return DropBoxHttpExecuter.ExecuteAsync(request, numberOfAttempts, token);
            }
            catch (Exception ex)
            {
                DropBoxWebExecuteException executeException = ex as DropBoxWebExecuteException;
                if (executeException != null)
                    throw DropBoxExceptionGenerator.Throw(executeException.Code, ex, request);

                throw DropBoxExceptionGenerator.Throw(ex);
            }
        }
コード例 #9
0
        public static JSONDataMap ExecuteUpload(DropBoxRequest request, int numberOfAttempts, CancellationToken token = new CancellationToken())
        {
            try
            {
                return DropBoxHttpUploader.Upload(request, numberOfAttempts, token);
            }
            catch (Exception ex)
            {
                DropBoxWebUploadException uploadError = ex as DropBoxWebUploadException;
                if (uploadError != null)
                    throw DropBoxExceptionGenerator.Throw(uploadError.Code, ex, request);

                throw DropBoxExceptionGenerator.Throw(ex);
            }
        }
コード例 #10
0
        public DropBoxRequest CreateCopyRequest(string root, string from, string to, string locale = "", string fromRef = "")
        {
            DropBoxRequest request = new DropBoxRequest
            {
                RequestUrl = DropBoxUrls.CopyUrl,
                MethodName = new HttpMethod("POST"),
                AuthorizationToken = _authenticationOptions.Token
            };
            request.AddParameter("root", root);
            request.AddParameter("from_path", from);
            request.AddParameter("to_path", to);
            request.AddParameter("locale", locale);
            request.AddParameter("from_copy_ref", fromRef);

            return request;
        }
コード例 #11
0
        public static DropBoxFile ExecuteDownload(DropBoxRequest request, int numberOfAttempts, CancellationToken token = new CancellationToken())
        {
            try
            {
                MemoryStream content = DropBoxHttpLoader.Load(request, numberOfAttempts, token);
                DropBoxFile dropBoxFile = new DropBoxFile(null, content);
                return dropBoxFile;
            }
            catch (Exception ex)
            {
                DropBoxWebLoadException loadError = ex as DropBoxWebLoadException;
                if (loadError != null)
                    throw DropBoxExceptionGenerator.Throw(loadError.Code, ex, request);

                throw DropBoxExceptionGenerator.Throw(ex);
            }
        }
コード例 #12
0
        public DropBoxRequest CreateMetadataRequest(string path, string fileLimit = "", string hash = "",
            string list = "", string includeDeleted = "", string rev = "", string locale = "",
            string includeMediaInfo = "", string includeMembership = "")
        {
            DropBoxRequest request = new DropBoxRequest
                {
                    RequestUrl = DropBoxUrls.GetMetadataUrl.AsFormat(path),
                    MethodName = new HttpMethod("GET"),
                    RequestTimeout = _authenticationOptions.Timeout,
                    AuthorizationToken = _authenticationOptions.Token
                };
            request.AddParameter("file_limit", fileLimit);
            request.AddParameter("hash", hash);
            request.AddParameter("list", list);
            request.AddParameter("include_deleted", includeDeleted);
            request.AddParameter("rev", rev);
            request.AddParameter("locale", locale);
            request.AddParameter("include_media_info", includeMediaInfo);
            request.AddParameter("include_membership", includeMembership);

            return request;
        }
コード例 #13
0
ファイル: DropBoxHttpExtension.cs プロジェクト: crasycode/nfx
        public static HttpResponseMessage RetryExecute(this HttpClient client, DropBoxRequest request
                                       , int numberOfAttempts, CancellationToken token
                                      , Func<HttpClient, DropBoxRequest, int, CancellationToken, HttpResponseMessage> action)
        {
            numberOfAttempts = numberOfAttempts <= 0 ? DropBoxHttpRequestSettings.DefaultNumberOfAttempts : numberOfAttempts;
            do
            {
                try
                {
                    return action(client, request, numberOfAttempts, token);
                }
                catch
                {
                    --numberOfAttempts;
                    if (numberOfAttempts == 0)
                        throw;
                }
                Thread.Sleep(DropBoxHttpRequestSettings.ThreadWaiteOnNextAttemptTime);

            } while (numberOfAttempts > 0);

            return null;
        }
コード例 #14
0
        public DropBoxRequest CreateCommitChunkUploadRequest(string path, string uploadId, string locale = "", string overwrite = "",
                                                             string parentRev = "", string autorename = "")
        {
            DropBoxRequest request = new DropBoxRequest
            {
                RequestUrl = DropBoxUrls.ChunkCommitUrl.AsFormat(path),
                MethodName = new HttpMethod("POST"),
                AuthorizationToken = _authenticationOptions.Token
            };
            request.AddParameter("upload_id", uploadId);
            request.AddParameter("locale", locale);
            request.AddParameter("overwrite", overwrite);
            request.AddParameter("parent_rev", parentRev);
            request.AddParameter("autorename", autorename);

            return request;
        }
コード例 #15
0
ファイル: DropBoxUploader.cs プロジェクト: crasycode/nfx
 private static JSONDataMap SendFile(HttpClient httpClient, DropBoxRequest request
                                     , int numberOfAttempts, CancellationToken token)
 {
     return httpClient.RetryExecute(request, numberOfAttempts, token, ExecuteUploadAction);
 }
コード例 #16
0
        public DropBoxRequest CreateDownloadRequest(string path, string rev = "")
        {
            DropBoxRequest request = new DropBoxRequest
            {
                RequestUrl = DropBoxUrls.GetUrl.AsFormat(path),
                MethodName = new HttpMethod("GET"),
                RequestTimeout = _authenticationOptions.Timeout,
                AuthorizationToken = _authenticationOptions.Token
            };
            request.AddParameter("rev", rev);

            return request;
        }
コード例 #17
0
        public DropBoxRequest CreateUploadRequest(Stream content, string path, string locale = "", string overwrite = ""
                                      , string parentRev = "", string autorename = "")
        {
            DropBoxRequest request = new DropBoxRequest
            {
                RequestUrl = DropBoxUrls.PutUrl.AsFormat(path),
                MethodName = new HttpMethod("PUT"),
                AuthorizationToken = _authenticationOptions.Token,
                RequestTimeout = _authenticationOptions.Timeout,
                Content = content,
            };
            request.AddParameter("locale", locale);
            request.AddParameter("overwrite", overwrite);
            request.AddParameter("parent_rev", parentRev);
            request.AddParameter("autorename", autorename);

            return request;
        }
コード例 #18
0
        public DropBoxRequest CreateChunkUploadRequest(Stream content, string uploadId = "", string offset = "")
        {
            DropBoxRequest request = new DropBoxRequest
            {
                RequestUrl = DropBoxUrls.ChunkPutUrl,
                MethodName = new HttpMethod("PUT"),
                AuthorizationToken = _authenticationOptions.Token,
                Content = content
            };

            request.AddParameter("upload_id", uploadId);
            request.AddParameter("offset", offset);

            return request;
        }