I'm trying to do something that I though would be simple; however, I'm tripping up on something...
In brief, I want to get all tracks from a playlist in an AppleMusic authorized user's playlist with the following function:
func getTracksFromAppleMusicPlaylist(playlistId: MusicItemID) async throws -> [Track]? {
print("Fetching AppleMusic Playlists...")
print("Playlist ID: \(playlistId)")
var playlistTracks: [Track]? = []
do {
var playlistRequest = MusicCatalogResourceRequest<Playlist>(matching: \.id, equalTo: playlistId )
playlistRequest.properties = [.tracks]
let playlistResponse = try await playlistRequest.response()
print("Playlist Response: \(playlistResponse)")
let playlistWithTracks = playlistResponse.items
let tracks = playlistWithTracks.flatMap { playlist -> MusicItemCollection<Track> in
playlist.tracks ?? []
}
playlistTracks = tracks.count > 1 ? tracks : nil
} catch {
print("Error", error)
// handle error
}
return playlistTracks
}
This function results in the following error:
2021-08-28 04:25:14.335455+0700 App[90763:6707890] [DataRequesting] Failed to perform MusicDataRequest.Context(
url: https://api.music.apple.com/v1/catalog/us/playlists/p.7XxxsXxXXxX?include=tracks&omit%5Bresource%5D=autos,
currentRetryCounts: [.other: 1]
) with MusicDataRequest.Error(
status: 404,
code: 40400,
title: "Resource Not Found",
detailText: "Resource with requested id was not found",
id: "QMK7GH4U7ITPMUTTBKIOMXXXX",
originalResponse: MusicDataResponse(
data: 159 bytes,
urlResponse: <NSHTTPURLResponse: 0x00000002820c0b60>
)
).
The playlistID being used is a value that has been picked from an existing playlist in the user's library, via a function that uses this code snippet:
if let url = URL(string: "https://api.music.apple.com/v1/me/library/playlists?limit=100") {
let dataRequest = MusicDataRequest(urlRequest: URLRequest(url: url))
...
The only thing I can think of is that the playlistId
from the above snippet is converted to a string when decoding into a struct, after which it is changed back to a MusicItemID
with an init, like MusicItemID(playlistId)
.
Any thoughts? Because I'm at a loss...