From 0fbd42a73f4465c2bc6722306c2456217e14cabd Mon Sep 17 00:00:00 2001 From: Claudiu Ionescu Date: Mon, 24 Feb 2020 11:08:19 +0100 Subject: [PATCH] Avoiding issues related to StreamContent in HttpClient --- .../Dropbox.Api/DropboxRequestHandler.cs | 65 +++++++------------ .../Generated/Files/FilesUserRoutes.cs | 48 +++++++------- .../Generated/Paper/PaperUserRoutes.cs | 16 ++--- .../Dropbox.Api/Stone/ITransport.cs | 2 +- 4 files changed, 55 insertions(+), 76 deletions(-) diff --git a/dropbox-sdk-dotnet/Dropbox.Api/DropboxRequestHandler.cs b/dropbox-sdk-dotnet/Dropbox.Api/DropboxRequestHandler.cs index ee2b32664b..77dbc245c7 100644 --- a/dropbox-sdk-dotnet/Dropbox.Api/DropboxRequestHandler.cs +++ b/dropbox-sdk-dotnet/Dropbox.Api/DropboxRequestHandler.cs @@ -200,7 +200,7 @@ async Task ITransport.SendRpcRequestAsync async Task ITransport.SendUploadRequestAsync( TRequest request, - Stream body, + IEnumerable body, string host, string route, string auth, @@ -278,7 +278,7 @@ private async Task RequestJsonStringWithRetry( string auth, RouteStyle routeStyle, string requestArg, - Stream body = null) + IEnumerable body = null) { var attempt = 0; var maxRetries = this.options.MaxClientRetries; @@ -290,57 +290,36 @@ private async Task RequestJsonStringWithRetry( { throw new ArgumentNullException("body"); } - - // to support retry logic, the body stream must be seekable - // if it isn't we won't retry - if (!body.CanSeek) - { - maxRetries = 0; - } } - try + while (true) { - while (true) + try { - try - { - return await this.RequestJsonString(host, routeName, auth, routeStyle, requestArg, body) - .ConfigureAwait(false); - } - catch (RateLimitException) + return await this.RequestJsonString(host, routeName, auth, routeStyle, requestArg, body) + .ConfigureAwait(false); + } + catch (RateLimitException) + { + throw; + } + catch (RetryException) + { + // dropbox maps 503 - ServiceUnavailable to be a rate limiting error. + // do not count a rate limiting error as an attempt + if (++attempt > maxRetries) { throw; } - catch (RetryException) - { - // dropbox maps 503 - ServiceUnavailable to be a rate limiting error. - // do not count a rate limiting error as an attempt - if (++attempt > maxRetries) - { - throw; - } - } + } - // use exponential backoff - var backoff = TimeSpan.FromSeconds(Math.Pow(2, attempt) * r.NextDouble()); + // use exponential backoff + var backoff = TimeSpan.FromSeconds(Math.Pow(2, attempt) * r.NextDouble()); #if PORTABLE40 await TaskEx.Delay(backoff); #else - await Task.Delay(backoff).ConfigureAwait(false); + await Task.Delay(backoff).ConfigureAwait(false); #endif - if (body != null) - { - body.Position = 0; - } - } - } - finally - { - if (body != null) - { - body.Dispose(); - } } } @@ -386,7 +365,7 @@ private async Task RequestJsonString( string auth, RouteStyle routeStyle, string requestArg, - Stream body = null) + IEnumerable body = null) { var hostname = this.options.HostMap[host]; var uri = this.GetRouteUri(hostname, routeName); @@ -451,7 +430,7 @@ private async Task RequestJsonString( throw new ArgumentNullException("body"); } - request.Content = new CustomStreamContent(body); + request.Content = new ByteArrayContent(body.ToArray()); request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); break; default: diff --git a/dropbox-sdk-dotnet/Dropbox.Api/Generated/Files/FilesUserRoutes.cs b/dropbox-sdk-dotnet/Dropbox.Api/Generated/Files/FilesUserRoutes.cs index b01e9e2276..906b95e5c5 100644 --- a/dropbox-sdk-dotnet/Dropbox.Api/Generated/Files/FilesUserRoutes.cs +++ b/dropbox-sdk-dotnet/Dropbox.Api/Generated/Files/FilesUserRoutes.cs @@ -190,7 +190,7 @@ public Metadata EndAlphaGetMetadata(sys.IAsyncResult asyncResult) /// processing the request; This will contain a . [sys.Obsolete("This function is deprecated, please use AlphaUploadAsync instead.")] - public t.Task AlphaUploadAsync(CommitInfoWithProperties commitInfoWithProperties, io.Stream body) + public t.Task AlphaUploadAsync(CommitInfoWithProperties commitInfoWithProperties, col.IEnumerable body) { return this.Transport.SendUploadRequestAsync(commitInfoWithProperties, body, "content", "/files/alpha/upload", "user", global::Dropbox.Api.Files.CommitInfoWithProperties.Encoder, global::Dropbox.Api.Files.FileMetadata.Decoder, global::Dropbox.Api.Files.UploadErrorWithProperties.Decoder); } @@ -206,7 +206,7 @@ public t.Task AlphaUploadAsync(CommitInfoWithProperties commitInfo /// send requests. /// An object that represents the asynchronous send request. [sys.Obsolete("This function is deprecated, please use BeginAlphaUpload instead.")] - public sys.IAsyncResult BeginAlphaUpload(CommitInfoWithProperties commitInfoWithProperties, io.Stream body, sys.AsyncCallback callback, object state = null) + public sys.IAsyncResult BeginAlphaUpload(CommitInfoWithProperties commitInfoWithProperties, col.IEnumerable body, sys.AsyncCallback callback, object state = null) { var task = this.AlphaUploadAsync(commitInfoWithProperties, body); @@ -255,7 +255,7 @@ public t.Task AlphaUploadAsync(string path, bool mute = false, col.IEnumerable propertyGroups = null, bool strictConflict = false, - io.Stream body = null) + col.IEnumerable body = null) { var commitInfoWithProperties = new CommitInfoWithProperties(path, mode, @@ -305,7 +305,7 @@ public sys.IAsyncResult BeginAlphaUpload(string path, bool mute = false, col.IEnumerable propertyGroups = null, bool strictConflict = false, - io.Stream body = null, + col.IEnumerable body = null, sys.AsyncCallback callback = null, object callbackState = null) { @@ -5418,7 +5418,7 @@ public SearchResult EndSearch(sys.IAsyncResult asyncResult) /// parameter contains the response from the server. /// Thrown if there is an error /// processing the request; This will contain a . - public t.Task UploadAsync(CommitInfo commitInfo, io.Stream body) + public t.Task UploadAsync(CommitInfo commitInfo, col.IEnumerable body) { return this.Transport.SendUploadRequestAsync(commitInfo, body, "content", "/files/upload", "user", global::Dropbox.Api.Files.CommitInfo.Encoder, global::Dropbox.Api.Files.FileMetadata.Decoder, global::Dropbox.Api.Files.UploadError.Decoder); } @@ -5433,7 +5433,7 @@ public t.Task UploadAsync(CommitInfo commitInfo, io.Stream body) /// A user provided object that distinguished this send from other /// send requests. /// An object that represents the asynchronous send request. - public sys.IAsyncResult BeginUpload(CommitInfo commitInfo, io.Stream body, sys.AsyncCallback callback, object state = null) + public sys.IAsyncResult BeginUpload(CommitInfo commitInfo, col.IEnumerable body, sys.AsyncCallback callback, object state = null) { var task = this.UploadAsync(commitInfo, body); @@ -5483,7 +5483,7 @@ public t.Task UploadAsync(string path, bool mute = false, col.IEnumerable propertyGroups = null, bool strictConflict = false, - io.Stream body = null) + col.IEnumerable body = null) { var commitInfo = new CommitInfo(path, mode, @@ -5532,7 +5532,7 @@ public sys.IAsyncResult BeginUpload(string path, bool mute = false, col.IEnumerable propertyGroups = null, bool strictConflict = false, - io.Stream body = null, + col.IEnumerable body = null, sys.AsyncCallback callback = null, object callbackState = null) { @@ -5584,7 +5584,7 @@ public FileMetadata EndUpload(sys.IAsyncResult asyncResult) /// Thrown if there is an error /// processing the request; This will contain a . - public t.Task UploadSessionAppendV2Async(UploadSessionAppendArg uploadSessionAppendArg, io.Stream body) + public t.Task UploadSessionAppendV2Async(UploadSessionAppendArg uploadSessionAppendArg, col.IEnumerable body) { return this.Transport.SendUploadRequestAsync(uploadSessionAppendArg, body, "content", "/files/upload_session/append_v2", "user", global::Dropbox.Api.Files.UploadSessionAppendArg.Encoder, enc.EmptyDecoder.Instance, global::Dropbox.Api.Files.UploadSessionLookupError.Decoder); } @@ -5599,7 +5599,7 @@ public t.Task UploadSessionAppendV2Async(UploadSessionAppendArg uploadSessionApp /// A user provided object that distinguished this send from other /// send requests. /// An object that represents the asynchronous send request. - public sys.IAsyncResult BeginUploadSessionAppendV2(UploadSessionAppendArg uploadSessionAppendArg, io.Stream body, sys.AsyncCallback callback, object state = null) + public sys.IAsyncResult BeginUploadSessionAppendV2(UploadSessionAppendArg uploadSessionAppendArg, col.IEnumerable body, sys.AsyncCallback callback, object state = null) { var task = this.UploadSessionAppendV2Async(uploadSessionAppendArg, body); @@ -5629,7 +5629,7 @@ public sys.IAsyncResult BeginUploadSessionAppendV2(UploadSessionAppendArg upload /// cref="UploadSessionLookupError"/>. public t.Task UploadSessionAppendV2Async(UploadSessionCursor cursor, bool close = false, - io.Stream body = null) + col.IEnumerable body = null) { var uploadSessionAppendArg = new UploadSessionAppendArg(cursor, close); @@ -5653,7 +5653,7 @@ public t.Task UploadSessionAppendV2Async(UploadSessionCursor cursor, /// An object that represents the asynchronous send request. public sys.IAsyncResult BeginUploadSessionAppendV2(UploadSessionCursor cursor, bool close = false, - io.Stream body = null, + col.IEnumerable body = null, sys.AsyncCallback callback = null, object callbackState = null) { @@ -5698,7 +5698,7 @@ public void EndUploadSessionAppendV2(sys.IAsyncResult asyncResult) /// processing the request; This will contain a . [sys.Obsolete("This function is deprecated, please use UploadSessionAppendAsync instead.")] - public t.Task UploadSessionAppendAsync(UploadSessionCursor uploadSessionCursor, io.Stream body) + public t.Task UploadSessionAppendAsync(UploadSessionCursor uploadSessionCursor, col.IEnumerable body) { return this.Transport.SendUploadRequestAsync(uploadSessionCursor, body, "content", "/files/upload_session/append", "user", global::Dropbox.Api.Files.UploadSessionCursor.Encoder, enc.EmptyDecoder.Instance, global::Dropbox.Api.Files.UploadSessionLookupError.Decoder); } @@ -5714,7 +5714,7 @@ public t.Task UploadSessionAppendAsync(UploadSessionCursor uploadSessionCursor, /// send requests. /// An object that represents the asynchronous send request. [sys.Obsolete("This function is deprecated, please use BeginUploadSessionAppend instead.")] - public sys.IAsyncResult BeginUploadSessionAppend(UploadSessionCursor uploadSessionCursor, io.Stream body, sys.AsyncCallback callback, object state = null) + public sys.IAsyncResult BeginUploadSessionAppend(UploadSessionCursor uploadSessionCursor, col.IEnumerable body, sys.AsyncCallback callback, object state = null) { var task = this.UploadSessionAppendAsync(uploadSessionCursor, body); @@ -5745,7 +5745,7 @@ public sys.IAsyncResult BeginUploadSessionAppend(UploadSessionCursor uploadSessi [sys.Obsolete("This function is deprecated, please use UploadSessionAppendAsync instead.")] public t.Task UploadSessionAppendAsync(string sessionId, ulong offset, - io.Stream body) + col.IEnumerable body) { var uploadSessionCursor = new UploadSessionCursor(sessionId, offset); @@ -5771,7 +5771,7 @@ public t.Task UploadSessionAppendAsync(string sessionId, [sys.Obsolete("This function is deprecated, please use BeginUploadSessionAppend instead.")] public sys.IAsyncResult BeginUploadSessionAppend(string sessionId, ulong offset, - io.Stream body, + col.IEnumerable body, sys.AsyncCallback callback, object callbackState = null) { @@ -5818,7 +5818,7 @@ public void EndUploadSessionAppend(sys.IAsyncResult asyncResult) /// Thrown if there is an error /// processing the request; This will contain a . - public t.Task UploadSessionFinishAsync(UploadSessionFinishArg uploadSessionFinishArg, io.Stream body) + public t.Task UploadSessionFinishAsync(UploadSessionFinishArg uploadSessionFinishArg, col.IEnumerable body) { return this.Transport.SendUploadRequestAsync(uploadSessionFinishArg, body, "content", "/files/upload_session/finish", "user", global::Dropbox.Api.Files.UploadSessionFinishArg.Encoder, global::Dropbox.Api.Files.FileMetadata.Decoder, global::Dropbox.Api.Files.UploadSessionFinishError.Decoder); } @@ -5833,7 +5833,7 @@ public t.Task UploadSessionFinishAsync(UploadSessionFinishArg uplo /// A user provided object that distinguished this send from other /// send requests. /// An object that represents the asynchronous send request. - public sys.IAsyncResult BeginUploadSessionFinish(UploadSessionFinishArg uploadSessionFinishArg, io.Stream body, sys.AsyncCallback callback, object state = null) + public sys.IAsyncResult BeginUploadSessionFinish(UploadSessionFinishArg uploadSessionFinishArg, col.IEnumerable body, sys.AsyncCallback callback, object state = null) { var task = this.UploadSessionFinishAsync(uploadSessionFinishArg, body); @@ -5862,7 +5862,7 @@ public sys.IAsyncResult BeginUploadSessionFinish(UploadSessionFinishArg uploadSe /// cref="UploadSessionFinishError"/>. public t.Task UploadSessionFinishAsync(UploadSessionCursor cursor, CommitInfo commit, - io.Stream body) + col.IEnumerable body) { var uploadSessionFinishArg = new UploadSessionFinishArg(cursor, commit); @@ -5884,7 +5884,7 @@ public t.Task UploadSessionFinishAsync(UploadSessionCursor cursor, /// An object that represents the asynchronous send request. public sys.IAsyncResult BeginUploadSessionFinish(UploadSessionCursor cursor, CommitInfo commit, - io.Stream body, + col.IEnumerable body, sys.AsyncCallback callback, object callbackState = null) { @@ -6158,7 +6158,7 @@ public UploadSessionFinishBatchJobStatus EndUploadSessionFinishBatchCheck(sys.IA /// The content to upload. /// The task that represents the asynchronous send operation. The TResult /// parameter contains the response from the server. - public t.Task UploadSessionStartAsync(UploadSessionStartArg uploadSessionStartArg, io.Stream body) + public t.Task UploadSessionStartAsync(UploadSessionStartArg uploadSessionStartArg, col.IEnumerable body) { return this.Transport.SendUploadRequestAsync(uploadSessionStartArg, body, "content", "/files/upload_session/start", "user", global::Dropbox.Api.Files.UploadSessionStartArg.Encoder, global::Dropbox.Api.Files.UploadSessionStartResult.Decoder, enc.EmptyDecoder.Instance); } @@ -6173,7 +6173,7 @@ public t.Task UploadSessionStartAsync(UploadSessionSta /// A user provided object that distinguished this send from other /// send requests. /// An object that represents the asynchronous send request. - public sys.IAsyncResult BeginUploadSessionStart(UploadSessionStartArg uploadSessionStartArg, io.Stream body, sys.AsyncCallback callback, object state = null) + public sys.IAsyncResult BeginUploadSessionStart(UploadSessionStartArg uploadSessionStartArg, col.IEnumerable body, sys.AsyncCallback callback, object state = null) { var task = this.UploadSessionStartAsync(uploadSessionStartArg, body); @@ -6210,7 +6210,7 @@ public sys.IAsyncResult BeginUploadSessionStart(UploadSessionStartArg uploadSess /// The task that represents the asynchronous send operation. The TResult /// parameter contains the response from the server. public t.Task UploadSessionStartAsync(bool close = false, - io.Stream body = null) + col.IEnumerable body = null) { var uploadSessionStartArg = new UploadSessionStartArg(close); @@ -6231,7 +6231,7 @@ public t.Task UploadSessionStartAsync(bool close = fal /// from other send requests. /// An object that represents the asynchronous send request. public sys.IAsyncResult BeginUploadSessionStart(bool close = false, - io.Stream body = null, + col.IEnumerable body = null, sys.AsyncCallback callback = null, object callbackState = null) { diff --git a/dropbox-sdk-dotnet/Dropbox.Api/Generated/Paper/PaperUserRoutes.cs b/dropbox-sdk-dotnet/Dropbox.Api/Generated/Paper/PaperUserRoutes.cs index 596842a5a2..71c77e291a 100644 --- a/dropbox-sdk-dotnet/Dropbox.Api/Generated/Paper/PaperUserRoutes.cs +++ b/dropbox-sdk-dotnet/Dropbox.Api/Generated/Paper/PaperUserRoutes.cs @@ -124,7 +124,7 @@ public void EndDocsArchive(sys.IAsyncResult asyncResult) /// Thrown if there is an error /// processing the request; This will contain a . - public t.Task DocsCreateAsync(PaperDocCreateArgs paperDocCreateArgs, io.Stream body) + public t.Task DocsCreateAsync(PaperDocCreateArgs paperDocCreateArgs, col.IEnumerable body) { return this.Transport.SendUploadRequestAsync(paperDocCreateArgs, body, "api", "/paper/docs/create", "user", global::Dropbox.Api.Paper.PaperDocCreateArgs.Encoder, global::Dropbox.Api.Paper.PaperDocCreateUpdateResult.Decoder, global::Dropbox.Api.Paper.PaperDocCreateError.Decoder); } @@ -139,7 +139,7 @@ public t.Task DocsCreateAsync(PaperDocCreateArgs pap /// A user provided object that distinguished this send from other /// send requests. /// An object that represents the asynchronous send request. - public sys.IAsyncResult BeginDocsCreate(PaperDocCreateArgs paperDocCreateArgs, io.Stream body, sys.AsyncCallback callback, object state = null) + public sys.IAsyncResult BeginDocsCreate(PaperDocCreateArgs paperDocCreateArgs, col.IEnumerable body, sys.AsyncCallback callback, object state = null) { var task = this.DocsCreateAsync(paperDocCreateArgs, body); @@ -161,7 +161,7 @@ public sys.IAsyncResult BeginDocsCreate(PaperDocCreateArgs paperDocCreateArgs, i /// cref="PaperDocCreateError"/>. public t.Task DocsCreateAsync(ImportFormat importFormat, string parentFolderId = null, - io.Stream body = null) + col.IEnumerable body = null) { var paperDocCreateArgs = new PaperDocCreateArgs(importFormat, parentFolderId); @@ -184,7 +184,7 @@ public t.Task DocsCreateAsync(ImportFormat importFor /// An object that represents the asynchronous send request. public sys.IAsyncResult BeginDocsCreate(ImportFormat importFormat, string parentFolderId = null, - io.Stream body = null, + col.IEnumerable body = null, sys.AsyncCallback callback = null, object callbackState = null) { @@ -1091,7 +1091,7 @@ public void EndDocsSharingPolicySet(sys.IAsyncResult asyncResult) /// Thrown if there is an error /// processing the request; This will contain a . - public t.Task DocsUpdateAsync(PaperDocUpdateArgs paperDocUpdateArgs, io.Stream body) + public t.Task DocsUpdateAsync(PaperDocUpdateArgs paperDocUpdateArgs, col.IEnumerable body) { return this.Transport.SendUploadRequestAsync(paperDocUpdateArgs, body, "api", "/paper/docs/update", "user", global::Dropbox.Api.Paper.PaperDocUpdateArgs.Encoder, global::Dropbox.Api.Paper.PaperDocCreateUpdateResult.Decoder, global::Dropbox.Api.Paper.PaperDocUpdateError.Decoder); } @@ -1106,7 +1106,7 @@ public t.Task DocsUpdateAsync(PaperDocUpdateArgs pap /// A user provided object that distinguished this send from other /// send requests. /// An object that represents the asynchronous send request. - public sys.IAsyncResult BeginDocsUpdate(PaperDocUpdateArgs paperDocUpdateArgs, io.Stream body, sys.AsyncCallback callback, object state = null) + public sys.IAsyncResult BeginDocsUpdate(PaperDocUpdateArgs paperDocUpdateArgs, col.IEnumerable body, sys.AsyncCallback callback, object state = null) { var task = this.DocsUpdateAsync(paperDocUpdateArgs, body); @@ -1132,7 +1132,7 @@ public t.Task DocsUpdateAsync(string docId, PaperDocUpdatePolicy docUpdatePolicy, long revision, ImportFormat importFormat, - io.Stream body) + col.IEnumerable body) { var paperDocUpdateArgs = new PaperDocUpdateArgs(docId, docUpdatePolicy, @@ -1161,7 +1161,7 @@ public sys.IAsyncResult BeginDocsUpdate(string docId, PaperDocUpdatePolicy docUpdatePolicy, long revision, ImportFormat importFormat, - io.Stream body, + col.IEnumerable body, sys.AsyncCallback callback, object callbackState = null) { diff --git a/dropbox-sdk-dotnet/Dropbox.Api/Stone/ITransport.cs b/dropbox-sdk-dotnet/Dropbox.Api/Stone/ITransport.cs index fd02f50981..9a63d53db6 100644 --- a/dropbox-sdk-dotnet/Dropbox.Api/Stone/ITransport.cs +++ b/dropbox-sdk-dotnet/Dropbox.Api/Stone/ITransport.cs @@ -90,7 +90,7 @@ Task SendRpcRequestAsync( /// An asynchronous task for the response. Task SendUploadRequestAsync( TRequest request, - Stream body, + IEnumerable body, string host, string route, string auth,