Skip to content

Commit

Permalink
Added method for uploading custom playlist cover image (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
DalerAsrorov authored and JMPerez committed Mar 5, 2018
1 parent 9a2c111 commit 02764fa
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,14 @@ spotifyApi.changePlaylistDetails('thelinmichael', '5ieJqeLJjjI8iJWaxeBLuK',
console.log('Something went wrong!', err);
});

// Upload a custom playlist cover image
spotifyApi.uploadCustomPlaylistCoverImage('thelinmichael', '5ieJqeLJjjI8iJWaxeBLuK','longbase64uri')
.then(function(data) {
console.log('Playlsit cover image uploaded!');
}, function(err) {
console.log('Something went wrong!', err);
});

// Follow a playlist (privately)
spotifyApi.followPlaylist('thelinmichael', '5ieJqeLJjjI8iJWaxeBLuK',
{
Expand Down
19 changes: 19 additions & 0 deletions src/spotify-web-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,25 @@ SpotifyWebApi.prototype = {
.execute(HttpManager.put, callback);
},

/**
* Replace the image used to represent a specific playlist.
* @param {string} userId The playlist's owner's user ID
* @param {string} playlistId The playlist's ID
* @param {string} base64URI Base64 encoded JPEG image data, maximum payload size is 256 KB
* @param {requestCallback} [callback] Optional callback method to be called instead of the promise.
* @example uploadCustomPlaylistCoverImage('thelinmichael', '3EsfV6XzCHU8SPNdbnFogK', 'longbase64uri').then(...)
* @returns {Promise|undefined} A promise that if successful, simply resolves to an empty object. If rejected,
* it contains an error object. Not returned if a callback is given.
*/
uploadCustomPlaylistCoverImage: function(userId, playlistId, base64URI, callback) {
return WebApiRequest.builder(this.getAccessToken())
.withPath('/v1/users/' + encodeURIComponent(userId) + '/playlists/' + playlistId + '/images')
.withHeaders({ 'Content-Type' : 'image/jpeg' })
.withBodyParameters(base64URI)
.build()
.execute(HttpManager.put, callback);
},

/**
* Add tracks to a playlist.
* @param {string} userId The playlist's owner's user ID
Expand Down
19 changes: 19 additions & 0 deletions test/spotify-web-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,25 @@ 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

0 comments on commit 02764fa

Please sign in to comment.