Skip to content

Commit 760d2fb

Browse files
philnashJMPerez
authored andcommitted
Adds Connect endpoints (#121)
* Adds endpoints to get payback state, list of devices and transfer playback
1 parent 49fc626 commit 760d2fb

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed

src/spotify-web-api.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,93 @@ SpotifyWebApi.prototype = {
15851585
}
15861586
},
15871587

1588+
/**
1589+
* Get the Current User's Connect Devices
1590+
* @param {requestCallback} [callback] Optional callback method to be called instead of the promise.
1591+
* @returns {Promise|undefined} A promise that if successful, resolves into a paging object of tracks,
1592+
* otherwise an error. Not returned if a callback is given.
1593+
*/
1594+
getMyDevices: function(callback) {
1595+
var request = WebApiRequest.builder()
1596+
.withPath('/v1/me/player/devices')
1597+
.build();
1598+
1599+
this._addAccessToken(request, this.getAccessToken());
1600+
1601+
var promise = this._performRequest(HttpManager.get, request);
1602+
1603+
if (callback) {
1604+
promise.then(function(data) {
1605+
callback(null, data);
1606+
}, function(err) {
1607+
callback(err);
1608+
});
1609+
} else {
1610+
return promise;
1611+
}
1612+
},
1613+
1614+
/**
1615+
* Get the Current User's Current Playback State
1616+
* @param {Object} [options] Options, being market.
1617+
* @param {requestCallback} [callback] Optional callback method to be called instead of the promise.
1618+
* @returns {Promise|undefined} A promise that if successful, resolves into a paging object of tracks,
1619+
* otherwise an error. Not returned if a callback is given.
1620+
*/
1621+
getMyCurrentPlaybackState: function(options, callback) {
1622+
var request = WebApiRequest.builder()
1623+
.withPath('/v1/me/player')
1624+
.build();
1625+
1626+
this._addAccessToken(request, this.getAccessToken());
1627+
this._addQueryParameters(request, options);
1628+
1629+
var promise = this._performRequest(HttpManager.get, request);
1630+
1631+
if (callback) {
1632+
promise.then(function(data) {
1633+
callback(null, data);
1634+
}, function(err) {
1635+
callback(err);
1636+
});
1637+
} else {
1638+
return promise;
1639+
}
1640+
},
1641+
1642+
/**
1643+
* Transfer a User's Playback
1644+
* @param {Object} [options] Options, being market.
1645+
* @param {requestCallback} [callback] Optional callback method to be called instead of the promise.
1646+
* @returns {Promise|undefined} A promise that if successful, resolves into a paging object of tracks,
1647+
* otherwise an error. Not returned if a callback is given.
1648+
*/
1649+
transferMyPlayback: function(options, callback) {
1650+
var request = WebApiRequest.builder()
1651+
.withPath('/v1/me/player')
1652+
.withHeaders({ 'Content-Type' : 'application/json' })
1653+
.withBodyParameters({
1654+
'device_ids': options.deviceIds,
1655+
'play': options.play || false
1656+
})
1657+
.build();
1658+
1659+
this._addAccessToken(request, this.getAccessToken());
1660+
this._addBodyParameters(request, options);
1661+
1662+
var promise = this._performRequest(HttpManager.put, request);
1663+
1664+
if (callback) {
1665+
promise.then(function(data) {
1666+
callback(null, data);
1667+
}, function(err) {
1668+
callback(err);
1669+
});
1670+
} else {
1671+
return promise;
1672+
}
1673+
},
1674+
15881675
/**
15891676
* Add the current user as a follower of one or more other Spotify users.
15901677
* @param {string[]} userIds The IDs of the users to be followed.

test/spotify-web-api.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,88 @@ describe('Spotify Web API', function() {
12501250
});
12511251
});
12521252

1253+
it("should get user's devices:", function(done) {
1254+
sinon.stub(HttpManager, '_makeRequest', function(method, options, uri, callback) {
1255+
method.should.equal(superagent.get);
1256+
uri.should.equal('https://api.spotify.com/v1/me/player/devices');
1257+
options.headers.should.eql({Authorization: 'Bearer someAccessToken'});
1258+
callback(null, {
1259+
body : {
1260+
devices: [ ]
1261+
}
1262+
});
1263+
});
1264+
1265+
var api = new SpotifyWebApi({
1266+
accessToken : 'someAccessToken'
1267+
});
1268+
1269+
api.getMyDevices()
1270+
.then(function(data) {
1271+
should.exist(data.body.devices);
1272+
done();
1273+
});
1274+
});
1275+
1276+
it("should get user's current playback status:", function(done) {
1277+
sinon.stub(HttpManager, '_makeRequest', function(method, options, uri, callback) {
1278+
method.should.equal(superagent.get);
1279+
uri.should.equal('https://api.spotify.com/v1/me/player');
1280+
options.query.should.eql({
1281+
market : "GB"
1282+
});
1283+
options.headers.should.eql({Authorization: 'Bearer someAccessToken'});
1284+
callback(null, {
1285+
body : {
1286+
device: { }
1287+
}
1288+
});
1289+
});
1290+
1291+
var api = new SpotifyWebApi({
1292+
accessToken : 'someAccessToken'
1293+
});
1294+
1295+
api.getMyCurrentPlaybackState({ market : "GB"})
1296+
.then(function(data) {
1297+
should.exist(data.body.device);
1298+
done();
1299+
});
1300+
});
1301+
1302+
it('should transfer the user\'s playback', function(done) {
1303+
1304+
sinon.stub(HttpManager, '_makeRequest', function(method, options, uri, callback) {
1305+
method.should.equal(superagent.put);
1306+
uri.should.equal('https://api.spotify.com/v1/me/player');
1307+
JSON.parse(options.data).should.eql({
1308+
'device_ids': ['deviceId'],
1309+
'play': true,
1310+
'deviceIds' : ['deviceId']
1311+
});
1312+
should.not.exist(options.query);
1313+
callback();
1314+
});
1315+
1316+
var accessToken = 'myAccessToken';
1317+
1318+
var api = new SpotifyWebApi({
1319+
accessToken : accessToken
1320+
});
1321+
1322+
api.transferMyPlayback({
1323+
deviceIds: ['deviceId'],
1324+
play: true
1325+
})
1326+
.then(function(data) {
1327+
done();
1328+
}, function(err) {
1329+
console.log(err);
1330+
done(err);
1331+
});
1332+
1333+
});
1334+
12531335
it.skip("should retrieve an access token using the client credentials flow", function(done) {
12541336
var clientId = 'someClientId',
12551337
clientSecret = 'someClientSecret';

0 commit comments

Comments
 (0)