Skip to content

Commit

Permalink
Update MLibraryResourceRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
rudrankriyam committed Mar 31, 2024
1 parent 316bae4 commit 0cd387a
Showing 1 changed file with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,32 @@ struct MLibraryResourceRequest<MusicItemType: MusicItem & Codable> {
/// A limit for the number of items to return
/// in the catalog resource response.
var limit: Int?

/// Creates a request to fetch all the items in alphabetical order.
init() {
setType()
}

/// Creates a request to fetch items using a filter that matches
/// a specific value.
init<Value>(matching _: KeyPath<MusicItemType.FilterLibraryType, Value>, equalTo value: Value) where MusicItemType: FilterableLibraryItem {
setType()

if let id = value as? MusicItemID {
ids = [id.rawValue]
}
}

/// Creates a request to fetch items using a filter that matches
/// any value from an array of possible values.
init<Value>(matching _: KeyPath<MusicItemType.FilterLibraryType, Value>, memberOf values: [Value]) where MusicItemType: FilterableLibraryItem {
setType()

if let ids = values as? [MusicItemID] {
self.ids = ids.map { $0.rawValue }
}
}

/// Fetches items from the user's library that match a specific filter.
func response() async throws -> MLibraryResourceResponse<MusicItemType> {
let url = try libraryEndpointURL
Expand Down Expand Up @@ -72,27 +72,31 @@ extension MLibraryResourceRequest {
default: type = nil
}
}

internal var libraryEndpointURL: URL {
get throws {
guard let type = type else { throw URLError(.badURL) }

var components = AppleMusicURLComponents()
var queryItems: [URLQueryItem]?
var queryItems: [URLQueryItem] = []
components.path = "me/library/\(type.rawValue)"

if let ids = ids {
queryItems = [URLQueryItem(name: "ids", value: ids.joined(separator: ","))]
} else if let limit = limit {
queryItems = [URLQueryItem(name: "limit", value: "\(limit)")]
queryItems += [URLQueryItem(name: "ids", value: ids.joined(separator: ","))]
}

if let limit = limit {
queryItems += [URLQueryItem(name: "limit", value: "\(limit)")]
}


queryItems += [URLQueryItem(name: "fields[albums]", value: "artistName,artistUrl,artwork,contentRating,editorialArtwork,name,playParams,releaseDate,url")]

components.queryItems = queryItems

guard let url = components.url else {
throw URLError(.badURL)
}

return url
}
}
Expand Down

0 comments on commit 0cd387a

Please sign in to comment.