Skip to content

Commit a97dabb

Browse files
committed
Use query string to send uris and position when adding tracks
1 parent e313316 commit a97dabb

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/spotify-web-api.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,22 @@ var SpotifyWebApi = (function() {
3232
} else if (typeof options === 'function') {
3333
cb = options;
3434
}
35-
requestData.params = _extend(requestData.params, opt);
35+
36+
// options extend postData, if any. Otherwise they extend parameters sent in the url
37+
var type = requestData.type || 'GET';
38+
if (type !== 'GET' && requestData.postData) {
39+
requestData.postData = _extend(requestData.postData, opt);
40+
} else {
41+
requestData.params = _extend(requestData.params, opt);
42+
}
3643
return _performRequest(requestData, cb);
3744
};
3845

3946
var _performRequest = function(requestData, callback) {
4047
var promiseFunction = function(resolve, reject) {
4148
var req = new XMLHttpRequest();
4249
var type = requestData.type || 'GET';
43-
if (type === 'GET') {
44-
req.open(type,
45-
_buildUrl(requestData.url, requestData.params),
46-
true);
47-
} else {
48-
req.open(type, _buildUrl(requestData.url));
49-
}
50+
req.open(type, _buildUrl(requestData.url, requestData.params));
5051
if (_accessToken) {
5152
req.setRequestHeader('Authorization', 'Bearer ' + _accessToken);
5253
}
@@ -365,7 +366,9 @@ var SpotifyWebApi = (function() {
365366
var requestData = {
366367
url: _baseUri + '/users/' + userId + '/playlists/' + playlistId + '/tracks',
367368
type: 'POST',
368-
postData: uris
369+
params: {
370+
uris: uris
371+
}
369372
};
370373
return _checkParamsAndPerformRequest(requestData, options, callback);
371374
};

tests/js/spec/test.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ describe('Basic tests', function() {
153153
api.getArtistAlbums('5YyScSZOuBHpoFhGvHFedc', {limit: 2}, callback);
154154
that.requests[0].respond(200,
155155
{'Content-Type':'application/json'},
156-
JSON.stringify(that.fixtures.artist_albums)
156+
JSON.stringify(that.fixtures.artist_albums_limit_2)
157157
);
158158
expect(callback.calledWith(null, that.fixtures.artist_albums_limit_2)).to.be.ok;
159159
expect(that.requests).to.have.length(1);
@@ -450,7 +450,21 @@ describe('Basic tests', function() {
450450
);
451451
expect(callback.calledWith(null, '')).to.be.ok;
452452
expect(that.requests).to.have.length(1);
453-
expect(that.requests[0].url).to.equal('https://api.spotify.com/v1/users/jmperezperez/playlists/7Kud0O2IdWLbEGgvBkW9di/tracks');
453+
expect(that.requests[0].url).to.equal('https://api.spotify.com/v1/users/jmperezperez/playlists/7Kud0O2IdWLbEGgvBkW9di/tracks?uris=spotify%3Atrack%3A2Oehrcv4Kov0SuIgWyQY9e');
454+
});
455+
456+
it('should add tracks to a playlist, specifying position', function() {
457+
var callback = sinon.spy();
458+
var api = new SpotifyWebApi();
459+
api.setAccessToken('<example_access_token>');
460+
api.addTracksToPlaylist('jmperezperez', '7Kud0O2IdWLbEGgvBkW9di', ['spotify:track:2Oehrcv4Kov0SuIgWyQY9e'], {position: 0}, callback);
461+
that.requests[0].respond(201,
462+
{'Content-Type':'application/json'},
463+
''
464+
);
465+
expect(callback.calledWith(null, '')).to.be.ok;
466+
expect(that.requests).to.have.length(1);
467+
expect(that.requests[0].url).to.equal('https://api.spotify.com/v1/users/jmperezperez/playlists/7Kud0O2IdWLbEGgvBkW9di/tracks?uris=spotify%3Atrack%3A2Oehrcv4Kov0SuIgWyQY9e&position=0');
454468
});
455469

456470
it('should remove tracks from a playlist', function() {

0 commit comments

Comments
 (0)