Peter-Schorn / SpotifyAPI

A Swift library for the Spotify web API. Supports all endpoints.
https://peter-schorn.github.io/SpotifyAPI/documentation/spotifywebapi
MIT License
251 stars 32 forks source link

SpotifyDecodingError: The data from the Spotify web API could not be decoded into 'CurrentlyPlayingContext' #38

Closed dominicap closed 2 years ago

dominicap commented 2 years ago

Hello,

I am trying to use the "currentPlayback" endpoint, however when making a request, I receive a SpotifyDecidingError as seen below:

SpotifyDecodingError: The data from the Spotify web API could not be decoded into 'CurrentlyPlayingContext'
URL: https://api.spotify.com/v1/me/player?additional_types=track,episode
http status code: 200
pretty coding path: context.type
Underlying error:
▿ Swift.DecodingError.dataCorrupted
  ▿ dataCorrupted: Swift.DecodingError.Context
    ▿ codingPath: 2 elements
      - CodingKeys(stringValue: "context", intValue: nil)
      - CodingKeys(stringValue: "type", intValue: nil)
    - debugDescription: "Cannot initialize IDCategory from invalid String value collection"
    - underlyingError: nil

Here is the function which I invoke.

  private func timeIntervalForCurrentTrack() {
    spotify.api.currentPlayback()
      .sink(
        receiveCompletion: { completion in
          switch completion {
          case .finished:
            print(completion)
          case .failure(let error):
            print(error)
          }
        },
        receiveValue: { results in
          if let results = results {
            if let progress = results.progressMS {
              print(progress)
            }
          }
        }
      )
      .store(in: &spotify.cancellables)
  }

Any suggestions? Thanks so much!

Peter-Schorn commented 2 years ago

Actually I do know what causes this error. You were playing a track in the context of your liked songs, so the value in the JSON payload for context.type is "collection". My library attempts to decode this value into an instance of IDCategory, but there is no case for this value in the IDCategory enum.

I've fixed this issue in an upcoming release. Expect it shortly.

dominicap commented 2 years ago

Makes sense, thanks so much!

Peter-Schorn commented 2 years ago

Released version 2.1.0, which fixes this issue!