everyone.
I’m working on an interface using Spotify API.
When an User is logged, I use a command to fetch the playlist using: Get a list of current user’s playlists
As we can see, the playlist list is wrapped in a paging object. I use this decoder when firing my request:
listPlaylistDecoder : Decode.Decoder PlaylistsPagined
listPlaylistDecoder =
decode PlaylistsPagined
|> required "items" (Decode.list playlistDecoder)
|> optional "next" Decode.string "end"
When I’m back in the update loop, I check if I have “end” or the next url and I call the API until I get all playlists:
LoadPlaylists (Ok playlist) ->
case playlist.nextPage of
"end" ->
( { model | playlists = List.append model.playlists (List.map (\p -> { playlist = p, tracks = [] }) playlist.playlists) }, Cmd.batch (loadTracksBatch model.playlists)
url ->
( { model | playlists = List.append model.playlists <| List.map (\p -> { playlist = p, tracks = [] }) playlist.playlists }, (getPlaylists LoadPlaylists playlist.nextPage))
Then, I will start to get every tracks for each playlist with a Cmd.batch using: Get playlist’s tracks
We also have a paginated object wrapping the tracks… so I do the same thing with this decoder:
listTrackDecoder : Decode.Decoder TracksPagined
listTrackDecoder =
decode TracksPagined
|> required "items" (Decode.list trackDecoder)
|> optional "next" Decode.string "end"
|> required "href" Decode.string
I keep href around to be able to link a track to its playlist.
When I’m in the update loop, I check once again if I have to use next until I finished.
LoadTracksPlaylist (Ok tracks) ->
case tracks.nextPage of
"end" ->
( { model | playlists = addTracksToPlaylist model.playlists tracks }, loadFeaturesBatch model.playlists)
url ->
( { model | playlists = addTracksToPlaylist model.playlists tracks }, (fetchTracks tracks.nextPage LoadTracksPlaylist) )
When I got all the tracks, I want to get the audio features from each track.
I get the id for all my tracks and start to call this endpoint with the maximum tracks id allowed (100).
I feel like I should accumulate the response and ask for more result without having to go back in the update loop but I don’t see how I can implement this.
If you have any ideas, it would be great!