Skip to content

Commit fba9ee7

Browse files
committed
Add search for playlists
1 parent a97dabb commit fba9ee7

File tree

5 files changed

+179
-2
lines changed

5 files changed

+179
-2
lines changed

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "spotify-web-api-js",
33
"description": "A client-side JS wrapper for the Spotify Web API",
4-
"version": "0.5.0",
4+
"version": "0.6.0",
55
"homepage": "https://github.com/JMPerez/spotify-web-api-js",
66
"author": {
77
"name": "José M. Pérez"

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "spotify-web-api-js",
33
"description": "A client-side JS wrapper for the Spotify Web API",
4-
"version": "0.5.0",
4+
"version": "0.6.0",
55
"homepage": "https://github.com/JMPerez/spotify-web-api-js",
66
"author": {
77
"name": "José M. Pérez"

src/spotify-web-api.js

+20
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,26 @@ var SpotifyWebApi = (function() {
739739
return _checkParamsAndPerformRequest(requestData, options, callback);
740740
};
741741

742+
/**
743+
* Fetches tracks from the Spotify catalog according to a query.
744+
* See [Search for an Item](https://developer.spotify.com/web-api/search-item/) on
745+
* the Spotify Developer site for more information about the endpoint.
746+
* @param {Object} options A JSON object with options that can be passed
747+
* @param {function(Object, Object)} callback An optional callback that receives 2 parameters. The first
748+
* one is the error object (null if no error), and the second is the value if the request succeeded.
749+
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
750+
*/
751+
Constr.prototype.searchPlaylists = function(query, options, callback) {
752+
var requestData = {
753+
url: _baseUri + '/search/',
754+
params: {
755+
q: query,
756+
type: 'playlist'
757+
}
758+
};
759+
return _checkParamsAndPerformRequest(requestData, options, callback);
760+
};
761+
742762
/**
743763
* Sets the access token to be used.
744764
* See [the Authorization Guide](https://developer.spotify.com/web-api/authorization-guide/) on

tests/fixtures/search_playlist.json

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
2+
{
3+
"playlists" : {
4+
"href" : "https://api.spotify.com/v1/search?query=music&offset=0&limit=5&type=playlist",
5+
"items" : [ {
6+
"collaborative" : false,
7+
"external_urls" : {
8+
"spotify" : "http://open.spotify.com/user/chillls/playlist/6OUo9M97qj4lmSTbnkNm35"
9+
},
10+
"href" : "https://api.spotify.com/v1/users/chillls/playlists/6OUo9M97qj4lmSTbnkNm35",
11+
"id" : "6OUo9M97qj4lmSTbnkNm35",
12+
"images" : [ ],
13+
"name" : "Christmas Music",
14+
"owner" : {
15+
"external_urls" : {
16+
"spotify" : "http://open.spotify.com/user/chillls"
17+
},
18+
"href" : "https://api.spotify.com/v1/users/chillls",
19+
"id" : "chillls",
20+
"type" : "user",
21+
"uri" : "spotify:user:chillls"
22+
},
23+
"public" : null,
24+
"tracks" : {
25+
"href" : "https://api.spotify.com/v1/users/chillls/playlists/6OUo9M97qj4lmSTbnkNm35/tracks",
26+
"total" : 102
27+
},
28+
"type" : "playlist",
29+
"uri" : "spotify:user:chillls:playlist:6OUo9M97qj4lmSTbnkNm35"
30+
}, {
31+
"collaborative" : false,
32+
"external_urls" : {
33+
"spotify" : "http://open.spotify.com/user/igreene21/playlist/5iQ0qBTxtuSZYsVDsBYxHQ"
34+
},
35+
"href" : "https://api.spotify.com/v1/users/igreene21/playlists/5iQ0qBTxtuSZYsVDsBYxHQ",
36+
"id" : "5iQ0qBTxtuSZYsVDsBYxHQ",
37+
"images" : [ ],
38+
"name" : "Pre-Game Music",
39+
"owner" : {
40+
"external_urls" : {
41+
"spotify" : "http://open.spotify.com/user/igreene21"
42+
},
43+
"href" : "https://api.spotify.com/v1/users/igreene21",
44+
"id" : "igreene21",
45+
"type" : "user",
46+
"uri" : "spotify:user:igreene21"
47+
},
48+
"public" : null,
49+
"tracks" : {
50+
"href" : "https://api.spotify.com/v1/users/igreene21/playlists/5iQ0qBTxtuSZYsVDsBYxHQ/tracks",
51+
"total" : 270
52+
},
53+
"type" : "playlist",
54+
"uri" : "spotify:user:igreene21:playlist:5iQ0qBTxtuSZYsVDsBYxHQ"
55+
}, {
56+
"collaborative" : false,
57+
"external_urls" : {
58+
"spotify" : "http://open.spotify.com/user/spotify/playlist/5FJXhjdILmRA2z5bvz4nzf"
59+
},
60+
"href" : "https://api.spotify.com/v1/users/spotify/playlists/5FJXhjdILmRA2z5bvz4nzf",
61+
"id" : "5FJXhjdILmRA2z5bvz4nzf",
62+
"images" : [ {
63+
"url" : "https://i.scdn.co/image/4669088eb741254479f9eb340bf1f93165b93288"
64+
} ],
65+
"name" : "Today's Top Hits",
66+
"owner" : {
67+
"external_urls" : {
68+
"spotify" : "http://open.spotify.com/user/spotify"
69+
},
70+
"href" : "https://api.spotify.com/v1/users/spotify",
71+
"id" : "spotify",
72+
"type" : "user",
73+
"uri" : "spotify:user:spotify"
74+
},
75+
"public" : null,
76+
"tracks" : {
77+
"href" : "https://api.spotify.com/v1/users/spotify/playlists/5FJXhjdILmRA2z5bvz4nzf/tracks",
78+
"total" : 51
79+
},
80+
"type" : "playlist",
81+
"uri" : "spotify:user:spotify:playlist:5FJXhjdILmRA2z5bvz4nzf"
82+
}, {
83+
"collaborative" : false,
84+
"external_urls" : {
85+
"spotify" : "http://open.spotify.com/user/mcs1992/playlist/4HiLVQvLMFLOZBiDGThuOX"
86+
},
87+
"href" : "https://api.spotify.com/v1/users/mcs1992/playlists/4HiLVQvLMFLOZBiDGThuOX",
88+
"id" : "4HiLVQvLMFLOZBiDGThuOX",
89+
"images" : [ {
90+
"url" : "https://i.scdn.co/image/d7c96c229c76af84b031cd1ed6ef41dd8c5bf3ff"
91+
} ],
92+
"name" : "Relaxed Driving Music - last update: 17-11-'14 (acoustic, vocalist, laid back, chilled out, road trip, unplugged, easy listening, cover, singer-songwriter)",
93+
"owner" : {
94+
"external_urls" : {
95+
"spotify" : "http://open.spotify.com/user/mcs1992"
96+
},
97+
"href" : "https://api.spotify.com/v1/users/mcs1992",
98+
"id" : "mcs1992",
99+
"type" : "user",
100+
"uri" : "spotify:user:mcs1992"
101+
},
102+
"public" : null,
103+
"tracks" : {
104+
"href" : "https://api.spotify.com/v1/users/mcs1992/playlists/4HiLVQvLMFLOZBiDGThuOX/tracks",
105+
"total" : 219
106+
},
107+
"type" : "playlist",
108+
"uri" : "spotify:user:mcs1992:playlist:4HiLVQvLMFLOZBiDGThuOX"
109+
}, {
110+
"collaborative" : false,
111+
"external_urls" : {
112+
"spotify" : "http://open.spotify.com/user/spotify/playlist/65y98W0UItf73DJKVgylTP"
113+
},
114+
"href" : "https://api.spotify.com/v1/users/spotify/playlists/65y98W0UItf73DJKVgylTP",
115+
"id" : "65y98W0UItf73DJKVgylTP",
116+
"images" : [ {
117+
"url" : "https://i.scdn.co/image/223eaea3d44938b833a86651267db608bea1c5a3"
118+
} ],
119+
"name" : "ESM | Electronic Study Music",
120+
"owner" : {
121+
"external_urls" : {
122+
"spotify" : "http://open.spotify.com/user/spotify"
123+
},
124+
"href" : "https://api.spotify.com/v1/users/spotify",
125+
"id" : "spotify",
126+
"type" : "user",
127+
"uri" : "spotify:user:spotify"
128+
},
129+
"public" : null,
130+
"tracks" : {
131+
"href" : "https://api.spotify.com/v1/users/spotify/playlists/65y98W0UItf73DJKVgylTP/tracks",
132+
"total" : 53
133+
},
134+
"type" : "playlist",
135+
"uri" : "spotify:user:spotify:playlist:65y98W0UItf73DJKVgylTP"
136+
} ],
137+
"limit" : 5,
138+
"next" : "https://api.spotify.com/v1/search?query=music&offset=5&limit=5&type=playlist",
139+
"offset" : 0,
140+
"previous" : null,
141+
"total" : 106349
142+
}
143+
}

tests/js/spec/test.js

+14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ describe('Basic tests', function() {
3030
search_album: loadFixture('search_album'),
3131
search_artist: loadFixture('search_artist'),
3232
search_track: loadFixture('search_track'),
33+
search_playlist: loadFixture('search_playlist'),
3334
user: loadFixture('user'),
3435
me: loadFixture('me'),
3536
user_playlists: loadFixture('user_playlists'),
@@ -238,6 +239,19 @@ describe('Basic tests', function() {
238239
expect(that.requests[0].url).to.equal('https://api.spotify.com/v1/search/?q=Mr.%20Brightside&type=track');
239240
});
240241

242+
it('should search for playlists', function() {
243+
var callback = sinon.spy();
244+
var api = new SpotifyWebApi();
245+
api.searchPlaylists('music', {offset: 0, limit: 5}, callback);
246+
that.requests[0].respond(200,
247+
{'Content-Type':'application/json'},
248+
JSON.stringify(that.fixtures.search_playlist)
249+
);
250+
expect(callback.calledWith(null, that.fixtures.search_playlist)).to.be.ok;
251+
expect(that.requests).to.have.length(1);
252+
expect(that.requests[0].url).to.equal('https://api.spotify.com/v1/search/?q=music&type=playlist&offset=0&limit=5');
253+
});
254+
241255
it('should get a track using a token', function() {
242256
var callback = sinon.spy();
243257
var api = new SpotifyWebApi();

0 commit comments

Comments
 (0)