From 760d2fb85d92009d2fe5112df1b47aa056cb75d7 Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Tue, 2 May 2017 21:38:53 +0100 Subject: [PATCH] Adds Connect endpoints (#121) * Adds endpoints to get payback state, list of devices and transfer playback --- src/spotify-web-api.js | 87 +++++++++++++++++++++++++++++++++++++++++ test/spotify-web-api.js | 82 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+) diff --git a/src/spotify-web-api.js b/src/spotify-web-api.js index fca3bc10..657f13ca 100644 --- a/src/spotify-web-api.js +++ b/src/spotify-web-api.js @@ -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. diff --git a/test/spotify-web-api.js b/test/spotify-web-api.js index 8678ba6c..17c2f58e 100644 --- a/test/spotify-web-api.js +++ b/test/spotify-web-api.js @@ -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';