Skip to content

Commit

Permalink
Drop AsyncUtils and its uses
Browse files Browse the repository at this point in the history
This retains only async versions of methods; it is up to the end user to
decide how to make use of them.

This is a breaking API change, so the version has been bumped to
4.0.0-pre.
  • Loading branch information
Zastai committed Jan 4, 2025
1 parent 205794c commit bdfc1c2
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 80 deletions.
31 changes: 0 additions & 31 deletions MetaBrainz.Common/AsyncUtils.cs

This file was deleted.

7 changes: 1 addition & 6 deletions MetaBrainz.Common/HttpError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ public HttpError(HttpStatusCode status, string? reason = null, Version? version
/// <summary>The HTTP message version from the error response, if available.</summary>
public Version? Version { get; private init; }

/// <summary>Creates a new HTTP error based on an response message.</summary>
/// <param name="response">The response.</param>
/// <returns>A new HTTP error containing information taken from the response message.</returns>
public static HttpError FromResponse(HttpResponseMessage response) => AsyncUtils.ResultOf(HttpError.FromResponseAsync(response));

/// <summary>Creates a new HTTP error based on an response message.</summary>
/// <summary>Creates a new HTTP error based on a response message.</summary>
/// <param name="response">The response message that triggered the error.</param>
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
/// <returns>A new HTTP error containing information taken from the response message.</returns>
Expand Down
22 changes: 2 additions & 20 deletions MetaBrainz.Common/HttpUtils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
Expand All @@ -26,7 +25,7 @@ public static class HttpUtils {
return null;
}
// There is no way to construct a copy of HTTP headers directly at the moment (see dotnet/runtime#95912).
using var dummy = new ByteArrayContent(Array.Empty<byte>());
using var dummy = new ByteArrayContent([]);
HttpUtils.Copy(headers, dummy.Headers);
return dummy.Headers;
}
Expand Down Expand Up @@ -76,13 +75,6 @@ public static ProductInfoHeaderValue CreateUserAgentHeader<T>() {
return new ProductInfoHeaderValue(an.Name ?? HttpUtils.UnknownAssemblyName, an.Version?.ToString());
}

/// <summary>Checks a response to ensure it was successful.</summary>
/// <param name="response">The response whose status should be checked.</param>
/// <returns><paramref name="response"/>.</returns>
/// <exception cref="HttpError">When the response did not have a successful status.</exception>
public static HttpResponseMessage EnsureSuccessful(this HttpResponseMessage response)
=> AsyncUtils.ResultOf(response.EnsureSuccessfulAsync());

/// <summary>Checks a response to ensure it was successful.</summary>
/// <param name="response">The response whose status should be checked.</param>
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
Expand Down Expand Up @@ -116,12 +108,6 @@ public static string GetContentEncoding(this HttpContentHeaders contentHeaders)
return characterSet?.ToLowerInvariant() ?? "utf-8";
}

/// <summary>Gets the content of an HTTP response as a string.</summary>
/// <param name="response">The response to process.</param>
/// <returns>The content of <paramref name="response"/> as a string.</returns>
public static string GetStringContent(this HttpResponseMessage response)
=> AsyncUtils.ResultOf(response.GetStringContentAsync());

/// <summary>Gets the content of an HTTP response as a string.</summary>
/// <param name="response">The response to process.</param>
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
Expand All @@ -135,11 +121,7 @@ public static async Task<string> GetStringContentAsync(this HttpResponseMessage
var stream = await content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
await using var _ = stream.ConfigureAwait(false);
using var sr = new StreamReader(stream, Encoding.GetEncoding(headers.GetContentEncoding()), false, 1024, true);
#if NET6_0
var text = await sr.ReadToEndAsync().ConfigureAwait(false);
#else
var text = await sr.ReadToEndAsync(cancellationToken).ConfigureAwait(false);
#endif
if (HttpUtils.TraceSource.Switch.ShouldTrace(TraceEventType.Verbose)) {
HttpUtils.TraceSource.TraceEvent(TraceEventType.Verbose, 2, "RESPONSE TEXT: {0}", TextUtils.FormatMultiLine(text));
}
Expand Down
2 changes: 1 addition & 1 deletion MetaBrainz.Common/MetaBrainz.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageCopyrightYears>2022, 2023, 2025</PackageCopyrightYears>
<PackageRepositoryName>MetaBrainz.Common</PackageRepositoryName>
<PackageTags>MetaBrainz</PackageTags>
<Version>3.0.1-pre</Version>
<Version>4.0.0-pre</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
22 changes: 0 additions & 22 deletions public-api/MetaBrainz.Common.net8.0.cs.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,6 @@

## Namespace: MetaBrainz.Common

### Type: AsyncUtils

```cs
public static class AsyncUtils {

public static void ResultOf(System.Threading.Tasks.Task task);

public static void ResultOf(System.Threading.Tasks.ValueTask task);

public static T ResultOf<T>(System.Threading.Tasks.Task<T> task);

public static T ResultOf<T>(System.Threading.Tasks.ValueTask<T> task);

}
```

### Type: HttpError

```cs
Expand Down Expand Up @@ -64,8 +48,6 @@ public class HttpError : System.Exception {

public HttpError(System.Net.HttpStatusCode status, string? reason = null, System.Version? version = null, string? message = null, System.Exception? cause = null);

public static HttpError FromResponse(System.Net.Http.HttpResponseMessage response);

public static System.Threading.Tasks.Task<HttpError> FromResponseAsync(System.Net.Http.HttpResponseMessage response, System.Threading.CancellationToken cancellationToken = default);

}
Expand All @@ -91,14 +73,10 @@ public static class HttpUtils {

public static System.Net.Http.Headers.ProductInfoHeaderValue CreateUserAgentHeader<T>();

public static System.Net.Http.HttpResponseMessage EnsureSuccessful(this System.Net.Http.HttpResponseMessage response);

public static System.Threading.Tasks.ValueTask<System.Net.Http.HttpResponseMessage> EnsureSuccessfulAsync(this System.Net.Http.HttpResponseMessage response, System.Threading.CancellationToken cancellationToken = default);

public static string GetContentEncoding(this System.Net.Http.Headers.HttpContentHeaders contentHeaders);

public static string GetStringContent(this System.Net.Http.HttpResponseMessage response);

public static System.Threading.Tasks.Task<string> GetStringContentAsync(this System.Net.Http.HttpResponseMessage response, System.Threading.CancellationToken cancellationToken = default);

}
Expand Down

0 comments on commit bdfc1c2

Please sign in to comment.