Skip to content

Commit a95f7f5

Browse files
'createPlaylist' request changed: Path and arguments (#281)
* New request path to createPlaylist * Update README.md * Fixed create playlist request * Update package.json * Revert "Update package.json" This reverts commit 5718272. * Add tests and make some changes Co-authored-by: thelinmichael <[email protected]>
1 parent f503602 commit a95f7f5

File tree

4 files changed

+159
-34
lines changed

4 files changed

+159
-34
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ spotifyApi.getUserPlaylists('thelinmichael')
418418
});
419419

420420
// Create a private playlist
421-
spotifyApi.createPlaylist('thelinmichael', 'My Cool Playlist', { 'public' : false })
421+
spotifyApi.createPlaylist('My playlist', { 'description': 'My description', 'public': true })
422422
.then(function(data) {
423423
console.log('Created playlist!');
424424
}, function(err) {

__tests__/spotify-web-api.js

+38-13
Original file line numberDiff line numberDiff line change
@@ -1512,14 +1512,36 @@ describe('Spotify Web API', () => {
15121512
});
15131513
});
15141514

1515-
test.skip('should create a playlist', function(done) {
1515+
test('should create a playlist', function(done) {
1516+
sinon.stub(HttpManager, '_makeRequest', function(
1517+
method,
1518+
options,
1519+
uri,
1520+
callback
1521+
) {
1522+
expect(method).toBe(superagent.post);
1523+
expect(uri).toBe(
1524+
'https://api.spotify.com/v1/me/playlists'
1525+
);
1526+
expect(JSON.parse(options.data)).toEqual({
1527+
name: 'My Cool Playlist'
1528+
});
1529+
expect(options.query).toBeFalsy();
1530+
callback(null, {
1531+
body: { name: 'My Cool Playlist' },
1532+
statusCode: 200
1533+
});
1534+
});
1535+
15161536
var api = new SpotifyWebApi();
15171537
api.setAccessToken('long-access-token');
15181538

15191539
api
1520-
.createPlaylist('thelinmichael', 'My Cool Playlist', { public: true })
1540+
.createPlaylist('My Cool Playlist')
15211541
.then(
15221542
function(data) {
1543+
expect(data.body.name).toBe('My Cool Playlist');
1544+
expect(data.statusCode).toBe(200);
15231545
done();
15241546
},
15251547
function(err) {
@@ -1538,55 +1560,58 @@ describe('Spotify Web API', () => {
15381560
) {
15391561
expect(method).toBe(superagent.post);
15401562
expect(uri).toBe(
1541-
'https://api.spotify.com/v1/users/thelinmichael/playlists'
1563+
'https://api.spotify.com/v1/me/playlists'
15421564
);
15431565
expect(JSON.parse(options.data)).toEqual({
15441566
name: 'My Cool Playlist',
1567+
description: 'It\'s really cool',
15451568
public: false
15461569
});
15471570
expect(options.query).toBeFalsy();
15481571
callback(null, {
1549-
body: { name: 'My Cool Playlist', public: false },
1572+
body: { name: 'My Cool Playlist', description: 'It\s really cool', public: false },
15501573
statusCode: 200
15511574
});
15521575
});
15531576

15541577
var api = new SpotifyWebApi();
15551578

15561579
api.createPlaylist(
1557-
'thelinmichael',
15581580
'My Cool Playlist',
1581+
{ description: 'It\'s really cool' },
15591582
{ public: false },
15601583
function(err, data) {
15611584
expect(data.body.name).toBe('My Cool Playlist');
1585+
expect(data.body.description).toBe('It\s really cool');
1586+
expect(data.body.public).toBe(false);
15621587
expect(data.statusCode).toBe(200);
15631588
done();
15641589
}
15651590
);
15661591
});
15671592

1568-
test('should create a playlist using callback without options', done => {
1569-
sinon.stub(HttpManager, '_makeRequest').callsFake(function(
1593+
test('should create a playlist using callback with user id', done => {
1594+
sinon.stub(HttpManager, '_makeRequest', function(
15701595
method,
15711596
options,
15721597
uri,
15731598
callback
15741599
) {
15751600
expect(method).toBe(superagent.post);
15761601
expect(uri).toBe(
1577-
'https://api.spotify.com/v1/users/thelinmichael/playlists'
1602+
'https://api.spotify.com/v1/me/playlists'
15781603
);
15791604
expect(JSON.parse(options.data)).toEqual({ name: 'My Cool Playlist' });
1580-
callback(null, { body: { name: 'My Cool Playlist' } });
1605+
callback(null, { body: { name: 'My Cool Playlist' }, statusCode: 200 });
15811606
expect(options.query).toBeFalsy();
15821607
});
15831608

15841609
var api = new SpotifyWebApi();
15851610

1586-
api.createPlaylist('thelinmichael', 'My Cool Playlist', function(
1587-
err,
1588-
data
1589-
) {
1611+
api.createPlaylist('thelinmichael', 'My Cool Playlist', {},
1612+
function(err, data) {
1613+
expect(data.body.name).toBe('My Cool Playlist');
1614+
expect(data.statusCode).toBe(200);
15901615
done();
15911616
});
15921617
});

package-lock.json

+109
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/spotify-web-api.js

+11-20
Original file line numberDiff line numberDiff line change
@@ -508,34 +508,25 @@ SpotifyWebApi.prototype = {
508508

509509
/**
510510
* Create a playlist.
511-
* @param {string} userId The playlist's owner's user ID.
512-
* @param {string} playlistName The name of the playlist.
513-
* @param {Object} [options] The possible options, currently only public.
511+
* @param {string} [name] The name of the playlist.
512+
* @param {Object} [options] The possible options, being description, collaborative and public.
514513
* @param {requestCallback} [callback] Optional callback method to be called instead of the promise.
515-
* @example createPlaylist('thelinmichael', 'My cool playlist!', { public : false }).then(...)
514+
* @example createPlaylist('My playlist', {''description': 'My description', 'collaborative' : false, 'public': true}).then(...)
516515
* @returns {Promise|undefined} A promise that if successful, resolves to an object containing information about the
517516
* created playlist. If rejected, it contains an error object. Not returned if a callback is given.
518517
*/
519-
createPlaylist: function(userId, playlistName, options, callback) {
520-
// In case someone is using a version where options parameter did not exist.
521-
var actualCallback;
522-
if (typeof options === 'function' && !callback) {
523-
actualCallback = options;
524-
} else {
525-
actualCallback = callback;
526-
}
527-
528-
var actualOptions = { name: playlistName };
529-
if (typeof options === 'object') {
530-
Object.keys(options).forEach(function(key) {
531-
actualOptions[key] = options[key];
532-
});
518+
createPlaylist: function(name, options, callback) {
519+
// In case someone is using a version where user id was required
520+
if (typeof options === 'string') {
521+
options = callback;
522+
callback = arguments[3];
533523
}
524+
options.name = name;
534525

535526
return WebApiRequest.builder(this.getAccessToken())
536-
.withPath('/v1/users/' + encodeURIComponent(userId) + '/playlists')
527+
.withPath('/v1/me/playlists')
537528
.withHeaders({ 'Content-Type': 'application/json' })
538-
.withBodyParameters(actualOptions)
529+
.withBodyParameters(options)
539530
.build()
540531
.execute(HttpManager.post, actualCallback);
541532
},

0 commit comments

Comments
 (0)