Not getting results when searching MusicKit using MusicDataRequest

I am trying to use MusicDataRequest to search for albums, artists, and tracks using a search term like the album's names. If I use an albums Id, for example, I get results. The following code gets me results. First is the data model, the I have a class with code for authorization and then the function that searches using Music Kit

struct MyAlbumResponse: Decodable {

    let data: [Album]

}

class music {

    @State var isAuthorizedForMusicKit = false

    var searchTerm = ""

    func requestMusicAuthorization() {

        Task.detached {

            let authorizationStatus = await MusicAuthorization.request()

            if authorizationStatus == .authorized {

                self.isAuthorizedForMusicKit = true

            } else {

                }

            }

        }
func album() async throws {

        let auth = requestMusicAuthorization()

        let url = URL(string: "https://api.music.apple.com/v1/catalog/US/albums/1440881121")!

        let dataRequest = MusicDataRequest(urlRequest: URLRequest(url: url))

        let dataResponse = try await dataRequest.response()

        let decoder = JSONDecoder()

        let albumResponse = try decoder.decode(MyAlbumResponse.self, from: dataResponse.data)

        print(albumResponse.data)

    }

The above code returns the results I expect. But If I change the function to use the search endpoint, I do not get any results. I dont get any errors either. The data model is also different.

struct AlbumSearchResponse: Decodable {

    var albums: Albums

}

    

struct Albums: Codable {

    var data: [AlbumsDatum]

    var href, next: String

}





struct AlbumsDatum: Codable {

    var attributes: Attributes

    var href, id, type: String

}





struct Attributes: Codable {

    var artistName: String

    var artwork: Artwork

    var copyright: String

    var genreNames: [String]

    var isComplete, isMasteredForItunes, isSingle: Bool

    var name: String

    var playParams: PlayParams

    var releaseDate: String

    var trackCount: Int

    var url: String

    var editorialNotes: EditorialNotes?

}
class music {

    @State var isAuthorizedForMusicKit = false

    var searchTerm = ""

    

    func requestMusicAuthorization() {

        Task.detached {

            let authorizationStatus = await MusicAuthorization.request()

            if authorizationStatus == .authorized {

                self.isAuthorizedForMusicKit = true

            } else {

                

                    

                }

            }

        }
func musicSearch() async throws {

        var auth = requestMusicAuthorization()

        let url = URL(string: "https://api.music.apple.com/v1/catalog/us/search?term=colors&limit=10&types=albums")!

        let dataRequest = MusicDataRequest(urlRequest: URLRequest(url: url))

        let dataResponse = try await dataRequest.response()

        let decoder = JSONDecoder()

        let albumSearchResponse = try decoder.decode(AlbumSearchResponse.self, from: dataResponse.data)

        print(albumSearchResponse.albums)
        }

I have gone through all of the documentation and example applications but can't find an answer to this. I dont understand why the search endpoint would be not usable, if that is the case.

The workaround I have been using is to use MusicCatalogSearchRequest instead, but this does not return enough attributes and I cannot figure out how to have it return more attributes such as totalTracks, copyright, etc. I would be fine using the method if there is a way to add attributes to be included with the results. Here is the code I am using that works. It is also under the same class as the other functions.

func Search() async throws {

        let auth = requestMusicAuthorization()

        let searchTerm = "colors"

        Task {

                do {

                    // Issue a catalog search request for albums matching the search term.

                    var searchRequest = MusicCatalogSearchRequest(term: searchTerm, types: [Album.self])

                    searchRequest.limit = 20

                    let searchResponse = try await searchRequest.response()

                    print(searchResponse)
                }
            }

        }

any and all help is much appreciated. Thanks

Accepted Reply

Figured it out. There are more options for the search results that you can see by using the debugDescription

Replies

Figured it out. There are more options for the search results that you can see by using the debugDescription

Hello @AnimalOnDrums,

You shouldn't need to use MusicDataRequest to load a specific resource by ID. The following code using MusicCatalogResourceRequest will do essentially the same thing:

let request = MusicCatalogResourceRequest<Album>(matching: \.id, equalTo: "1440881121")
let response = try await request.response()
if let album = response.items.first {
    print("album: \(album)")
}

Similarly, you shouldn't need to use MusicDataRequest to load catalog search results. For that, MusicCatalogSearchRequest is indeed the right answer.

Can you please tell us more about this?

The workaround I have been using is to use MusicCatalogSearchRequest instead, but this does not return enough attributes and I cannot figure out how to have it return more attributes such as totalTracks, copyright, etc.

I tried it myself just now, and I get all the same attributes using the catalog search endpoint.

Please feel free to file a ticket on Feedback Assistant with more information about your issue.

I hope this helps.

Best regards,