Get a Catalog Playlist's Relationship Directly by Name

Hi there!

I am trying to map the Apple Music API endpoints to MusicKit. For example, I am looking at the catalog playlist endpoints.

To get a catalog playlist by its identifier, it is:

let request = MusicCatalogResourceRequest<Playlist>(matching: \.id, equalTo: "pl.f4d106fed2bd41149aaacabb233eb5eb")

let response = try await request.response()

print(response.items)

For multiple identifiers, it is:

  let request = MusicCatalogResourceRequest<Playlist>(matching: \.id, memberOf: ["pl.f4d106fed2bd41149aaacabb233eb5eb", "pl.4b364b8b182f4115acbf6deb83bd5222"])

let response = try await request.response()

print(response.items)

To get its relationship like more by curator and featured artists, I can set the properties:

var request = MusicCatalogResourceRequest<Playlist>(matching: \.id, equalTo: "pl.f4d106fed2bd41149aaacabb233eb5eb")

request.properties = [.featuredArtists, .moreByCurator, .tracks]

let response = try await request.response()

print(response.items)

My question is, how can I map the endpoint Get a Catalog Playlist's Relationship Directly by Name" to MusicKit:

https://api.music.apple.com/v1/catalog/{storefront}/playlists/{id}/{relationship}

The possible value of the relationship can be curator, library, or tracks. Where can I set these relationship values in the MusicKit request?

Thank you!

Answered by Frameworks Engineer in 709284022

Hello @snuff4,

Thank you for your question on how to get relationships directly by name.

In order for MusicKit to offer a nice and expressive API in Swift to expose data from the Apple Music catalog, we are often lead to consider the plethora of different endpoints available in Apple Music API, and expose a curated and carefully crafted set of APIs in Swift to get to the essence of this data.

For the specific example of the tracks relationship of a Playlist, you can get this data by loading additional properties using either the with(…) method, or using MusicCatalogResourceRequest as you just showed.

Granted, it's not loading only the tracks; it's in fact loading the playlist alongside with the tracks; but you still get the same data about the tracks.

You already noted in this thread that we are currently missing the curator relationship for Playlist. Similarly, MusicKit doesn't expose the library relationship.

As explained in that thread, for the time being, you could load this raw data using MusicDataRequest, define your own Decodable-conforming representation of this resource, and decode the response's data using MusicItemCollection and your own custom structure.

I hope this helps.

Best regards,

Accepted Answer

Hello @snuff4,

Thank you for your question on how to get relationships directly by name.

In order for MusicKit to offer a nice and expressive API in Swift to expose data from the Apple Music catalog, we are often lead to consider the plethora of different endpoints available in Apple Music API, and expose a curated and carefully crafted set of APIs in Swift to get to the essence of this data.

For the specific example of the tracks relationship of a Playlist, you can get this data by loading additional properties using either the with(…) method, or using MusicCatalogResourceRequest as you just showed.

Granted, it's not loading only the tracks; it's in fact loading the playlist alongside with the tracks; but you still get the same data about the tracks.

You already noted in this thread that we are currently missing the curator relationship for Playlist. Similarly, MusicKit doesn't expose the library relationship.

As explained in that thread, for the time being, you could load this raw data using MusicDataRequest, define your own Decodable-conforming representation of this resource, and decode the response's data using MusicItemCollection and your own custom structure.

I hope this helps.

Best regards,

Get a Catalog Playlist's Relationship Directly by Name
 
 
Q