From 48d2d85849f650bc8939526598f6c53cebe261ef Mon Sep 17 00:00:00 2001 From: Rudrank Riyam Date: Sun, 24 Jul 2022 01:23:54 +0530 Subject: [PATCH] Add methods for .all for all catalog items --- .../MusadoraKit/Catalog/CatalogAlbum.swift | 79 ++++++++++++++++--- .../MusadoraKit/Catalog/CatalogCurator.swift | 29 ++++++- .../Catalog/CatalogMusicVideo.swift | 58 +++++++++++++- .../MusadoraKit/Catalog/CatalogPlaylist.swift | 40 ++++++++-- .../Catalog/CatalogRadioShow.swift | 32 +++++++- Sources/MusadoraKit/Catalog/CatalogSong.swift | 48 +++++++++-- 6 files changed, 252 insertions(+), 34 deletions(-) diff --git a/Sources/MusadoraKit/Catalog/CatalogAlbum.swift b/Sources/MusadoraKit/Catalog/CatalogAlbum.swift index 5b4143a80..cd46d4539 100644 --- a/Sources/MusadoraKit/Catalog/CatalogAlbum.swift +++ b/Sources/MusadoraKit/Catalog/CatalogAlbum.swift @@ -17,50 +17,88 @@ public extension MusadoraKit { /// - properties: Additional relationships to fetch with the album. /// - Returns: `Album` matching the given identifier. static func catalogAlbum(id: MusicItemID, - with properties: [PartialMusicAsyncProperty] = []) async throws -> Album - { + with properties: [PartialMusicAsyncProperty] = []) async throws -> Album { var request = MusicCatalogResourceRequest(matching: \.id, equalTo: id) request.properties = properties let response = try await request.response() - + guard let album = response.items.first else { throw MusadoraKitError.notFound(for: id.rawValue) } return album } - + + /// Fetch an album from the Apple Music catalog by using its identifier with all properties. + /// - Parameters: + /// - id: The unique identifier for the album. + /// - Returns: `Album` matching the given identifier. + static func catalogAlbum(id: MusicItemID) async throws -> Album { + var request = MusicCatalogResourceRequest(matching: \.id, equalTo: id) + request.properties = .all + let response = try await request.response() + + guard let album = response.items.first else { + throw MusadoraKitError.notFound(for: id.rawValue) + } + return album + } + /// Fetch one or more albums from the Apple Music catalog by using their identifiers. /// - Parameters: /// - ids: The unique identifiers for the albums. /// - properties: Additional relationships to fetch with the albums. /// - Returns: `Albums` matching the given identifiers. static func catalogAlbums(ids: [MusicItemID], - with properties: [PartialMusicAsyncProperty] = []) async throws -> Albums - { + with properties: [PartialMusicAsyncProperty] = []) async throws -> Albums { var request = MusicCatalogResourceRequest(matching: \.id, memberOf: ids) request.properties = properties let response = try await request.response() return response.items } - + + /// Fetch one or more albums from the Apple Music catalog by using their identifiers with all properties. + /// - Parameters: + /// - ids: The unique identifiers for the albums. + /// - Returns: `Albums` matching the given identifiers. + static func catalogAlbums(ids: [MusicItemID]) async throws -> Albums { + var request = MusicCatalogResourceRequest(matching: \.id, memberOf: ids) + request.properties = .all + let response = try await request.response() + return response.items + } + /// Fetch an album from Apple Music catalog by using their UPC value. /// - Parameters: /// - upc: The UPC (Universal Product Code) value for the album or single. /// - properties: Additional relationships to fetch with the album. /// - Returns: `Album` matching the given UPC value. static func catalogAlbums(upc: String, - with properties: [PartialMusicAsyncProperty] = []) async throws -> Album - { + with properties: [PartialMusicAsyncProperty] = []) async throws -> Album { var request = MusicCatalogResourceRequest(matching: \.upc, equalTo: upc) request.properties = properties let response = try await request.response() - + guard let album = response.items.first else { throw MusadoraKitError.notFound(for: upc) } return album } - + + /// Fetch an album from Apple Music catalog by using their UPC value with all properties. + /// - Parameters: + /// - upc: The UPC (Universal Product Code) value for the album or single. + /// - Returns: `Album` matching the given UPC value. + static func catalogAlbums(upc: String) async throws -> Album { + var request = MusicCatalogResourceRequest(matching: \.upc, equalTo: upc) + request.properties = .all + let response = try await request.response() + + guard let album = response.items.first else { + throw MusadoraKitError.notFound(for: upc) + } + return album + } + /// Fetch multiple albums from Apple Music catalog by using their UPC values. /// - Parameters: /// - upcs: The UPC (Universal Product Code) values for the albums or singles. @@ -74,16 +112,31 @@ public extension MusadoraKit { let response = try await request.response() return response.items } + + /// Fetch multiple albums from Apple Music catalog by using their UPC values with all properties. + /// - Parameters: + /// - upcs: The UPC (Universal Product Code) values for the albums or singles. + /// - Returns: `Albums` matching the given UPC values. + static func catalogAlbums(upcs: [String]) async throws -> Albums { + var request = MusicCatalogResourceRequest(matching: \.upc, memberOf: upcs) + request.properties = .all + let response = try await request.response() + return response.items + } } extension Array where Element == PartialMusicAsyncProperty { public static var all: Self { + var properties: [PartialMusicAsyncProperty] = [.artistURL, .genres, .artists, .appearsOn, .otherVersions, .recordLabels, .relatedAlbums, .relatedVideos, .tracks] #if compiler(>=5.7) if #available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) { - return [.artistURL, .genres, .artists, .audioVariants, .appearsOn, .otherVersions, .recordLabels, .relatedAlbums, .relatedVideos, .tracks] + properties += [.audioVariants] + return properties + } else { + return properties } #else - return [.artistURL, .genres, .artists, .appearsOn, .otherVersions, .recordLabels, .relatedAlbums, .relatedVideos, .tracks] + return properties #endif } } diff --git a/Sources/MusadoraKit/Catalog/CatalogCurator.swift b/Sources/MusadoraKit/Catalog/CatalogCurator.swift index 36599da12..8f8b43c62 100644 --- a/Sources/MusadoraKit/Catalog/CatalogCurator.swift +++ b/Sources/MusadoraKit/Catalog/CatalogCurator.swift @@ -21,8 +21,7 @@ public extension MusadoraKit { /// - properties: Additional relationships to fetch with the curator. /// - Returns: `Curator` matching the given identifier. static func catalogCurator(id: MusicItemID, - with properties: [PartialMusicAsyncProperty] = []) async throws -> Curator - { + with properties: [PartialMusicAsyncProperty] = []) async throws -> Curator { var request = MusicCatalogResourceRequest(matching: \.id, equalTo: id) request.properties = properties let response = try await request.response() @@ -33,6 +32,21 @@ public extension MusadoraKit { return curator } + /// Fetch a curator from the Apple Music catalog by using its identifier with all properties. + /// - Parameters: + /// - id: The unique identifier for the curator. + /// - Returns: `Curator` matching the given identifier. + static func catalogCurator(id: MusicItemID) async throws -> Curator { + var request = MusicCatalogResourceRequest(matching: \.id, equalTo: id) + request.properties = .all + let response = try await request.response() + + guard let curator = response.items.first else { + throw MusadoraKitError.notFound(for: id.rawValue) + } + return curator + } + /// Fetch multiple curators from the Apple Music catalog by using their identifiers. /// - Parameters: /// - ids: The unique identifiers for the curators. @@ -46,6 +60,17 @@ public extension MusadoraKit { let response = try await request.response() return response.items } + + /// Fetch multiple curators from the Apple Music catalog by using their identifiers with all properties. + /// - Parameters: + /// - ids: The unique identifiers for the curators. + /// - Returns: `Curators` matching the given identifiers. + static func catalogCurators(ids: [MusicItemID]) async throws -> Curators { + var request = MusicCatalogResourceRequest(matching: \.id, memberOf: ids) + request.properties = .all + let response = try await request.response() + return response.items + } } @available(iOS 15.4, macOS 12.3, tvOS 15.4, *) diff --git a/Sources/MusadoraKit/Catalog/CatalogMusicVideo.swift b/Sources/MusadoraKit/Catalog/CatalogMusicVideo.swift index e78171e48..513993fc0 100644 --- a/Sources/MusadoraKit/Catalog/CatalogMusicVideo.swift +++ b/Sources/MusadoraKit/Catalog/CatalogMusicVideo.swift @@ -17,8 +17,7 @@ public extension MusadoraKit { /// - properties: Additional relationships to fetch with the music video. /// - Returns: `MusicVideo` matching the given identifier. static func catalogMusicVideo(id: MusicItemID, - with properties: [PartialMusicAsyncProperty] = []) async throws -> MusicVideo - { + with properties: [PartialMusicAsyncProperty] = []) async throws -> MusicVideo { var request = MusicCatalogResourceRequest(matching: \.id, equalTo: id) request.properties = properties let response = try await request.response() @@ -29,20 +28,45 @@ public extension MusadoraKit { return musicVideo } + /// Fetch a music video from the Apple Music catalog by using its identifier with all properties. + /// - Parameters: + /// - id: The unique identifier for the music video. + /// - Returns: `MusicVideo` matching the given identifier. + static func catalogMusicVideo(id: MusicItemID) async throws -> MusicVideo { + var request = MusicCatalogResourceRequest(matching: \.id, equalTo: id) + request.properties = .all + let response = try await request.response() + + guard let musicVideo = response.items.first else { + throw MusadoraKitError.notFound(for: id.rawValue) + } + return musicVideo + } + /// Fetch multiple music videos from the Apple Music catalog by using their identifiers. /// - Parameters: /// - ids: The unique identifiers for the music videos. /// - properties: Additional relationships to fetch with the music videos. /// - Returns: `MusicVideos` matching the given identifiers. static func catalogMusicVideos(ids: [MusicItemID], - with properties: [PartialMusicAsyncProperty] = []) async throws -> MusicVideos - { + with properties: [PartialMusicAsyncProperty] = []) async throws -> MusicVideos { var request = MusicCatalogResourceRequest(matching: \.id, memberOf: ids) request.properties = properties let response = try await request.response() return response.items } + /// Fetch multiple music videos from the Apple Music catalog by using their identifiers with all properties. + /// - Parameters: + /// - ids: The unique identifiers for the music videos. + /// - Returns: `MusicVideos` matching the given identifiers. + static func catalogMusicVideos(ids: [MusicItemID]) async throws -> MusicVideos { + var request = MusicCatalogResourceRequest(matching: \.id, memberOf: ids) + request.properties = .all + let response = try await request.response() + return response.items + } + /// Fetch one or more music videos from Apple Music catalog by using their ISRC value. /// /// Note that one ISRC value may return more than one music video. @@ -59,6 +83,19 @@ public extension MusadoraKit { return response.items } + /// Fetch one or more music videos from Apple Music catalog by using their ISRC value with all properties. + /// + /// Note that one ISRC value may return more than one music video. + /// - Parameters: + /// - isrc: The ISRC values for the music videos. + /// - Returns: `MusicVideos` matching the given ISRC value. + static func catalogMusicVideo(isrc: String) async throws -> MusicVideos { + var request = MusicCatalogResourceRequest(matching: \.isrc, equalTo: isrc) + request.properties = .all + let response = try await request.response() + return response.items + } + /// Fetch multiple music videos from Apple Music catalog by using their ISRC values. /// /// Note that one ISRC value may return more than one music video. @@ -74,6 +111,19 @@ public extension MusadoraKit { let response = try await request.response() return response.items } + + /// Fetch multiple music videos from Apple Music catalog by using their ISRC values with all properties. + /// + /// Note that one ISRC value may return more than one music video. + /// - Parameters: + /// - isrc: The ISRC values for the music videos. + /// - Returns: `MusicVideos` matching the given ISRC values. + static func catalogMusicVideos(isrc: [String]) async throws -> MusicVideos { + var request = MusicCatalogResourceRequest(matching: \.isrc, memberOf: isrc) + request.properties = .all + let response = try await request.response() + return response.items + } } extension Array where Element == PartialMusicAsyncProperty { diff --git a/Sources/MusadoraKit/Catalog/CatalogPlaylist.swift b/Sources/MusadoraKit/Catalog/CatalogPlaylist.swift index 91391daa4..528dde799 100644 --- a/Sources/MusadoraKit/Catalog/CatalogPlaylist.swift +++ b/Sources/MusadoraKit/Catalog/CatalogPlaylist.swift @@ -17,8 +17,7 @@ public extension MusadoraKit { /// - properties: Additional relationships to fetch with the playlist. /// - Returns: `Playlist` matching the given identifier. static func catalogPlaylist(id: MusicItemID, - with properties: [PartialMusicAsyncProperty] = []) async throws -> Playlist - { + with properties: [PartialMusicAsyncProperty] = []) async throws -> Playlist { var request = MusicCatalogResourceRequest(matching: \.id, equalTo: id) request.properties = properties let response = try await request.response() @@ -29,29 +28,58 @@ public extension MusadoraKit { return playlist } + /// Fetch a playlist from the Apple Music catalog by using its identifier with all properties. + /// - Parameters: + /// - id: The unique identifier for the playlist. + /// - Returns: `Playlist` matching the given identifier. + static func catalogPlaylist(id: MusicItemID) async throws -> Playlist { + var request = MusicCatalogResourceRequest(matching: \.id, equalTo: id) + request.properties = .all + let response = try await request.response() + + guard let playlist = response.items.first else { + throw MusadoraKitError.notFound(for: id.rawValue) + } + return playlist + } + /// Fetch multiple playlists from the Apple Music catalog by using their identifiers. /// - Parameters: /// - ids: The unique identifiers for the playlists. /// - properties: Additional relationships to fetch with the playlists. /// - Returns: `Playlists` matching the given identifiers. static func catalogPlaylists(ids: [MusicItemID], - with properties: [PartialMusicAsyncProperty] = []) async throws -> Playlists - { + with properties: [PartialMusicAsyncProperty] = []) async throws -> Playlists { var request = MusicCatalogResourceRequest(matching: \.id, memberOf: ids) request.properties = properties let response = try await request.response() return response.items } + + /// Fetch multiple playlists from the Apple Music catalog by using their identifiers with all properties. + /// - Parameters: + /// - ids: The unique identifiers for the playlists. + /// - Returns: `Playlists` matching the given identifiers. + static func catalogPlaylists(ids: [MusicItemID]) async throws -> Playlists { + var request = MusicCatalogResourceRequest(matching: \.id, memberOf: ids) + request.properties = .all + let response = try await request.response() + return response.items + } } extension Array where Element == PartialMusicAsyncProperty { public static var all: Self { + var properties: [PartialMusicAsyncProperty] = [.tracks, .featuredArtists, .moreByCurator] #if compiler(>=5.7) if #available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) { - return [.tracks, .curator, .featuredArtists, .moreByCurator, .radioShow] + properties += [.curator, .radioShow] + return properties + } else { + return properties } #else - return [.tracks, .featuredArtists, .moreByCurator] + return properties #endif } } diff --git a/Sources/MusadoraKit/Catalog/CatalogRadioShow.swift b/Sources/MusadoraKit/Catalog/CatalogRadioShow.swift index aa1231512..32fa222be 100644 --- a/Sources/MusadoraKit/Catalog/CatalogRadioShow.swift +++ b/Sources/MusadoraKit/Catalog/CatalogRadioShow.swift @@ -21,8 +21,7 @@ public extension MusadoraKit { /// - properties: Additional relationships to fetch with the radio show. /// - Returns: `RadioShow` matching the given identifier. static func catalogRadioShow(id: MusicItemID, - with properties: [PartialMusicAsyncProperty] = []) async throws -> RadioShow - { + with properties: [PartialMusicAsyncProperty] = []) async throws -> RadioShow { var request = MusicCatalogResourceRequest(matching: \.id, equalTo: id) request.properties = properties let response = try await request.response() @@ -33,19 +32,44 @@ public extension MusadoraKit { return radioShow } + /// Fetch a radio show from the Apple Music catalog by using its identifier with all properties. + /// - Parameters: + /// - id: The unique identifier for the radio show. + /// - Returns: `RadioShow` matching the given identifier. + static func catalogRadioShow(id: MusicItemID) async throws -> RadioShow { + var request = MusicCatalogResourceRequest(matching: \.id, equalTo: id) + request.properties = .all + let response = try await request.response() + + guard let radioShow = response.items.first else { + throw MusadoraKitError.notFound(for: id.rawValue) + } + return radioShow + } + /// Fetch multiple radio shows from the Apple Music catalog by using their identifiers. /// - Parameters: /// - ids: The unique identifiers for the radio shows. /// - properties: Additional relationships to fetch with the radio shows. /// - Returns: `RadioShows` matching the given identifiers. static func catalogCurators(ids: [MusicItemID], - with properties: [PartialMusicAsyncProperty] = []) async throws -> RadioShows - { + with properties: [PartialMusicAsyncProperty] = []) async throws -> RadioShows { var request = MusicCatalogResourceRequest(matching: \.id, memberOf: ids) request.properties = properties let response = try await request.response() return response.items } + + /// Fetch multiple radio shows from the Apple Music catalog by using their identifiers. + /// - Parameters: + /// - ids: The unique identifiers for the radio shows. + /// - Returns: `RadioShows` matching the given identifiers. + static func catalogCurators(ids: [MusicItemID]) async throws -> RadioShows { + var request = MusicCatalogResourceRequest(matching: \.id, memberOf: ids) + request.properties = .all + let response = try await request.response() + return response.items + } } @available(iOS 15.4, macOS 12.3, tvOS 15.4, *) diff --git a/Sources/MusadoraKit/Catalog/CatalogSong.swift b/Sources/MusadoraKit/Catalog/CatalogSong.swift index d1a867271..8db5866b1 100644 --- a/Sources/MusadoraKit/Catalog/CatalogSong.swift +++ b/Sources/MusadoraKit/Catalog/CatalogSong.swift @@ -28,12 +28,10 @@ public extension MusadoraKit { return song } -#if compiler(>=5.7) /// Fetch a song from the Apple Music catalog by using its identifier with all properties. /// - Parameters: /// - id: The unique identifier for the song. /// - Returns: `Song` matching the given identifier. - @available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) static func catalogSong(id: MusicItemID) async throws -> Song { var request = MusicCatalogResourceRequest(matching: \.id, equalTo: id) request.properties = .all @@ -44,7 +42,6 @@ public extension MusadoraKit { } return song } -#endif /// Fetch multiple songs from the Apple Music catalog by using their identifiers. /// - Parameters: @@ -59,6 +56,17 @@ public extension MusadoraKit { return response.items } + /// Fetch multiple songs from the Apple Music catalog by using their identifiers with all properties. + /// - Parameters: + /// - ids: The unique identifiers for the songs. + /// - Returns: `Songs` matching the given identifiers. + static func catalogSongs(ids: [MusicItemID]) async throws -> Songs { + var request = MusicCatalogResourceRequest(matching: \.id, memberOf: ids) + request.properties = .all + let response = try await request.response() + return response.items + } + /// Fetch one or more songs from Apple Music catalog by using their ISRC value. /// /// Note that one ISRC value may return more than one song. @@ -74,6 +82,19 @@ public extension MusadoraKit { return response.items } + /// Fetch one or more songs from Apple Music catalog by using their ISRC value with all properties. + /// + /// Note that one ISRC value may return more than one song. + /// - Parameters: + /// - isrc: The ISRC values for the songs. + /// - Returns: `Songs` matching the given ISRC value. + static func catalogSong(isrc: String) async throws -> Songs { + var request = MusicCatalogResourceRequest(matching: \.isrc, equalTo: isrc) + request.properties = .all + let response = try await request.response() + return response.items + } + /// Fetch multiple songs from Apple Music catalog by using their ISRC values. /// /// Note that one ISRC value may return more than one song. @@ -88,16 +109,33 @@ public extension MusadoraKit { let response = try await request.response() return response.items } + + /// Fetch multiple songs from Apple Music catalog by using their ISRC values with all properties. + /// + /// Note that one ISRC value may return more than one song. + /// - Parameters: + /// - isrc: The ISRC values for the songs. + /// - Returns: `Songs` matching the given ISRC values. + static func catalogSongs(isrc: [String]) async throws -> Songs { + var request = MusicCatalogResourceRequest(matching: \.isrc, memberOf: isrc) + request.properties = .all + let response = try await request.response() + return response.items + } } extension Array where Element == PartialMusicAsyncProperty { public static var all: Self { + var properties: [PartialMusicAsyncProperty] = [.albums, .artists, .composers, .genres, .musicVideos, .artistURL, .station] #if compiler(>=5.7) if #available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) { - return [.audioVariants, .albums, .artists, .composers, .genres, .musicVideos, .artistURL, .station] + properties += [.audioVariants] + return properties + } else { + return properties } #else - return [.albums, .artists, .composers, .genres, .musicVideos, .artistURL, .station] + return properties #endif } }