forked from brianstrauch/spotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist.go
59 lines (49 loc) · 1.74 KB
/
playlist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package spotify
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
)
// GetPlaylists gets a list of the playlists owned or followed by the current Spotify user.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-a-list-of-current-users-playlists
func (a *API) GetPlaylists() ([]*Playlist, error) {
playlistPage := &struct {
PagingMeta
Items []*Playlist `json:"items"`
}{}
// TODO: Iterate over all pages of playlists
err := a.get("v1", "/me/playlists", nil, playlistPage)
return playlistPage.Items, err
}
// CreatePlaylist creates a playlist for a Spotify user. (The playlist will be empty until you add tracks.)
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-create-playlist
func (a *API) CreatePlaylist(userID, name string, public, collaborative bool, description string) (*Playlist, error) {
query := make(url.Values)
query.Add("user_id", userID)
body := &struct {
Name string `json:"name"`
Public bool `json:"public"`
Collaborative bool `json:"collaborative"`
Description string `json:"description"`
}{
Name: name,
Public: public,
Collaborative: collaborative,
Description: description,
}
data, err := json.Marshal(body)
if err != nil {
return nil, err
}
playlist := new(Playlist)
err = a.post("v1", fmt.Sprintf("/users/%s/playlists", userID), query, bytes.NewReader(data), playlist)
return playlist, err
}
// GetPlaylist gets a playlist owned by a Spotify user.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-playlist
func (a *API) GetPlaylist(id string) (*Playlist, error) {
playlist := new(Playlist)
err := a.get("v1", fmt.Sprintf("/playlists/%s", id), nil, playlist)
return playlist, err
}