-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaylists.go
76 lines (64 loc) · 1.88 KB
/
playlists.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package spotifyclient
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
)
type PlaylistService service
func (p *PlaylistService) List() ([]*Playlist, error) {
playlistPage := &struct {
PagingMeta
Items []*Playlist `json:"items"`
}{}
// TODO: Iterate over all pages of playlists
err := p.client.get("v1", "/me/playlists", nil, playlistPage)
return playlistPage.Items, err
}
func (p *PlaylistService) Create(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 = p.client.post("v1", fmt.Sprintf("/users/%s/playlists", userID), query, bytes.NewReader(data), playlist)
return playlist, err
}
func (p *PlaylistService) Fetch(id string) (*Playlist, error) {
playlist := new(Playlist)
err := p.client.get("v1", fmt.Sprintf("/playlists/%s", id), nil, playlist)
return playlist, err
}
func (p *PlaylistService) Update(id, name, description string, public, collaborative bool) (*Playlist, error) {
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 = p.client.put("v1", fmt.Sprintf("/playlists/%s", id), nil, bytes.NewReader(data))
return playlist, err
}