Skip to content

Commit 5566b6e

Browse files
author
AJ Hsu
committed
Fill up setPlaylistID, fetchMetadata and fetchTracks method for ChartFetcher
1 parent 78d80b7 commit 5566b6e

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

src/api/ChartFetcher.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ export default class ChartFetcher extends Fetcher {
1111
*/
1212
constructor(http, territory = 'TW') {
1313
super(http, territory = 'TW')
14+
15+
/**
16+
* @ignore
17+
*/
18+
this.playlist_id = undefined
1419
}
1520

1621
/**
@@ -25,4 +30,44 @@ export default class ChartFetcher extends Fetcher {
2530
territory: this.territory
2631
})
2732
}
33+
34+
/**
35+
* Init the chart fetcher.
36+
*
37+
* @param {string} playlist_id - The playlist ID.
38+
* @return {ChartFetcher}
39+
* @see https://docs-en.kkbox.codes/v1.1/reference#charts-playlist_id
40+
*/
41+
setPlaylistID(playlist_id) {
42+
this.playlist_id = playlist_id
43+
return this
44+
}
45+
46+
/**
47+
* Fetch playlist of the chart you set.
48+
*
49+
* @return {Promise}
50+
* @example api.chartFetcher.setPlaylistID('4mJSYXvueA8t0odsny').fetchMetadata()
51+
* @see https://docs-en.kkbox.codes/v1.1/reference#charts-playlist_id
52+
*/
53+
fetchMetadata() {
54+
return this.http.get(ENDPOINT + this.playlist_id, {territory: this.territory})
55+
}
56+
57+
/**
58+
* Fetch tracks of the playlist with the chart fetcher you init. Result will be paged.
59+
*
60+
* @param {number} [limit] - The size of one page.
61+
* @param {number} [offset] - The offset index for first element.
62+
* @return {Promise}
63+
* @example api.chartFetcher.setPlaylistID('4mJSYXvueA8t0odsny').fetchTracks()
64+
* @see https://docs-en.kkbox.codes/v1.1/reference#charts-playlist_id-tracks
65+
*/
66+
fetchTracks(limit = undefined, offset = undefined) {
67+
return this.http.get(ENDPOINT + this.playlist_id + '/tracks', {
68+
territory: this.territory,
69+
limit: limit,
70+
offset: offset
71+
})
72+
}
2873
}

test/apitest.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,20 @@ describe('Api Begin to Test', () => {
358358
})
359359
})
360360
})
361+
362+
describe('#fetchMetadata()', () => {
363+
it('should succeed', (done) => {
364+
chartFetcher.setPlaylistID('4mJSYXvueA8t0odsny').fetchMetadata()
365+
.then(response => response.status.should.be.exactly(200), reject => should.not.exists(reject))
366+
})
367+
})
368+
369+
describe('#fetchTracks()', () => {
370+
it('should succeed', (done) => {
371+
chartFetcher.setPlaylistID('4mJSYXvueA8t0odsny').fetchTracks()
372+
.then(response => response.status.should.be.exactly(200), reject => should.not.exists(reject))
373+
})
374+
})
361375
})
362376

363377
describe('New Release Category', () => {

0 commit comments

Comments
 (0)