Skip to content

Commit

Permalink
Add ToString() method to fluent API requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
wo80 committed Mar 24, 2024
1 parent 6fc3c0e commit 33be566
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 15 deletions.
22 changes: 18 additions & 4 deletions src/Hqub.MusicBrainz.Client/Example6.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,34 @@ public static async Task Search(MusicBrainzClient client, string genre, int limi

for (; i < pages && i < MAX_PAGES; i++)
{
// Try to avoid rate limit ...
await Task.Delay(1000);
// Advance browse offset.
request.Offset(i * limit);

// Advance browse offset and fetch results.
// Check if the request is in cache.
bool cached = await client.Cache.Contains(request.ToString());

if (!cached)
{
// Try to avoid MusicBrainz rate limit ...
await Task.Delay(1000);
}

// Fetch results.
groups = await request.Offset(i * limit).GetAsync();

DisplayReleases(groups, i + 1, pages);
}

Console.WriteLine();

if (i == MAX_PAGES)
{
Console.WriteLine();
Console.WriteLine("Stopping at page {0} ...", i);
}
else
{
Console.WriteLine("Done ...");
}
}

private static void DisplayArtists(ArtistList artists)
Expand Down
47 changes: 38 additions & 9 deletions src/Hqub.MusicBrainz/Services/BrowseRequest.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
namespace Hqub.MusicBrainz.Services
{
using Hqub.MusicBrainz.Entities.Collections;
using System;
using System.Threading;
using System.Threading.Tasks;

/// <summary>
/// Prepare a browse request to the MusicBrainz webservice.
/// Prepare a browse request to the MusicBrainz web service.
/// </summary>
/// <typeparam name="T">Any supported MusicBrainz entity.</typeparam>
public class BrowseRequest<T>
public abstract class BrowseRequest<T>
{
private readonly MusicBrainzClient client;

Expand Down Expand Up @@ -83,7 +82,7 @@ public BrowseRequest<T> Offset(int offset)
}

/// <summary>
/// Set the type of release or release-group to browse.
/// Set the type of the release or release-group to browse.
/// </summary>
/// <param name="type">The release type (for example 'album').</param>
/// <returns></returns>
Expand All @@ -97,7 +96,7 @@ public BrowseRequest<T> Type(string type)
}

/// <summary>
/// Set the status of release to browse.
/// Set the status of the release to browse.
/// </summary>
/// <param name="status">The release status (for example 'official').</param>
/// <returns></returns>
Expand All @@ -123,10 +122,12 @@ public async Task<T> GetAsync(CancellationToken ct = default)
/// <summary>
/// Initiate the actual request.
/// </summary>
protected virtual Task<T> BrowseAsync(MusicBrainzClient client, CancellationToken ct)
{
throw new NotImplementedException();
}
protected abstract Task<T> BrowseAsync(MusicBrainzClient client, CancellationToken ct);

/// <summary>
/// Returns the request path.
/// </summary>
public abstract override string ToString();
}

#region Helper classes
Expand All @@ -138,6 +139,7 @@ public ArtistBrowseRequest(MusicBrainzClient client, UrlBuilder builder, string
{
}

/// <inheritdoc />
protected override async Task<ArtistList> BrowseAsync(MusicBrainzClient client, CancellationToken ct)
{
string url = builder.CreateBrowseUrl(EntityName, relatedEntity, id, limit, offset, include);
Expand All @@ -146,6 +148,12 @@ protected override async Task<ArtistList> BrowseAsync(MusicBrainzClient client,

return new ArtistList() { Items = list.Items, Count = list.Count, Offset = list.Offset };
}

/// <inheritdoc />
public override string ToString()
{
return builder.CreateBrowseUrl(EntityName, relatedEntity, id, limit, offset, include);
}
}

class RecordingBrowseRequest : BrowseRequest<RecordingList>
Expand All @@ -155,6 +163,7 @@ public RecordingBrowseRequest(MusicBrainzClient client, UrlBuilder builder, stri
{
}

/// <inheritdoc />
protected override async Task<RecordingList> BrowseAsync(MusicBrainzClient client, CancellationToken ct)
{
string url = builder.CreateBrowseUrl(EntityName, relatedEntity, id, limit, offset, include);
Expand All @@ -163,6 +172,12 @@ protected override async Task<RecordingList> BrowseAsync(MusicBrainzClient clien

return new RecordingList() { Items = list.Items, Count = list.Count, Offset = list.Offset };
}

/// <inheritdoc />
public override string ToString()
{
return builder.CreateBrowseUrl(EntityName, relatedEntity, id, limit, offset, include);
}
}

class ReleaseGroupBrowseRequest : BrowseRequest<ReleaseGroupList>
Expand All @@ -172,6 +187,7 @@ public ReleaseGroupBrowseRequest(MusicBrainzClient client, UrlBuilder builder, s
{
}

/// <inheritdoc />
protected override async Task<ReleaseGroupList> BrowseAsync(MusicBrainzClient client, CancellationToken ct)
{
string url = builder.CreateBrowseUrl(EntityName, relatedEntity, id, type, null, limit, offset, include);
Expand All @@ -180,6 +196,12 @@ protected override async Task<ReleaseGroupList> BrowseAsync(MusicBrainzClient cl

return new ReleaseGroupList() { Items = list.Items, Count = list.Count, Offset = list.Offset };
}

/// <inheritdoc />
public override string ToString()
{
return builder.CreateBrowseUrl(EntityName, relatedEntity, id, type, null, limit, offset, include);
}
}

class ReleaseBrowseRequest : BrowseRequest<ReleaseList>
Expand All @@ -189,6 +211,7 @@ public ReleaseBrowseRequest(MusicBrainzClient client, UrlBuilder builder, string
{
}

/// <inheritdoc />
protected override async Task<ReleaseList> BrowseAsync(MusicBrainzClient client, CancellationToken ct)
{
string url = builder.CreateBrowseUrl(EntityName, relatedEntity, id, type, status, limit, offset, include);
Expand All @@ -197,6 +220,12 @@ protected override async Task<ReleaseList> BrowseAsync(MusicBrainzClient client,

return new ReleaseList() { Items = list.Items, Count = list.Count, Offset = list.Offset };
}

/// <inheritdoc />
public override string ToString()
{
return builder.CreateBrowseUrl(EntityName, relatedEntity, id, type, status, limit, offset, include);
}
}

#endregion
Expand Down
10 changes: 9 additions & 1 deletion src/Hqub.MusicBrainz/Services/LookupRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Threading.Tasks;

/// <summary>
/// Prepare a lookup request to the MusicBrainz webservice.
/// Prepare a lookup request to the MusicBrainz web service.
/// </summary>
/// <typeparam name="T">Any supported MusicBrainz entity.</typeparam>
public class LookupRequest<T>
Expand Down Expand Up @@ -54,5 +54,13 @@ public async Task<T> GetAsync(CancellationToken ct = default)

return await client.GetAsync<T>(url, ct);
}

/// <summary>
/// Returns the request path.
/// </summary>
public override string ToString()
{
return builder.CreateLookupUrl(EntityName, id, include);
}
}
}
10 changes: 9 additions & 1 deletion src/Hqub.MusicBrainz/Services/SearchRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Threading.Tasks;

/// <summary>
/// Prepare a search request to the MusicBrainz webservice.
/// Prepare a search request to the MusicBrainz web service.
/// </summary>
/// <typeparam name="T">Any supported MusicBrainz entity.</typeparam>
public class SearchRequest<T>
Expand Down Expand Up @@ -77,5 +77,13 @@ public async Task<T> GetAsync(CancellationToken ct = default)

return await client.GetAsync<T>(url, ct);
}

/// <summary>
/// Returns the request path.
/// </summary>
public override string ToString()
{
return builder.CreateSearchUrl(EntityName, query, limit, offset);
}
}
}

0 comments on commit 33be566

Please sign in to comment.