diff --git a/src/Docker.DotNet/DockerClient.cs b/src/Docker.DotNet/DockerClient.cs index 8042eeb1..3bf62fdf 100644 --- a/src/Docker.DotNet/DockerClient.cs +++ b/src/Docker.DotNet/DockerClient.cs @@ -72,18 +72,18 @@ internal Task MakeRequestAsync( IEnumerable errorHandlers, HttpMethod method, string path, - CancellationToken token) + CancellationToken cancellationToken) { - return MakeRequestAsync(errorHandlers, method, path, null, null, token); + return MakeRequestAsync(errorHandlers, method, path, null, null, cancellationToken); } internal Task MakeRequestAsync( IEnumerable errorHandlers, HttpMethod method, string path, - CancellationToken token) + CancellationToken cancellationToken) { - return MakeRequestAsync(errorHandlers, method, path, null, null, token); + return MakeRequestAsync(errorHandlers, method, path, null, null, cancellationToken); } internal Task MakeRequestAsync( @@ -91,9 +91,9 @@ internal Task MakeRequestAsync( HttpMethod method, string path, IQueryString? queryString, - CancellationToken token) + CancellationToken cancellationToken) { - return MakeRequestAsync(errorHandlers, method, path, queryString, null, token); + return MakeRequestAsync(errorHandlers, method, path, queryString, null, cancellationToken); } internal Task MakeRequestAsync( @@ -101,9 +101,9 @@ internal Task MakeRequestAsync( HttpMethod method, string path, IQueryString? queryString, - CancellationToken token) + CancellationToken cancellationToken) { - return MakeRequestAsync(errorHandlers, method, path, queryString, null, token); + return MakeRequestAsync(errorHandlers, method, path, queryString, null, cancellationToken); } internal Task MakeRequestAsync( @@ -112,9 +112,9 @@ internal Task MakeRequestAsync( string path, IQueryString? queryString, IRequestContent? body, - CancellationToken token) + CancellationToken cancellationToken) { - return MakeRequestAsync(errorHandlers, method, path, queryString, body, null, token); + return MakeRequestAsync(errorHandlers, method, path, queryString, body, null, cancellationToken); } internal Task MakeRequestAsync( @@ -123,9 +123,9 @@ internal Task MakeRequestAsync( string path, IQueryString? queryString, IRequestContent? body, - CancellationToken token) + CancellationToken cancellationToken) { - return MakeRequestAsync(errorHandlers, method, path, queryString, body, null, token); + return MakeRequestAsync(errorHandlers, method, path, queryString, body, null, cancellationToken); } internal Task MakeRequestAsync( @@ -135,9 +135,9 @@ internal Task MakeRequestAsync( IQueryString? queryString, IRequestContent? body, IDictionary? headers, - CancellationToken token) + CancellationToken cancellationToken) { - return MakeRequestAsync(errorHandlers, method, path, queryString, body, headers, _clientOptions.Timeout, token); + return MakeRequestAsync(errorHandlers, method, path, queryString, body, headers, _clientOptions.Timeout, cancellationToken); } internal Task MakeRequestAsync( @@ -148,9 +148,9 @@ internal Task MakeRequestAsync( IRequestContent? body, IDictionary? headers, TimeSpan timeout, - CancellationToken token) + CancellationToken cancellationToken) { - return MakeRequestAsync(errorHandlers, method, path, queryString, body, headers, timeout, token); + return MakeRequestAsync(errorHandlers, method, path, queryString, body, headers, timeout, cancellationToken); } internal async Task MakeRequestAsync( @@ -161,9 +161,9 @@ internal async Task MakeRequestAsync( IRequestContent? body, IDictionary? headers, TimeSpan timeout, - CancellationToken token) + CancellationToken cancellationToken) { - using var response = await PrivateMakeRequestAsync(timeout, HttpCompletionOption.ResponseContentRead, method, path, queryString, headers, body, token) + using var response = await PrivateMakeRequestAsync(timeout, HttpCompletionOption.ResponseContentRead, method, path, queryString, headers, body, cancellationToken) .ConfigureAwait(false); await HandleIfErrorResponseAsync(response.StatusCode, response, errorHandlers) @@ -174,7 +174,7 @@ await HandleIfErrorResponseAsync(response.StatusCode, response, errorHandlers) return default!; } - return await JsonSerializer.DeserializeAsync(response.Content, token) + return await JsonSerializer.DeserializeAsync(response.Content, cancellationToken) .ConfigureAwait(false); } @@ -182,9 +182,9 @@ internal Task MakeRequestForStreamAsync( IEnumerable errorHandlers, HttpMethod method, string path, - CancellationToken token) + CancellationToken cancellationToken) { - return MakeRequestForStreamAsync(errorHandlers, method, path, null, token); + return MakeRequestForStreamAsync(errorHandlers, method, path, null, cancellationToken); } internal Task MakeRequestForStreamAsync( @@ -192,9 +192,9 @@ internal Task MakeRequestForStreamAsync( HttpMethod method, string path, IQueryString? queryString, - CancellationToken token) + CancellationToken cancellationToken) { - return MakeRequestForStreamAsync(errorHandlers, method, path, queryString, null, token); + return MakeRequestForStreamAsync(errorHandlers, method, path, queryString, null, cancellationToken); } internal Task MakeRequestForStreamAsync( @@ -203,9 +203,9 @@ internal Task MakeRequestForStreamAsync( string path, IQueryString? queryString, IRequestContent? body, - CancellationToken token) + CancellationToken cancellationToken) { - return MakeRequestForStreamAsync(errorHandlers, method, path, queryString, body, null, token); + return MakeRequestForStreamAsync(errorHandlers, method, path, queryString, body, null, cancellationToken); } internal Task MakeRequestForStreamAsync( @@ -215,9 +215,9 @@ internal Task MakeRequestForStreamAsync( IQueryString? queryString, IRequestContent? body, IDictionary? headers, - CancellationToken token) + CancellationToken cancellationToken) { - return MakeRequestForStreamAsync(errorHandlers, method, path, queryString, body, headers, Timeout.InfiniteTimeSpan, token); + return MakeRequestForStreamAsync(errorHandlers, method, path, queryString, body, headers, Timeout.InfiniteTimeSpan, cancellationToken); } internal async Task MakeRequestForStreamAsync( @@ -228,9 +228,9 @@ internal async Task MakeRequestForStreamAsync( IRequestContent? body, IDictionary? headers, TimeSpan timeout, - CancellationToken token) + CancellationToken cancellationToken) { - var response = await PrivateMakeRequestAsync(timeout, HttpCompletionOption.ResponseHeadersRead, method, path, queryString, headers, body, token) + var response = await PrivateMakeRequestAsync(timeout, HttpCompletionOption.ResponseHeadersRead, method, path, queryString, headers, body, cancellationToken) .ConfigureAwait(false); try @@ -256,12 +256,12 @@ internal async Task MakeRequestForRawResponseAsync( IQueryString? queryString, IRequestContent? body, IDictionary? headers, - CancellationToken token) + CancellationToken cancellationToken) { - var response = await PrivateMakeRequestAsync(Timeout.InfiniteTimeSpan, HttpCompletionOption.ResponseHeadersRead, method, path, queryString, headers, body, token) + var response = await PrivateMakeRequestAsync(Timeout.InfiniteTimeSpan, HttpCompletionOption.ResponseHeadersRead, method, path, queryString, headers, body, cancellationToken) .ConfigureAwait(false); - await HandleIfErrorResponseAsync(response.StatusCode, response) + await HandleIfErrorResponseAsync(response.StatusCode, response, null) .ConfigureAwait(false); return response; @@ -413,7 +413,7 @@ private HttpRequestMessage PrepareRequest(HttpMethod method, string path, IQuery return request; } - private async Task HandleIfErrorResponseAsync(HttpStatusCode statusCode, HttpResponseMessage response, IEnumerable? handlers) + private static async Task HandleIfErrorResponseAsync(HttpStatusCode statusCode, HttpResponseMessage response, IEnumerable? handlers) { var isErrorResponse = (statusCode < HttpStatusCode.OK || statusCode >= HttpStatusCode.BadRequest) && statusCode != HttpStatusCode.SwitchingProtocols; @@ -444,28 +444,6 @@ private async Task HandleIfErrorResponseAsync(HttpStatusCode statusCode, HttpRes } } - private async Task HandleIfErrorResponseAsync(HttpStatusCode statusCode, HttpResponseMessage response) - { - var isErrorResponse = statusCode < HttpStatusCode.OK || statusCode >= HttpStatusCode.BadRequest; - - string? responseBody = null; - - if (isErrorResponse) - { - // If it is not an error response, we do not read the response body because the caller may wish to consume it. - // If it is an error response, we do because there is nothing else going to be done with it anyway, and - // we want to report the response body in the error message as it contains potentially useful info. - responseBody = await response.Content.ReadAsStringAsync() - .ConfigureAwait(false); - } - - // No custom handler was fired. Default the response for generic success/failures. - if (isErrorResponse) - { - throw new DockerApiException(statusCode, responseBody); - } - } - private struct NoContent; }