Skip to content

Commit

Permalink
Merge pull request #57 from Zastai/update-support-libraries
Browse files Browse the repository at this point in the history
Update support libraries (and drop `QueryException`)
  • Loading branch information
Zastai committed Dec 29, 2023
2 parents 858e2f4 + 6778ae3 commit 157c6bf
Show file tree
Hide file tree
Showing 95 changed files with 2,048 additions and 2,048 deletions.
4 changes: 2 additions & 2 deletions Directory.Packages.props
Expand Up @@ -4,8 +4,8 @@
<!-- Package Versions -->
<ItemGroup>
<PackageVersion Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageVersion Include="MetaBrainz.Common" Version="1.0.0" />
<PackageVersion Include="MetaBrainz.Common.Json" Version="5.1.0" />
<PackageVersion Include="MetaBrainz.Common" Version="3.0.0" />
<PackageVersion Include="MetaBrainz.Common.Json" Version="6.0.1" />
</ItemGroup>

</Project>
19 changes: 10 additions & 9 deletions MetaBrainz.MusicBrainz/Interfaces/IPagedQueryResults.cs
@@ -1,10 +1,11 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

using JetBrains.Annotations;

using MetaBrainz.Common;
using MetaBrainz.Common.Json;

namespace MetaBrainz.MusicBrainz.Interfaces;
Expand Down Expand Up @@ -46,8 +47,8 @@ public interface IPagedQueryResults<TResults, out TItem> : IJsonBasedObject
/// of results, based on <see cref="Offset"/> and <see cref="Limit"/>.
/// </summary>
/// <returns>This result set (with updated values).</returns>
/// <exception cref="QueryException">When the web service reports an error.</exception>
/// <exception cref="WebException">When something goes wrong with the web request.</exception>
/// <exception cref="HttpError">When the web service reports an error.</exception>
/// <exception cref="HttpRequestException">When something goes wrong with the request.</exception>
TResults Next();

/// <summary>
Expand All @@ -56,8 +57,8 @@ public interface IPagedQueryResults<TResults, out TItem> : IJsonBasedObject
/// </summary>
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
/// <returns>This result set (with updated values).</returns>
/// <exception cref="QueryException">When the web service reports an error.</exception>
/// <exception cref="WebException">When something goes wrong with the web request.</exception>
/// <exception cref="HttpError">When the web service reports an error.</exception>
/// <exception cref="HttpRequestException">When something goes wrong with the request.</exception>
Task<TResults> NextAsync(CancellationToken cancellationToken = default);

/// <summary>
Expand All @@ -77,8 +78,8 @@ public interface IPagedQueryResults<TResults, out TItem> : IJsonBasedObject
/// of results, based on <see cref="Offset"/> and <see cref="Limit"/>.
/// </summary>
/// <returns>This result set (with updated values).</returns>
/// <exception cref="QueryException">When the web service reports an error.</exception>
/// <exception cref="WebException">When something goes wrong with the web request.</exception>
/// <exception cref="HttpError">When the web service reports an error.</exception>
/// <exception cref="HttpRequestException">When something goes wrong with the request.</exception>
TResults Previous();

/// <summary>
Expand All @@ -87,8 +88,8 @@ public interface IPagedQueryResults<TResults, out TItem> : IJsonBasedObject
/// </summary>
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
/// <returns>This result set (with updated values).</returns>
/// <exception cref="QueryException">When the web service reports an error.</exception>
/// <exception cref="WebException">When something goes wrong with the web request.</exception>
/// <exception cref="HttpError">When the web service reports an error.</exception>
/// <exception cref="HttpRequestException">When something goes wrong with the request.</exception>
Task<TResults> PreviousAsync(CancellationToken cancellationToken = default);

/// <summary>The current results.</summary>
Expand Down
3 changes: 2 additions & 1 deletion MetaBrainz.MusicBrainz/Json/Converters.cs
Expand Up @@ -35,7 +35,8 @@ internal static class Converters {
yield return SearchResultsReader.Instance;
yield return TagReader.Instance;
// Other objects we deserialize
yield return MessageOrErrorReader.Instance;
yield return ErrorResultReader.Instance;
yield return MessageResultReader.Instance;
}
}

Expand Down
38 changes: 18 additions & 20 deletions MetaBrainz.MusicBrainz/Json/HelperMethods.cs
Expand Up @@ -15,25 +15,23 @@ internal static class HelperMethods {
return TimeSpan.FromMilliseconds(ms.Value);
}

public static EntityType ParseEntityType(string? text) {
return text switch {
"area" => EntityType.Area,
"artist" => EntityType.Artist,
"collection" => EntityType.Collection,
"event" => EntityType.Event,
"genre" => EntityType.Genre,
"instrument" => EntityType.Instrument,
"label" => EntityType.Label,
"place" => EntityType.Place,
"recording" => EntityType.Recording,
"release" => EntityType.Release,
"release-group" => EntityType.ReleaseGroup, // for Annotation
"release_group" => EntityType.ReleaseGroup, // for Collection and Relationship
"series" => EntityType.Series,
"url" => EntityType.Url,
"work" => EntityType.Work,
_ => EntityType.Unknown
};
}
public static EntityType ParseEntityType(string? text) => text switch {
"area" => EntityType.Area,
"artist" => EntityType.Artist,
"collection" => EntityType.Collection,
"event" => EntityType.Event,
"genre" => EntityType.Genre,
"instrument" => EntityType.Instrument,
"label" => EntityType.Label,
"place" => EntityType.Place,
"recording" => EntityType.Recording,
"release" => EntityType.Release,
"release-group" => EntityType.ReleaseGroup, // for Annotation
"release_group" => EntityType.ReleaseGroup, // for Collection and Relationship
"series" => EntityType.Series,
"url" => EntityType.Url,
"work" => EntityType.Work,
_ => EntityType.Unknown
};

}
48 changes: 48 additions & 0 deletions MetaBrainz.MusicBrainz/Json/Readers/AuthorizationErrorReader.cs
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Text.Json;

using MetaBrainz.Common.Json;
using MetaBrainz.Common.Json.Converters;
using MetaBrainz.MusicBrainz.Objects;

namespace MetaBrainz.MusicBrainz.Json.Readers;

internal sealed class AuthorizationErrorReader : ObjectReader<AuthorizationError> {

public static readonly AuthorizationErrorReader Instance = new();

protected override AuthorizationError ReadObjectContents(ref Utf8JsonReader reader, JsonSerializerOptions options) {
string? error = null;
string? description = null;
Dictionary<string, object?>? rest = null;
while (reader.TokenType == JsonTokenType.PropertyName) {
var prop = reader.GetPropertyName();
try {
reader.Read();
switch (prop) {
case "error":
error = reader.GetString();
break;
case "error_description":
description = reader.GetString();
break;
default:
rest ??= new Dictionary<string, object?>();
rest[prop] = reader.GetOptionalObject(options);
break;
}
}
catch (Exception e) {
throw new JsonException($"Failed to deserialize the '{prop}' property.", e);
}
reader.Read();
}
return new AuthorizationError {
Error = error,
Description = description,
UnhandledProperties = rest
};
}

}
30 changes: 19 additions & 11 deletions MetaBrainz.MusicBrainz/Json/Readers/DiscIdLookupResultReader.cs
Expand Up @@ -88,40 +88,44 @@ internal sealed class DiscIdLookupResultReader : ObjectReader<DiscIdLookupResult
}
reader.Read();
}
var result = new DiscIdLookupResult();
if (id is not null && offsets is not null && sectors is not null) { // Disc
DiscIdLookupResult result;
if (id is not null && offsets is not null && sectors is not null) {
// Disc
if (offsetCount is not null) {
var reported = offsetCount.Value;
var actual = offsets.Count;
if (reported != actual) {
throw new JsonException($"The number of offsets ({actual}) does not match the reported offset count ({reported}).");
}
}
result.Disc = new Disc(id, offsets, sectors.Value) {
var disc = new Disc(id, offsets, sectors.Value) {
Releases = releases,
};
result = new DiscIdLookupResult(disc);
// clear used fields
id = null;
offsetCount = null;
offsets = null;
releases = null;
sectors = null;
}
else if (id is not null && title is not null && tracks is not null) { // Stub
else if (id is not null && title is not null && tracks is not null) {
// CD Stub
if (trackCount is not null) {
var reported = trackCount.Value;
var actual = tracks.Count;
if (reported != actual) {
throw new JsonException($"The number of tracks ({actual}) does not match the reported track count ({reported}).");
}
}
result.Stub = new CdStub(id, title) {
var stub = new CdStub(id, title) {
Artist = artist,
Barcode = barcode,
Disambiguation = disambiguation,
TrackCount = trackCount ?? 0,
Tracks = tracks,
};
result = new DiscIdLookupResult(stub);
// clear used fields
artist = null;
barcode = null;
Expand All @@ -131,25 +135,29 @@ internal sealed class DiscIdLookupResultReader : ObjectReader<DiscIdLookupResult
trackCount = null;
tracks = null;
}
else if (id is null && releases is not null) { // Fuzzy Lookup - release list
else if (id is null && releases is not null) {
// Fuzzy Lookup - release list
if (releaseCount is not null) {
var reported = releaseCount.Value;
var actual = releases.Count;
if (reported != actual) // FIXME: Or should this just throw?
{
if (reported != actual) {
// FIXME: Or should this just throw?
rest["release-count"] = releaseCount.Value;
}
}
if (releaseOffset is not null && releaseOffset.Value != 0) // FIXME: Or should this just throw?
{
if (releaseOffset is not null && releaseOffset.Value != 0) {
// FIXME: Or should this just throw?
rest["release-offset"] = releaseOffset.Value;
}
result.Releases = releases;
result = new DiscIdLookupResult(releases);
// clear used fields
releaseCount = null;
releaseOffset = null;
releases = null;
}
else {
result = new DiscIdLookupResult();
}
// any field still set at this point is unhandled
if (artist is not null) {
rest["artist"] = artist;
Expand Down
Expand Up @@ -8,14 +8,13 @@

namespace MetaBrainz.MusicBrainz.Json.Readers;

internal sealed class MessageOrErrorReader : ObjectReader<MessageOrError> {
internal sealed class ErrorResultReader : ObjectReader<ErrorResult> {

public static readonly MessageOrErrorReader Instance = new();
public static readonly ErrorResultReader Instance = new();

protected override MessageOrError ReadObjectContents(ref Utf8JsonReader reader, JsonSerializerOptions options) {
protected override ErrorResult ReadObjectContents(ref Utf8JsonReader reader, JsonSerializerOptions options) {
string? error = null;
string? help = null;
string? message = null;
Dictionary<string, object?>? rest = null;
while (reader.TokenType == JsonTokenType.PropertyName) {
var prop = reader.GetPropertyName();
Expand All @@ -28,9 +27,6 @@ internal sealed class MessageOrErrorReader : ObjectReader<MessageOrError> {
case "help":
help = reader.GetString();
break;
case "message":
message = reader.GetString();
break;
default:
rest ??= new Dictionary<string, object?>();
rest[prop] = reader.GetOptionalObject(options);
Expand All @@ -42,10 +38,9 @@ internal sealed class MessageOrErrorReader : ObjectReader<MessageOrError> {
}
reader.Read();
}
return new MessageOrError {
return new ErrorResult {
Error = error,
Help = help,
Message = message,
UnhandledProperties = rest
};
}
Expand Down
43 changes: 43 additions & 0 deletions MetaBrainz.MusicBrainz/Json/Readers/MessageResultReader.cs
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Text.Json;

using MetaBrainz.Common.Json;
using MetaBrainz.Common.Json.Converters;
using MetaBrainz.MusicBrainz.Objects;

namespace MetaBrainz.MusicBrainz.Json.Readers;

internal sealed class MessageResultReader : ObjectReader<MessageResult> {

public static readonly MessageResultReader Instance = new();

protected override MessageResult ReadObjectContents(ref Utf8JsonReader reader, JsonSerializerOptions options) {
string? message = null;
Dictionary<string, object?>? rest = null;
while (reader.TokenType == JsonTokenType.PropertyName) {
var prop = reader.GetPropertyName();
try {
reader.Read();
switch (prop) {
case "message":
message = reader.GetString();
break;
default:
rest ??= new Dictionary<string, object?>();
rest[prop] = reader.GetOptionalObject(options);
break;
}
}
catch (Exception e) {
throw new JsonException($"Failed to deserialize the '{prop}' property.", e);
}
reader.Read();
}
return new MessageResult {
Message = message,
UnhandledProperties = rest
};
}

}
2 changes: 1 addition & 1 deletion MetaBrainz.MusicBrainz/MetaBrainz.MusicBrainz.csproj
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>

<Sdk Name="MetaBrainz.Build.Sdk" Version="3.1.1" />
<Sdk Name="MetaBrainz.Build.Sdk" Version="3.1.2" />

<PropertyGroup>
<Authors>Zastai</Authors>
Expand Down

0 comments on commit 157c6bf

Please sign in to comment.