Skip to content

Commit

Permalink
Add methods for .all for all catalog items
Browse files Browse the repository at this point in the history
  • Loading branch information
rudrankriyam committed Jul 23, 2022
1 parent 8c5fd66 commit 48d2d85
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 34 deletions.
79 changes: 66 additions & 13 deletions Sources/MusadoraKit/Catalog/CatalogAlbum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Album>] = []) async throws -> Album
{
with properties: [PartialMusicAsyncProperty<Album>] = []) async throws -> Album {
var request = MusicCatalogResourceRequest<Album>(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<Album>(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<Album>] = []) async throws -> Albums
{
with properties: [PartialMusicAsyncProperty<Album>] = []) async throws -> Albums {
var request = MusicCatalogResourceRequest<Album>(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<Album>(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<Album>] = []) async throws -> Album
{
with properties: [PartialMusicAsyncProperty<Album>] = []) async throws -> Album {
var request = MusicCatalogResourceRequest<Album>(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<Album>(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.
Expand All @@ -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<Album>(matching: \.upc, memberOf: upcs)
request.properties = .all
let response = try await request.response()
return response.items
}
}

extension Array where Element == PartialMusicAsyncProperty<Album> {
public static var all: Self {
var properties: [PartialMusicAsyncProperty<Album>] = [.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
}
}
29 changes: 27 additions & 2 deletions Sources/MusadoraKit/Catalog/CatalogCurator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Curator>] = []) async throws -> Curator
{
with properties: [PartialMusicAsyncProperty<Curator>] = []) async throws -> Curator {
var request = MusicCatalogResourceRequest<Curator>(matching: \.id, equalTo: id)
request.properties = properties
let response = try await request.response()
Expand All @@ -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<Curator>(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.
Expand All @@ -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<Curator>(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, *)
Expand Down
58 changes: 54 additions & 4 deletions Sources/MusadoraKit/Catalog/CatalogMusicVideo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<MusicVideo>] = []) async throws -> MusicVideo
{
with properties: [PartialMusicAsyncProperty<MusicVideo>] = []) async throws -> MusicVideo {
var request = MusicCatalogResourceRequest<MusicVideo>(matching: \.id, equalTo: id)
request.properties = properties
let response = try await request.response()
Expand All @@ -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<MusicVideo>(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<MusicVideo>] = []) async throws -> MusicVideos
{
with properties: [PartialMusicAsyncProperty<MusicVideo>] = []) async throws -> MusicVideos {
var request = MusicCatalogResourceRequest<MusicVideo>(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<MusicVideo>(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.
Expand All @@ -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<MusicVideo>(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.
Expand All @@ -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<MusicVideo>(matching: \.isrc, memberOf: isrc)
request.properties = .all
let response = try await request.response()
return response.items
}
}

extension Array where Element == PartialMusicAsyncProperty<MusicVideo> {
Expand Down
40 changes: 34 additions & 6 deletions Sources/MusadoraKit/Catalog/CatalogPlaylist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Playlist>] = []) async throws -> Playlist
{
with properties: [PartialMusicAsyncProperty<Playlist>] = []) async throws -> Playlist {
var request = MusicCatalogResourceRequest<Playlist>(matching: \.id, equalTo: id)
request.properties = properties
let response = try await request.response()
Expand All @@ -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<Playlist>(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<Playlist>] = []) async throws -> Playlists
{
with properties: [PartialMusicAsyncProperty<Playlist>] = []) async throws -> Playlists {
var request = MusicCatalogResourceRequest<Playlist>(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<Playlist>(matching: \.id, memberOf: ids)
request.properties = .all
let response = try await request.response()
return response.items
}
}

extension Array where Element == PartialMusicAsyncProperty<Playlist> {
public static var all: Self {
var properties: [PartialMusicAsyncProperty<Playlist>] = [.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
}
}
Loading

0 comments on commit 48d2d85

Please sign in to comment.