Skip to content

Commit

Permalink
Add catalog genre
Browse files Browse the repository at this point in the history
  • Loading branch information
rudrankriyam committed Apr 10, 2022
1 parent dc3a32b commit d0294aa
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions Sources/MusadoraKit/Catalog/CatalogGenre.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Genre>

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
extension AppleMusicEndpoint {
static var genres: Self {
Expand All @@ -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<Genre> {
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<Genre>] = []) async throws -> Genre {
var request = MusicCatalogResourceRequest<Genre>(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<Genre>] = []) async throws -> Genres {
var request = MusicCatalogResourceRequest<Genre>(matching: \.id, memberOf: ids)
request.properties = properties
let response = try await request.response()
return response.items
}
}

0 comments on commit d0294aa

Please sign in to comment.