Skip to content

Commit

Permalink
Adds Connect endpoints (#121)
Browse files Browse the repository at this point in the history
* Adds endpoints to get payback state, list of devices and transfer playback
  • Loading branch information
philnash authored and JMPerez committed May 2, 2017
1 parent 49fc626 commit 760d2fb
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
87 changes: 87 additions & 0 deletions src/spotify-web-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,93 @@ SpotifyWebApi.prototype = {
}
},

/**
* Get the Current User's Connect Devices
* @param {requestCallback} [callback] Optional callback method to be called instead of the promise.
* @returns {Promise|undefined} A promise that if successful, resolves into a paging object of tracks,
* otherwise an error. Not returned if a callback is given.
*/
getMyDevices: function(callback) {
var request = WebApiRequest.builder()
.withPath('/v1/me/player/devices')
.build();

this._addAccessToken(request, this.getAccessToken());

var promise = this._performRequest(HttpManager.get, request);

if (callback) {
promise.then(function(data) {
callback(null, data);
}, function(err) {
callback(err);
});
} else {
return promise;
}
},

/**
* Get the Current User's Current Playback State
* @param {Object} [options] Options, being market.
* @param {requestCallback} [callback] Optional callback method to be called instead of the promise.
* @returns {Promise|undefined} A promise that if successful, resolves into a paging object of tracks,
* otherwise an error. Not returned if a callback is given.
*/
getMyCurrentPlaybackState: function(options, callback) {
var request = WebApiRequest.builder()
.withPath('/v1/me/player')
.build();

this._addAccessToken(request, this.getAccessToken());
this._addQueryParameters(request, options);

var promise = this._performRequest(HttpManager.get, request);

if (callback) {
promise.then(function(data) {
callback(null, data);
}, function(err) {
callback(err);
});
} else {
return promise;
}
},

/**
* Transfer a User's Playback
* @param {Object} [options] Options, being market.
* @param {requestCallback} [callback] Optional callback method to be called instead of the promise.
* @returns {Promise|undefined} A promise that if successful, resolves into a paging object of tracks,
* otherwise an error. Not returned if a callback is given.
*/
transferMyPlayback: function(options, callback) {
var request = WebApiRequest.builder()
.withPath('/v1/me/player')
.withHeaders({ 'Content-Type' : 'application/json' })
.withBodyParameters({
'device_ids': options.deviceIds,
'play': options.play || false
})
.build();

this._addAccessToken(request, this.getAccessToken());
this._addBodyParameters(request, options);

var promise = this._performRequest(HttpManager.put, request);

if (callback) {
promise.then(function(data) {
callback(null, data);
}, function(err) {
callback(err);
});
} else {
return promise;
}
},

/**
* Add the current user as a follower of one or more other Spotify users.
* @param {string[]} userIds The IDs of the users to be followed.
Expand Down
82 changes: 82 additions & 0 deletions test/spotify-web-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,88 @@ describe('Spotify Web API', function() {
});
});

it("should get user's devices:", function(done) {
sinon.stub(HttpManager, '_makeRequest', function(method, options, uri, callback) {
method.should.equal(superagent.get);
uri.should.equal('https://api.spotify.com/v1/me/player/devices');
options.headers.should.eql({Authorization: 'Bearer someAccessToken'});
callback(null, {
body : {
devices: [ ]
}
});
});

var api = new SpotifyWebApi({
accessToken : 'someAccessToken'
});

api.getMyDevices()
.then(function(data) {
should.exist(data.body.devices);
done();
});
});

it("should get user's current playback status:", function(done) {
sinon.stub(HttpManager, '_makeRequest', function(method, options, uri, callback) {
method.should.equal(superagent.get);
uri.should.equal('https://api.spotify.com/v1/me/player');
options.query.should.eql({
market : "GB"
});
options.headers.should.eql({Authorization: 'Bearer someAccessToken'});
callback(null, {
body : {
device: { }
}
});
});

var api = new SpotifyWebApi({
accessToken : 'someAccessToken'
});

api.getMyCurrentPlaybackState({ market : "GB"})
.then(function(data) {
should.exist(data.body.device);
done();
});
});

it('should transfer the user\'s playback', 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');
JSON.parse(options.data).should.eql({
'device_ids': ['deviceId'],
'play': true,
'deviceIds' : ['deviceId']
});
should.not.exist(options.query);
callback();
});

var accessToken = 'myAccessToken';

var api = new SpotifyWebApi({
accessToken : accessToken
});

api.transferMyPlayback({
deviceIds: ['deviceId'],
play: true
})
.then(function(data) {
done();
}, function(err) {
console.log(err);
done(err);
});

});

it.skip("should retrieve an access token using the client credentials flow", function(done) {
var clientId = 'someClientId',
clientSecret = 'someClientSecret';
Expand Down

0 comments on commit 760d2fb

Please sign in to comment.