diff --git a/Sources/MusadoraKit/Catalog/CatalogGenre.swift b/Sources/MusadoraKit/Catalog/CatalogGenre.swift index 71f7bba31..c02d42749 100644 --- a/Sources/MusadoraKit/Catalog/CatalogGenre.swift +++ b/Sources/MusadoraKit/Catalog/CatalogGenre.swift @@ -8,6 +8,10 @@ import Foundation import MusicKit +/// A collection of genres. +@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) +public typealias Genres = MusicItemCollection + @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) extension AppleMusicEndpoint { static var genres: Self { @@ -17,7 +21,34 @@ extension AppleMusicEndpoint { @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) public extension MusadoraKit { - static func genres() async throws -> MusicItemCollection { - try await self.decode(endpoint: .genres) + + /// Fetch a genre from the Apple Music catalog by using its identifier. + /// - Parameters: + /// - id: The unique identifier for the genre. + /// - properties: Additional relationships to fetch with the genre. + /// - Returns: `Genre` matching the given identifier. + static func catalogGenre(id: MusicItemID, + with properties: [PartialMusicAsyncProperty] = []) async throws -> Genre { + var request = MusicCatalogResourceRequest(matching: \.id, equalTo: id) + request.properties = properties + let response = try await request.response() + + guard let genre = response.items.first else { + throw MusadoraKitError.notFound(for: id.rawValue) + } + return genre + } + + /// Fetch multiple genres from the Apple Music catalog by using their identifiers. + /// - Parameters: + /// - ids: The unique identifiers for the genres. + /// - properties: Additional relationships to fetch with the genres. + /// - Returns: `Genres` matching the given identifiers. + static func catalogGenres(ids: [MusicItemID], + with properties: [PartialMusicAsyncProperty] = []) async throws -> Genres { + var request = MusicCatalogResourceRequest(matching: \.id, memberOf: ids) + request.properties = properties + let response = try await request.response() + return response.items } }