zmb3 / spotify

A Go wrapper for the Spotify Web API
Apache License 2.0
1.37k stars 288 forks source link

Would you be interested in a method which returns all the playlists of a user? #28

Closed avinassh closed 7 years ago

avinassh commented 7 years ago

I am working on something where I need all the playlists of a user. I am not sure if this should be part of this library or not. Anyways here is my code:

func (c *Client) CurrentUsersAllPlaylists() (*SimplePlaylistPage, error){
    var allPlaylists *SimplePlaylistPage
    var total int
    limit := 50
    offset := 0
    opt := Options{
        Limit:  &limit,
        Offset: &offset,
    }
    for {
        playlists, err := c.CurrentUsersPlaylistsOpt(&opt)
        if err != nil {
            return nil, err
        }
        total = playlists.Total
        if allPlaylists == nil {
            allPlaylists = playlists
        } else {
            allPlaylists.Playlists = append(allPlaylists.Playlists, playlists.Playlists...)
        }
        offset = offset + limit
        if total < offset {
            break
        }
    }
    return allPlaylists, nil
}

If you think this is a good idea to have this as part of spotify lib, then I would be happy to send a PR 😄

zmb3 commented 7 years ago

What you did here is fine, but I'm hesitant to add it to the library just because there's nothing special about playlists. If we add support for paging through all playlists, then any calls that return pages should have similar functionality. It would be too much work to do this for everything and would add too much to the API.

Note that all pages return prev and next URLs to get the prev/next set of pages. It may be worth looking into a utility function that can walk through pages until the next page comes back empty.

avinassh commented 7 years ago

Makes sense, thank you! (So closing this issue)

It may be worth looking into a utility function that can walk through pages until the next page comes back empty.

Tell me more?