Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 33 additions & 55 deletions src/Docker.DotNet/DockerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,38 +72,38 @@ internal Task MakeRequestAsync(
IEnumerable<ApiResponseErrorHandlingDelegate> errorHandlers,
HttpMethod method,
string path,
CancellationToken token)
CancellationToken cancellationToken)
{
return MakeRequestAsync<NoContent>(errorHandlers, method, path, null, null, token);
return MakeRequestAsync<NoContent>(errorHandlers, method, path, null, null, cancellationToken);
}

internal Task<T> MakeRequestAsync<T>(
IEnumerable<ApiResponseErrorHandlingDelegate> errorHandlers,
HttpMethod method,
string path,
CancellationToken token)
CancellationToken cancellationToken)
{
return MakeRequestAsync<T>(errorHandlers, method, path, null, null, token);
return MakeRequestAsync<T>(errorHandlers, method, path, null, null, cancellationToken);
}

internal Task MakeRequestAsync(
IEnumerable<ApiResponseErrorHandlingDelegate> errorHandlers,
HttpMethod method,
string path,
IQueryString? queryString,
CancellationToken token)
CancellationToken cancellationToken)
{
return MakeRequestAsync<NoContent>(errorHandlers, method, path, queryString, null, token);
return MakeRequestAsync<NoContent>(errorHandlers, method, path, queryString, null, cancellationToken);
}

internal Task<T> MakeRequestAsync<T>(
IEnumerable<ApiResponseErrorHandlingDelegate> errorHandlers,
HttpMethod method,
string path,
IQueryString? queryString,
CancellationToken token)
CancellationToken cancellationToken)
{
return MakeRequestAsync<T>(errorHandlers, method, path, queryString, null, token);
return MakeRequestAsync<T>(errorHandlers, method, path, queryString, null, cancellationToken);
}

internal Task MakeRequestAsync(
Expand All @@ -112,9 +112,9 @@ internal Task MakeRequestAsync(
string path,
IQueryString? queryString,
IRequestContent? body,
CancellationToken token)
CancellationToken cancellationToken)
{
return MakeRequestAsync<NoContent>(errorHandlers, method, path, queryString, body, null, token);
return MakeRequestAsync<NoContent>(errorHandlers, method, path, queryString, body, null, cancellationToken);
}

internal Task<T> MakeRequestAsync<T>(
Expand All @@ -123,9 +123,9 @@ internal Task<T> MakeRequestAsync<T>(
string path,
IQueryString? queryString,
IRequestContent? body,
CancellationToken token)
CancellationToken cancellationToken)
{
return MakeRequestAsync<T>(errorHandlers, method, path, queryString, body, null, token);
return MakeRequestAsync<T>(errorHandlers, method, path, queryString, body, null, cancellationToken);
}

internal Task<T> MakeRequestAsync<T>(
Expand All @@ -135,9 +135,9 @@ internal Task<T> MakeRequestAsync<T>(
IQueryString? queryString,
IRequestContent? body,
IDictionary<string, string>? headers,
CancellationToken token)
CancellationToken cancellationToken)
{
return MakeRequestAsync<T>(errorHandlers, method, path, queryString, body, headers, _clientOptions.Timeout, token);
return MakeRequestAsync<T>(errorHandlers, method, path, queryString, body, headers, _clientOptions.Timeout, cancellationToken);
}

internal Task MakeRequestAsync(
Expand All @@ -148,9 +148,9 @@ internal Task MakeRequestAsync(
IRequestContent? body,
IDictionary<string, string>? headers,
TimeSpan timeout,
CancellationToken token)
CancellationToken cancellationToken)
{
return MakeRequestAsync<NoContent>(errorHandlers, method, path, queryString, body, headers, timeout, token);
return MakeRequestAsync<NoContent>(errorHandlers, method, path, queryString, body, headers, timeout, cancellationToken);
}

internal async Task<T> MakeRequestAsync<T>(
Expand All @@ -161,9 +161,9 @@ internal async Task<T> MakeRequestAsync<T>(
IRequestContent? body,
IDictionary<string, string>? 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)
Expand All @@ -174,27 +174,27 @@ await HandleIfErrorResponseAsync(response.StatusCode, response, errorHandlers)
return default!;
}

return await JsonSerializer.DeserializeAsync<T>(response.Content, token)
return await JsonSerializer.DeserializeAsync<T>(response.Content, cancellationToken)
.ConfigureAwait(false);
}

internal Task<StandardStreamResponse> MakeRequestForStreamAsync(
IEnumerable<ApiResponseErrorHandlingDelegate> 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<StandardStreamResponse> MakeRequestForStreamAsync(
IEnumerable<ApiResponseErrorHandlingDelegate> errorHandlers,
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<StandardStreamResponse> MakeRequestForStreamAsync(
Expand All @@ -203,9 +203,9 @@ internal Task<StandardStreamResponse> 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<StandardStreamResponse> MakeRequestForStreamAsync(
Expand All @@ -215,9 +215,9 @@ internal Task<StandardStreamResponse> MakeRequestForStreamAsync(
IQueryString? queryString,
IRequestContent? body,
IDictionary<string, string>? 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<StandardStreamResponse> MakeRequestForStreamAsync(
Expand All @@ -228,9 +228,9 @@ internal async Task<StandardStreamResponse> MakeRequestForStreamAsync(
IRequestContent? body,
IDictionary<string, string>? 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
Expand All @@ -256,12 +256,12 @@ internal async Task<HttpResponseMessage> MakeRequestForRawResponseAsync(
IQueryString? queryString,
IRequestContent? body,
IDictionary<string, string>? 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;
Expand Down Expand Up @@ -413,7 +413,7 @@ private HttpRequestMessage PrepareRequest(HttpMethod method, string path, IQuery
return request;
}

private async Task HandleIfErrorResponseAsync(HttpStatusCode statusCode, HttpResponseMessage response, IEnumerable<ApiResponseErrorHandlingDelegate>? handlers)
private static async Task HandleIfErrorResponseAsync(HttpStatusCode statusCode, HttpResponseMessage response, IEnumerable<ApiResponseErrorHandlingDelegate>? handlers)
{
var isErrorResponse = (statusCode < HttpStatusCode.OK || statusCode >= HttpStatusCode.BadRequest) && statusCode != HttpStatusCode.SwitchingProtocols;

Expand Down Expand Up @@ -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;
}

Expand Down
Loading