Skip to content

Commit 22c9b38

Browse files
authored
Accept collaborative parameter for CreatePlaylistForUser (#158)
* Accept collaborative parameter for CreatePlaylistForUser The collaborative parameter is optional in the Spotify Web API, and was not supported in this library. I add the parameter to directly to the CreatePlaylistForUser function, though a case could be made for writing a separate function in order to avoid breaking existing calls. Note that I do not check for the relationship between public and collaborative in the body of the function, instead I let the Web API return an error. I added two tests for the private/public-collaborative permutations in order to demonstrate the intended usage of the fields. * Break out CreateCollaborativePlaylistForUser In order to avoid breaking users of V1 CreatePlaylistForUser, we create a separate function specific to creating collaborative playlists and undo the changes to CreatePlaylistForUser. * Remove unnecessary comment
1 parent 16e45d8 commit 22c9b38

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

playlist.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ func (c *Client) GetPlaylistTracksOpt(playlistID ID,
309309
// creating a private playlist requires ScopePlaylistModifyPrivate.
310310
//
311311
// On success, the newly created playlist is returned.
312+
// TODO Accept a collaborative parameter and delete
313+
// CreateCollaborativePlaylistForUser.
312314
func (c *Client) CreatePlaylistForUser(userID, playlistName, description string, public bool) (*FullPlaylist, error) {
313315
spotifyURL := fmt.Sprintf("%susers/%s/playlists", c.baseURL, userID)
314316
body := struct {
@@ -339,6 +341,42 @@ func (c *Client) CreatePlaylistForUser(userID, playlistName, description string,
339341
return &p, err
340342
}
341343

344+
// CreateCollaborativePlaylistForUser creates a playlist for a Spotify user.
345+
// A collaborative playlist is one that could have tracks added to and removed
346+
// from by other Spotify users.
347+
// Collaborative playlists must be private as per the Spotify API.
348+
func (c *Client) CreateCollaborativePlaylistForUser(userID, playlistName, description string) (*FullPlaylist, error) {
349+
spotifyURL := fmt.Sprintf("%susers/%s/playlists", c.baseURL, userID)
350+
body := struct {
351+
Name string `json:"name"`
352+
Public bool `json:"public"`
353+
Description string `json:"description"`
354+
Collaborative bool `json:"collaborative"`
355+
}{
356+
playlistName,
357+
false,
358+
description,
359+
true,
360+
}
361+
bodyJSON, err := json.Marshal(body)
362+
if err != nil {
363+
return nil, err
364+
}
365+
req, err := http.NewRequest("POST", spotifyURL, bytes.NewReader(bodyJSON))
366+
if err != nil {
367+
return nil, err
368+
}
369+
req.Header.Set("Content-Type", "application/json")
370+
371+
var p FullPlaylist
372+
err = c.execute(req, &p, http.StatusCreated)
373+
if err != nil {
374+
return nil, err
375+
}
376+
377+
return &p, err
378+
}
379+
342380
// ChangePlaylistName changes the name of a playlist. This call requires that the
343381
// user has authorized the ScopePlaylistModifyPublic or ScopePlaylistModifyPrivate
344382
// scopes (depending on whether the playlist is public or private).

playlist_test.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,10 @@ func TestUserFollowsPlaylist(t *testing.T) {
154154
}
155155
}
156156

157+
// NOTE collaborative is a fmt boolean.
157158
var newPlaylist = `
158159
{
159-
"collaborative": false,
160+
"collaborative": %t,
160161
"description": "Test Description",
161162
"external_urls": {
162163
"spotify": "http://open.spotify.com/user/thelinmichael/playlist/7d2D2S200NyUE5KYs80PwO"
@@ -194,7 +195,7 @@ var newPlaylist = `
194195
}`
195196

196197
func TestCreatePlaylist(t *testing.T) {
197-
client, server := testClientString(http.StatusCreated, newPlaylist)
198+
client, server := testClientString(http.StatusCreated, fmt.Sprintf(newPlaylist, false))
198199
defer server.Close()
199200

200201
p, err := client.CreatePlaylistForUser("thelinmichael", "A New Playlist", "Test Description", false)
@@ -213,6 +214,34 @@ func TestCreatePlaylist(t *testing.T) {
213214
if p.Tracks.Total != 0 {
214215
t.Error("Expected new playlist to be empty")
215216
}
217+
if p.Collaborative {
218+
t.Error("Expected non-collaborative playlist, got collaborative")
219+
}
220+
}
221+
222+
func TestCreateCollaborativePlaylist(t *testing.T) {
223+
client, server := testClientString(http.StatusCreated, fmt.Sprintf(newPlaylist, true))
224+
defer server.Close()
225+
226+
p, err := client.CreateCollaborativePlaylistForUser("thelinmichael", "A New Playlist", "Test Description")
227+
if err != nil {
228+
t.Error(err)
229+
}
230+
if p.IsPublic {
231+
t.Error("Expected private playlist, got public")
232+
}
233+
if p.Name != "A New Playlist" {
234+
t.Errorf("Expected 'A New Playlist', got '%s'\n", p.Name)
235+
}
236+
if p.Description != "Test Description" {
237+
t.Errorf("Expected 'Test Description', got '%s'\n", p.Description)
238+
}
239+
if p.Tracks.Total != 0 {
240+
t.Error("Expected new playlist to be empty")
241+
}
242+
if !p.Collaborative {
243+
t.Error("Expected collaborative playlist, got non-collaborative")
244+
}
216245
}
217246

218247
func TestRenamePlaylist(t *testing.T) {

0 commit comments

Comments
 (0)