|
| 1 | +// |
| 2 | +// InFavoritesParser.swift |
| 3 | +// MusadoraKit |
| 4 | +// |
| 5 | +// Created by Rudrank Riyam on 10/11/25. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +/// Response structure for Apple Music API inFavorites queries. |
| 11 | +private struct InFavoritesResponse: Decodable { |
| 12 | + let data: [InFavoritesResponseItem] |
| 13 | +} |
| 14 | + |
| 15 | +private struct InFavoritesResponseItem: Decodable { |
| 16 | + let attributes: InFavoritesAttributes? |
| 17 | + let relationships: InFavoritesRelationships? |
| 18 | +} |
| 19 | + |
| 20 | +private struct InFavoritesAttributes: Decodable { |
| 21 | + let inFavorites: Bool? |
| 22 | +} |
| 23 | + |
| 24 | +private struct InFavoritesRelationships: Decodable { |
| 25 | + let library: InFavoritesLibrary? |
| 26 | +} |
| 27 | + |
| 28 | +private struct InFavoritesLibrary: Decodable { |
| 29 | + struct Item: Decodable {} |
| 30 | + let data: [Item] |
| 31 | +} |
| 32 | + |
| 33 | +/// Internal parser for extracting inFavorites status from Apple Music API responses. |
| 34 | +internal enum InFavoritesParser { |
| 35 | + /// Parse the inFavorites response from Apple Music API data. |
| 36 | + /// |
| 37 | + /// This function extracts the inFavorites boolean value from the API response, |
| 38 | + /// checking both that the item is in the library and that the inFavorites attribute exists. |
| 39 | + /// |
| 40 | + /// - Parameters: |
| 41 | + /// - data: The raw Data from the API response |
| 42 | + /// - itemType: The type of music item being parsed |
| 43 | + /// - Returns: The inFavorites boolean value |
| 44 | + /// - Throws: MusadoraKitError if parsing fails, item not found, not in library, or inFavorites not found |
| 45 | + static func parse(from data: Data, itemType: LibraryMusicItemType) throws -> Bool { |
| 46 | + let response = try JSONDecoder().decode(InFavoritesResponse.self, from: data) |
| 47 | + |
| 48 | + guard let item = response.data.first else { |
| 49 | + throw MusadoraKitError.notFound(for: "\(itemType.rawValue) in catalog") |
| 50 | + } |
| 51 | + |
| 52 | + guard let libraryData = item.relationships?.library?.data, !libraryData.isEmpty else { |
| 53 | + throw MusadoraKitError.notInLibrary(item: itemType.rawValue) |
| 54 | + } |
| 55 | + |
| 56 | + guard let inFavorites = item.attributes?.inFavorites else { |
| 57 | + throw MusadoraKitError.notFound(for: "inFavorites") |
| 58 | + } |
| 59 | + |
| 60 | + return inFavorites |
| 61 | + } |
| 62 | + |
| 63 | + /// Fetch inFavorites status for a music item from the Apple Music API. |
| 64 | + /// |
| 65 | + /// This helper method handles the common logic for fetching favorite status across all music item types. |
| 66 | + /// |
| 67 | + /// - Parameters: |
| 68 | + /// - id: The catalog ID of the music item |
| 69 | + /// - itemType: The type of music item |
| 70 | + /// - Returns: `true` if the item is in favorites, `false` otherwise |
| 71 | + /// - Throws: An error if the item is not in library or if the request fails |
| 72 | + static func fetchInFavorites(for id: MusicItemID, itemType: LibraryMusicItemType) async throws -> Bool { |
| 73 | + let storefront = try await MusicDataRequest.currentCountryCode |
| 74 | + var components = AppleMusicURLComponents() |
| 75 | + components.path = "catalog/\(storefront)/\(itemType.rawValue)/\(id.rawValue)" |
| 76 | + components.queryItems = [ |
| 77 | + URLQueryItem(name: "relate", value: "library"), |
| 78 | + URLQueryItem(name: "extend", value: "inFavorites") |
| 79 | + ] |
| 80 | + |
| 81 | + guard let url = components.url else { |
| 82 | + throw URLError(.badURL) |
| 83 | + } |
| 84 | + |
| 85 | + let request = MusicDataRequest(urlRequest: .init(url: url)) |
| 86 | + let response = try await request.response() |
| 87 | + |
| 88 | + return try parse(from: response.data, itemType: itemType) |
| 89 | + } |
| 90 | +} |
0 commit comments