Hi there!
I am fetching library songs using the endpoint: https://api.music.apple.com/v1/me/library/songs
Here's the code snippet -
struct Songs: Decodable {
let data: [Song]
}
public func librarySongs() async throws {
let url = URL(string: "https://api.music.apple.com/v1/me/library/songs")
guard let url = url else { return }
let dataRequest = MusicDataRequest(urlRequest: URLRequest(url: url))
do {
let dataResponse = try await dataRequest.response()
print(dataResponse.debugDescription)
let response = try JSONDecoder().decode(Songs.self, from: dataResponse.data)
} catch {
print("**ERROR**")
print(error)
print(error.localizedDescription)
}
}
Trying to decode it using a custom struct gives me the following error. -
**ERROR**
keyNotFound(CodingKeys(stringValue: "artistName", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "data", intValue: nil), _JSONKey(stringValue: "Index 20", intValue: 20), CodingKeys(stringValue: "attributes", intValue: nil)], debugDescription: "No value associated with key CodingKeys(stringValue: \"artistName\", intValue: nil) (\"artistName\").", underlyingError: nil))
The data couldn’t be read because it is missing.
On digging through the response, I found that one of the songs doesn't have an artist name because it is just a recording of my song I added to the library through GarageBand.
In this case, what should be the approach?