Peter-Schorn / SpotifyAPI

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

Getting info from Current Playback Context #31

Closed kimfucious closed 3 years ago

kimfucious commented 3 years ago

HI @Peter-Schorn,

I am having a devil of a time trying to extract track info from currentPlayback(), and I thought you might be kind enough to provide some insight in this regard.

If I log the output from this, I can see that the currentPlayback??.context?.href and currentPlayback??.context?.externalUrls seem to be related to the artist, when what I'm trying to get at is the track name and it's album with the artwork. And since artists is an array, it invovles a bit more logic than I want to deal with for simply the aforementioned track related info.

So what I wound up doing is the below, which seems more effort (and an additional call) to get at data, which I thought would be easy to get at in the context, but apparently is not.

I realize that you didn't create the Spotify API, and that your wunderbar library is can only work with what it gives us, while making the lives of folks that use it easier (thanks, again!), but I imagine you know enough about it to answer me this:

Is the below, the most efficient way to get at track data from current playback context, or is there a better way?

Thanks!

if let currentPlayback = try await spotify.api.currentPlayback(market: "from_token").awaitSingleValue() {
        nowPlayingContext = currentPlayback // <- goes to state for playback control
        if let id = currentPlayback?.item?.id {
            let href = "spotify:track:\(id)"
            if let track = try await spotify.api.track(href).awaitSingleValue() {
                nowPlayingTrack = track // <- goes to state for rendering details in the view
            }
        }
}
Peter-Schorn commented 3 years ago

If I log the output from this, I can see that the currentPlayback??.context?.href and currentPlayback??.context?.externalUrls seem to be related to the artist

SpotifyContext could refer to an artist, album, playlist, or show.

if let id = currentPlayback?.item?.id {
    let href = "spotify:track:\(id)"

This is a URI, not an href. An href is a full URL to an endpoint, such as https://api.spotify.com/v1/tracks/6dGnYIeXmHdcikdzNNDMm2. And you shouldn't assume that the currently playing item is a track; it could also be an episode.

CurrentlyPlayingContext.item contains the currently playing track/episode. Read the documentation.

Peter-Schorn commented 3 years ago

I'm closing this because I assume you resolved your issue on your own. Open a new issue if you have any other questions.