Skip to content

Commit

Permalink
Avoid passing device_id in body parameters (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
JMPerez authored Mar 5, 2018
1 parent 394d3f6 commit 10d44d8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
14 changes: 11 additions & 3 deletions src/spotify-web-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1083,12 +1083,20 @@ SpotifyWebApi.prototype = {
* otherwise an error. Not returned if a callback is given.
*/
play: function(options, callback) {
/*jshint camelcase: false */
var _options = options || {};
var queryParams = _options.device_id ? {device_id: _options.device_id} : null;
var postData = {};
['context_uri', 'uris', 'offset'].forEach(function(field) {
if (field in _options) {
postData[field] = _options[field];
}
});
return WebApiRequest.builder(this.getAccessToken())
.withPath('/v1/me/player/play')
/*jshint camelcase: false */
.withQueryParameters(options && options.device_id ? {device_id: options.device_id} : null)
.withQueryParameters(queryParams)
.withHeaders({ 'Content-Type' : 'application/json' })
.withBodyParameters(options)
.withBodyParameters(postData)
.build()
.execute(HttpManager.put, callback);
},
Expand Down
29 changes: 8 additions & 21 deletions test/spotify-web-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1130,25 +1130,6 @@ describe('Spotify Web API', function() {
});
});

it('should upload playlist cover image', function(done) {
sinon.stub(HttpManager, '_makeRequest', function(method, options, uri, callback) {
method.should.equal(superagent.put);
uri.should.equal('https://api.spotify.com/v1/users/thelinmichael/playlists/5ieJqeLJjjI8iJWaxeBLuK/images');
(options.data).should.eql('longbase64uri')
callback(null, { statusCode : 202 });
should.not.exist(options.query);
});

var api = new SpotifyWebApi();
api.setAccessToken('long-access-token');

api.uploadCustomPlaylistCoverImage('thelinmichael', '5ieJqeLJjjI8iJWaxeBLuK', 'longbase64uri')
.then(function(data) {
(202).should.equal(data.statusCode);
done();
});
});

it('should add tracks to playlist', function(done) {
sinon.stub(HttpManager, '_makeRequest', function(method, options, uri, callback) {
method.should.equal(superagent.post);
Expand Down Expand Up @@ -1375,12 +1356,18 @@ describe('Spotify Web API', function() {

});

it('should resume the user\'s playback with a device id', function(done) {
it('should resume the user\'s playback with options', function(done) {

sinon.stub(HttpManager, '_makeRequest', function(method, options, uri, callback) {
method.should.equal(superagent.put);
uri.should.equal('https://api.spotify.com/v1/me/player/play');
options.query.should.eql({device_id: 'my_device_id'});
JSON.parse(options.data).should.eql({
context_uri: 'my_context',
offset: {
position: 5
}
});
callback();
});

Expand All @@ -1390,7 +1377,7 @@ describe('Spotify Web API', function() {
accessToken : accessToken
});

api.play({device_id: 'my_device_id'})
api.play({device_id: 'my_device_id', context_uri: 'my_context', offset: {'position': 5}})
.then(function(data) {
done();
}, function(err) {
Expand Down

0 comments on commit 10d44d8

Please sign in to comment.