ParkSquare.Discogs 1.0.12

.NET Standard 2.1
There is a newer version of this package available.
See the version list below for details.
dotnet add package ParkSquare.Discogs --version 1.0.12
NuGet\Install-Package ParkSquare.Discogs -Version 1.0.12
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ParkSquare.Discogs" Version="1.0.12" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ParkSquare.Discogs --version 1.0.12
#r "nuget: ParkSquare.Discogs, 1.0.12"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install ParkSquare.Discogs as a Cake Addin
#addin nuget:?package=ParkSquare.Discogs&version=1.0.12

// Install ParkSquare.Discogs as a Cake Tool
#tool nuget:?package=ParkSquare.Discogs&version=1.0.12

C# Discogs Library

Background

Discogs is a fully cross-referenced music database of recordings, releases, artists, labels and album artwork. Discogs provides a RESTful API to access this data, and this C# library is the easiest way to consume it so you can build your own Discogs-powered applications for the web, desktop, and mobile devices.

Getting Started

Sign up to get a Discogs account and go to Developer Settings to create a new auth token. Not all endpoints require authentication, however, the rate limit is higher for authenticated users so it will always be passed if you have configured one.

Rate Limits

The rate limit is generally around 60 requests per minute if authenticated, or 25 requests per minute if not. Once this limit has been reached, further calls will respond with 429 Too Many Requests. You should therefore make sure your code handles this, either with some sort of wait/retry or a circuit breaker.

Paged Results

Some of the API calls return paged data, with a maximum of 100 items per page. As such, you will need to either handle these scenarios yourself, or use the built-in convenience methods that automatically handle getting the paged results and presenting them back as one combined dataset.

Configuration

You must create an implementation of IClientConfig and pass this into the constructor of the ApiQueryBuilder class. The recommended way is to use the .Net Core built-in dependency injection framework, and to bind the properties to your appsettings.json file:

In Startup.cs:

services.AddSingleton<IClientConfig, ClientConfig>();

In your application:

public class ClientConfig : IClientConfig
{
    public ClientConfig(IConfiguration configuration)
    {
        configuration.Bind("DiscogsClient", this);
    }

    public string AuthToken { get; set; }

    public string BaseUrl { get; set; }
}

In appsettings.json:

{
  "DiscogsClient": {
    "AuthToken": "your-auth-token-here",
    "BaseUrl": "https://api.discogs.com"
  }
}

This method is not mandatory, you can pass in any implementation of the configuration interface, for example the values could just as easily be hardcoded:

public class HardCodedClientConfig : IClientConfig
{
    public string AuthToken => "your-auth-token";

    public string BaseUrl => "https://api.discogs.com";
}

Usage Examples

Discogs Client

All calls are made via the DiscogsClient object, which can be instantiated manually or using Dependency Injection (recommended).

Manual construction

var client = new DiscogsClient(
    new HttpClient(new HttpClientHandler()),
    new ApiQueryBuilder(new ClientConfig()));

Dependency injection

Add these lines to your Startup.cs:

services.AddSingleton<IApiQueryBuilder, ApiQueryBuilder>();

services.AddSingleton<IClientConfig, ClientConfig>();

services.AddHttpClient<IDiscogsClient, DiscogsClient>();
    .ConfigurePrimaryHttpMessageHandler(_ =>
        new HttpClientHandler
        {
            AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
        });

The last line registers a typed HttpClient for the DiscogsClient to use, and requires the following package:

Microsoft.Extensions.Http

There is a detailed discussion on using dependency injection with HttpClient here.

There are three methods for searching the database:

Task<SearchResults> SearchAsync(SearchCriteria criteria);

Task<SearchResults> SearchAsync(SearchCriteria criteria, PageOptions pageOptions);
    
Task<SearchResults> SearchAllAsync(SearchCriteria criteria);

The SearchAsync() methods allow specifying search criteria and optional pagination options. If no pagination options are passed, then the defaults will be used which returns the first page up to a maximum of 100 items.

Calling SearchAllAsync() automatically handles making and combing paged results.

This example shows all available search fields, the Query property is akin to performing a free text search and Title is generally in the format 'Artist - Title':

var searchResult = await _client.SearchAllAsync(new SearchCriteria
{
    Artist = "some artist",
    Title = "artist - title",
    ReleaseTitle = "some title",
    Query = "funk soul",
    Year = 1998,
    Track = "some track",
    Barcode = "111234",
    CatalogNumber = "XASIOIJH",
    Country = "UK",
    Format = "CD"
});

A search will return one or more Master Releases.

Master Release

A Master Release represents a set of similar Releases. It will also specify the Main Release, which is often chronologically the earliest.

var firstFoundMasterId = result.Results.First().MasterId;
var master = await _client.GetMasterReleaseAsync(firstFoundMasterId);

Versions

Master Release Versions are a set of all Releases that are a version of the Master Release. Similar to Search, this method has optional pagination properties:

Task<VersionResults> GetVersionsAsync(VersionsCriteria criteria);

Task<VersionResults> GetVersionsAsync(VersionsCriteria criteria, PageOptions pageOptions);

Task<VersionResults> GetAllVersionsAsync(VersionsCriteria criteria);

Also similar to Search, you can omit the pagination properties, or call GetAllVersionsAsync() to automatically handle retrieving and combining paged results.

var versions = await _client.GetAllVersionsAsync(new VersionsCriteria(firstFoundMasterId));

The result of the Versions methods contains information on the available filters, and how many items of each filter type are present in the dataset. The criteria object allow you to add filters for: format, label, release year and country.

Release

Release represents a particular physical or digital object released by one or more artists, and is where the bulk of the information about a recording is held.

var release = await _client.GetReleaseAsync(master.MainReleaseId);

Advanced Configuration

Handling Rate Limiting

You can use the excellent Polly library to automatically add retries and a circuit breaker pattern to your Discogs calls. This example exponentially backs off making calls and then gives up after three attempts. It will also break the circuit and stop making calls altogether if 3 failures are returned within a 15 second period.

services.AddHttpClient<IDiscogsClient, DiscogsClient>()
    .AddPolicyHandler(Policy.Handle<HttpRequestException>()
        .OrTransientHttpError()
        .OrResult(msg => msg.StatusCode == HttpStatusCode.TooManyRequests)
        .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2,
            retryAttempt))))
    .AddPolicyHandler(Policy.Handle<HttpRequestException>()
        .OrTransientHttpError()
        .OrResult(msg => msg.StatusCode == HttpStatusCode.TooManyRequests)
        .CircuitBreakerAsync(3, TimeSpan.FromSeconds(15)
        ))
    .ConfigurePrimaryHttpMessageHandler(_ =>
        new HttpClientHandler
        {
            AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
        });
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.1
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.34 215 8/22/2022
1.0.33 143 8/22/2022
1.0.31 147 8/19/2022
1.0.30 395 4/14/2022
1.0.28 232 2/19/2022
1.0.26 196 1/27/2022
1.0.24 201 1/12/2022
1.0.22 5,960 11/24/2021
1.0.20 4,819 11/23/2021
1.0.18 4,605 11/23/2021
1.0.16 6,337 11/23/2021
1.0.15 154 11/22/2021
1.0.14 572 11/21/2021
1.0.13 938 11/21/2021
1.0.12 1,205 11/21/2021
1.0.9 1,258 11/20/2021