Skip to content

Commit

Permalink
update catalog property to use userToken
Browse files Browse the repository at this point in the history
  • Loading branch information
rudrankriyam committed Mar 30, 2024
1 parent 05fa01c commit 316bae4
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions Sources/MusadoraKit/Library/LibraryCatalog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,29 @@ public extension FilterableLibraryItem {
get async throws {
let path = try LibraryMusicItemType.path(for: Self.self)
let url = try catalogURL(path: path)
let request = MusicDataRequest(urlRequest: URLRequest(url: url))
let response = try await request.response()
let items = try JSONDecoder().decode(MusicItemCollection<Self>.self, from: response.data)
let decoder = JSONDecoder()

guard let item = items.first else {
throw MusadoraKitError.notFound(for: id.rawValue)
if let userToken = MusadoraKit.userToken {
let request = MUserRequest(urlRequest: .init(url: url), userToken: userToken)
let data = try await request.response()
let items = try decoder.decode(MusicItemCollection<Self>.self, from: data)

guard let item = items.first else {
throw MusadoraKitError.notFound(for: id.rawValue)
}

return item
} else {
let request = MusicDataRequest(urlRequest: URLRequest(url: url))
let response = try await request.response()
let items = try decoder.decode(MusicItemCollection<Self>.self, from: response.data)

guard let item = items.first else {
throw MusadoraKitError.notFound(for: id.rawValue)
}

return item
}
return item
}
}

Expand Down

0 comments on commit 316bae4

Please sign in to comment.