Skip to content

Commit

Permalink
feat: Replace JSON.NET with System.Text.Json (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn authored Nov 7, 2024
1 parent 0c69aca commit af0580e
Show file tree
Hide file tree
Showing 316 changed files with 1,998 additions and 2,319 deletions.
5 changes: 4 additions & 1 deletion src/Docker.DotNet/Docker.DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
<PackageId>Docker.DotNet.Enhanced</PackageId>
<Description>Docker.DotNet is a library that allows you to interact with the Docker Remote API programmatically with fully asynchronous, non-blocking and object-oriented code in your .NET applications.</Description>
<AssemblyName>Docker.DotNet</AssemblyName>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Buffers" Version="4.5.1" />
<PackageReference Include="System.IO.Pipelines" Version="8.0.0" />
<PackageReference Include="System.Net.Http.Json" Version="8.0.1" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>
</Project>
93 changes: 70 additions & 23 deletions src/Docker.DotNet/DockerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ internal DockerClient(DockerClientConfiguration configuration, Version requested
Configuration = configuration;
DefaultTimeout = configuration.DefaultTimeout;

JsonSerializer = new JsonSerializer();
Images = new ImageOperations(this);
Containers = new ContainerOperations(this);
System = new SystemOperations(this);
Expand Down Expand Up @@ -149,57 +148,100 @@ await sock.ConnectAsync(new Microsoft.Net.Http.Client.UnixDomainSocketEndPoint(p

public IExecOperations Exec { get; }

internal JsonSerializer JsonSerializer { get; }
internal static JsonSerializer JsonSerializer => JsonSerializer.Instance;

public void Dispose()
{
Configuration.Dispose();
_client.Dispose();
}

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

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

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

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

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

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

internal Task<DockerApiResponse> MakeRequestAsync(
internal Task<T> MakeRequestAsync<T>(
IEnumerable<ApiResponseErrorHandlingDelegate> errorHandlers,
HttpMethod method,
string path,
IQueryString queryString,
IRequestContent body,
IDictionary<string, string> headers,
CancellationToken token)
{
return MakeRequestAsync(errorHandlers, method, path, queryString, body, null, token);
return MakeRequestAsync<T>(errorHandlers, method, path, queryString, body, headers, DefaultTimeout, token);
}

internal Task<DockerApiResponse> MakeRequestAsync(
internal Task MakeRequestAsync(
IEnumerable<ApiResponseErrorHandlingDelegate> errorHandlers,
HttpMethod method,
string path,
IQueryString queryString,
IRequestContent body,
IDictionary<string, string> headers,
TimeSpan timeout,
CancellationToken token)
{
return MakeRequestAsync(errorHandlers, method, path, queryString, body, headers, DefaultTimeout, token);
return MakeRequestAsync<NoContent>(errorHandlers, method, path, queryString, body, headers, timeout, token);
}

internal async Task<DockerApiResponse> MakeRequestAsync(
internal async Task<T> MakeRequestAsync<T>(
IEnumerable<ApiResponseErrorHandlingDelegate> errorHandlers,
HttpMethod method,
string path,
Expand All @@ -209,19 +251,19 @@ internal async Task<DockerApiResponse> MakeRequestAsync(
TimeSpan timeout,
CancellationToken token)
{
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, token)
.ConfigureAwait(false);

using (response)
{
await HandleIfErrorResponseAsync(response.StatusCode, response, errorHandlers)
.ConfigureAwait(false);

var responseBody = await response.Content.ReadAsStringAsync()
.ConfigureAwait(false);
await HandleIfErrorResponseAsync(response.StatusCode, response, errorHandlers)
.ConfigureAwait(false);

return new DockerApiResponse(response.StatusCode, responseBody);
if (typeof(T) == typeof(NoContent))
{
return default;
}

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

internal Task<Stream> MakeRequestForStreamAsync(
Expand Down Expand Up @@ -294,9 +336,12 @@ internal async Task<HttpResponseMessage> MakeRequestForRawResponseAsync(
IDictionary<string, string> headers,
CancellationToken token)
{
var response = await PrivateMakeRequestAsync(SInfiniteTimeout, HttpCompletionOption.ResponseHeadersRead, method, path, queryString, headers, body, token).ConfigureAwait(false);
var response = await PrivateMakeRequestAsync(SInfiniteTimeout, HttpCompletionOption.ResponseHeadersRead, method, path, queryString, headers, body, token)
.ConfigureAwait(false);

await HandleIfErrorResponseAsync(response.StatusCode, response)
.ConfigureAwait(false);

return response;
}

Expand Down Expand Up @@ -347,7 +392,7 @@ internal async Task<WriteClosableStream> MakeRequestForHijackedStreamAsync(
await HandleIfErrorResponseAsync(response.StatusCode, response, errorHandlers)
.ConfigureAwait(false);

if (!(response.Content is HttpConnectionResponseContent content))
if (response.Content is not HttpConnectionResponseContent content)
{
throw new NotSupportedException("message handler does not support hijacked streams");
}
Expand Down Expand Up @@ -388,7 +433,7 @@ private async Task<HttpResponseMessage> PrivateMakeRequestAsync(
}
}

internal HttpRequestMessage PrepareRequest(HttpMethod method, string path, IQueryString queryString, IDictionary<string, string> headers, IRequestContent data)
private HttpRequestMessage PrepareRequest(HttpMethod method, string path, IQueryString queryString, IDictionary<string, string> headers, IRequestContent data)
{
if (string.IsNullOrEmpty(path))
{
Expand Down Expand Up @@ -469,6 +514,8 @@ private async Task HandleIfErrorResponseAsync(HttpStatusCode statusCode, HttpRes
throw new DockerApiException(statusCode, responseBody);
}
}

private struct NoContent;
}

internal delegate void ApiResponseErrorHandlingDelegate(HttpStatusCode statusCode, string responseBody);
Expand Down
13 changes: 5 additions & 8 deletions src/Docker.DotNet/Endpoints/ConfigsOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ internal ConfigOperations(DockerClient client)

async Task<IList<SwarmConfig>> IConfigOperations.ListConfigsAsync(CancellationToken cancellationToken)
{
var response = await _client.MakeRequestAsync(_client.NoErrorHandlers, HttpMethod.Get, "configs", cancellationToken).ConfigureAwait(false);
return _client.JsonSerializer.DeserializeObject<IList<SwarmConfig>>(response.Body);
return await _client.MakeRequestAsync<IList<SwarmConfig>>(_client.NoErrorHandlers, HttpMethod.Get, "configs", cancellationToken).ConfigureAwait(false);
}

async Task<SwarmCreateConfigResponse> IConfigOperations.CreateConfigAsync(SwarmCreateConfigParameters body, CancellationToken cancellationToken)
Expand All @@ -29,9 +28,8 @@ async Task<SwarmCreateConfigResponse> IConfigOperations.CreateConfigAsync(SwarmC
throw new ArgumentNullException(nameof(body));
}

var data = new JsonRequestContent<SwarmConfigSpec>(body.Config, _client.JsonSerializer);
var response = await _client.MakeRequestAsync(_client.NoErrorHandlers, HttpMethod.Post, "configs/create", null, data, cancellationToken).ConfigureAwait(false);
return _client.JsonSerializer.DeserializeObject<SwarmCreateConfigResponse>(response.Body);
var data = new JsonRequestContent<SwarmConfigSpec>(body.Config, DockerClient.JsonSerializer);
return await _client.MakeRequestAsync<SwarmCreateConfigResponse>(_client.NoErrorHandlers, HttpMethod.Post, "configs/create", null, data, cancellationToken).ConfigureAwait(false);
}

async Task<SwarmConfig> IConfigOperations.InspectConfigAsync(string id, CancellationToken cancellationToken)
Expand All @@ -41,8 +39,7 @@ async Task<SwarmConfig> IConfigOperations.InspectConfigAsync(string id, Cancella
throw new ArgumentNullException(nameof(id));
}

var response = await _client.MakeRequestAsync(_client.NoErrorHandlers, HttpMethod.Get, $"configs/{id}", cancellationToken).ConfigureAwait(false);
return _client.JsonSerializer.DeserializeObject<SwarmConfig>(response.Body);
return await _client.MakeRequestAsync<SwarmConfig>(_client.NoErrorHandlers, HttpMethod.Get, $"configs/{id}", cancellationToken).ConfigureAwait(false);
}

Task IConfigOperations.RemoveConfigAsync(string id, CancellationToken cancellationToken)
Expand All @@ -55,4 +52,4 @@ Task IConfigOperations.RemoveConfigAsync(string id, CancellationToken cancellati
return _client.MakeRequestAsync(_client.NoErrorHandlers, HttpMethod.Delete, $"configs/{id}", cancellationToken);
}
}
}
}
Loading

0 comments on commit af0580e

Please sign in to comment.