Skip to content

Commit 88ae444

Browse files
committed
Merge pull request #10 from JMPerez/add-follow-unfollow-playlists
Add methods to follow and unfollow playlists
2 parents 03954ea + 03a9943 commit 88ae444

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

src/spotify-web-api.js

+46
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,31 @@ var SpotifyWebApi = (function() {
274274
return _checkParamsAndPerformRequest(requestData, callback);
275275
};
276276

277+
/**
278+
* Add the current user as a follower of one playlist.
279+
* See [Follow a Playlist](https://developer.spotify.com/web-api/follow-playlist/) on
280+
* the Spotify Developer site for more information about the endpoint.
281+
* @param {string} ownerId The id of the playlist owner. If you know the Spotify URI of
282+
* the playlist, it is easy to find the owner's user id
283+
* (e.g. spotify:user:<here_is_the_owner_id>:playlist:xxxx)
284+
* @param {string} playlistId The id of the playlist. If you know the Spotify URI it is easy
285+
* to find the playlist id (e.g. spotify:user:xxxx:playlist:<here_is_the_playlist_id>)
286+
* @param {Object} options A JSON object with options that can be passed. For instance,
287+
* whether you want the playlist to be followed privately ({public: false})
288+
* @param {function(Object, Object)} callback An optional callback that receives 2 parameters. The first
289+
* one is the error object (null if no error), and the second is an empty value if the request succeeded.
290+
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
291+
*/
292+
Constr.prototype.followPlaylist = function(ownerId, playlistId, options, callback) {
293+
var requestData = {
294+
url: _baseUri + '/users/' + ownerId + '/playlists/' + playlistId + '/followers',
295+
type: 'PUT',
296+
postData: {}
297+
};
298+
299+
return _checkParamsAndPerformRequest(requestData, options, callback);
300+
};
301+
277302
/**
278303
* Removes the current user as a follower of one or more other Spotify users.
279304
* See [Unfollow Artists or Users](https://developer.spotify.com/web-api/unfollow-artists-users/) on
@@ -318,6 +343,27 @@ var SpotifyWebApi = (function() {
318343
return _checkParamsAndPerformRequest(requestData, callback);
319344
};
320345

346+
/**
347+
* Remove the current user as a follower of one playlist.
348+
* See [Unfollow a Playlist](https://developer.spotify.com/web-api/unfollow-playlist/) on
349+
* the Spotify Developer site for more information about the endpoint.
350+
* @param {string} ownerId The id of the playlist owner. If you know the Spotify URI of
351+
* the playlist, it is easy to find the owner's user id
352+
* (e.g. spotify:user:<here_is_the_owner_id>:playlist:xxxx)
353+
* @param {string} playlistId The id of the playlist. If you know the Spotify URI it is easy
354+
* to find the playlist id (e.g. spotify:user:xxxx:playlist:<here_is_the_playlist_id>)
355+
* @param {function(Object, Object)} callback An optional callback that receives 2 parameters. The first
356+
* one is the error object (null if no error), and the second is an empty value if the request succeeded.
357+
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
358+
*/
359+
Constr.prototype.unfollowPlaylist = function(ownerId, playlistId, callback) {
360+
var requestData = {
361+
url: _baseUri + '/users/' + ownerId + '/playlists/' + playlistId + '/followers',
362+
type: 'DELETE'
363+
};
364+
return _checkParamsAndPerformRequest(requestData, callback);
365+
};
366+
321367
/**
322368
* Checks to see if the current user is following one or more other Spotify users.
323369
* See [Check if Current User Follows](https://developer.spotify.com/web-api/check-current-user-follows/) on

tests/js/spec/test.js

+33
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,39 @@
685685
expect(that.requests).to.have.length(1);
686686
expect(that.requests[0].url).to.equal('https://api.spotify.com/v1/me/following/?ids=artistid01%2Cartistid02&type=artist');
687687
});
688+
689+
it('should follow a playlist publicly', function() {
690+
var callback = sinon.spy();
691+
var api = new SpotifyWebApi();
692+
api.followPlaylist('spotify', '2ujjMpFriZ2nayLmrD1Jgl', callback);
693+
that.requests[0].respond(200);
694+
expect(that.requests[0].method).to.equal('PUT');
695+
expect(callback.calledWith(null, '')).to.be.ok;
696+
expect(that.requests).to.have.length(1);
697+
expect(that.requests[0].url).to.equal('https://api.spotify.com/v1/users/spotify/playlists/2ujjMpFriZ2nayLmrD1Jgl/followers');
698+
});
699+
700+
it('should follow a playlist privately', function() {
701+
var callback = sinon.spy();
702+
var api = new SpotifyWebApi();
703+
api.followPlaylist('spotify', '2ujjMpFriZ2nayLmrD1Jgl', {"public": false}, callback);
704+
that.requests[0].respond(200);
705+
expect(that.requests[0].method).to.equal('PUT');
706+
expect(callback.calledWith(null, '')).to.be.ok;
707+
expect(that.requests).to.have.length(1);
708+
expect(that.requests[0].url).to.equal('https://api.spotify.com/v1/users/spotify/playlists/2ujjMpFriZ2nayLmrD1Jgl/followers');
709+
});
710+
711+
it('should unfollow a playlist', function() {
712+
var callback = sinon.spy();
713+
var api = new SpotifyWebApi();
714+
api.unfollowPlaylist('spotify', '2ujjMpFriZ2nayLmrD1Jgl', callback);
715+
that.requests[0].respond(200);
716+
expect(that.requests[0].method).to.equal('DELETE');
717+
expect(callback.calledWith(null, '')).to.be.ok;
718+
expect(that.requests).to.have.length(1);
719+
expect(that.requests[0].url).to.equal('https://api.spotify.com/v1/users/spotify/playlists/2ujjMpFriZ2nayLmrD1Jgl/followers');
720+
});
688721
});
689722

690723
describe('Using Promises/A+ through Q.js', function() {

0 commit comments

Comments
 (0)