2016-12-20 05:21:07 +00:00
|
|
|
import qs from 'qs'
|
|
|
|
|
import Client, { ClientError } from './client'
|
|
|
|
|
|
|
|
|
|
export class MusicBrainzError extends ClientError {}
|
|
|
|
|
|
|
|
|
|
export default class MusicBrainz extends Client {
|
2017-11-07 05:54:56 +00:00
|
|
|
constructor(
|
|
|
|
|
{
|
|
|
|
|
baseURL = process.env.MUSICBRAINZ_BASE_URL ||
|
|
|
|
|
'http://musicbrainz.org/ws/2/',
|
|
|
|
|
errorClass = MusicBrainzError,
|
|
|
|
|
// MusicBrainz API requests are limited to an *average* of 1 req/sec.
|
|
|
|
|
// That means if, for example, we only need to make a few API requests to
|
|
|
|
|
// fulfill a query, we might as well make them all at once - as long as
|
|
|
|
|
// we then wait a few seconds before making more. In practice this can
|
|
|
|
|
// seemingly be set to about 5 requests every 5 seconds before we're
|
|
|
|
|
// considered to exceed the rate limit.
|
|
|
|
|
limit = 5,
|
|
|
|
|
period = 5500,
|
|
|
|
|
...options
|
|
|
|
|
} = {}
|
|
|
|
|
) {
|
2016-12-20 05:21:07 +00:00
|
|
|
super({ baseURL, errorClass, limit, period, ...options })
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 05:54:56 +00:00
|
|
|
parseErrorMessage(response, body) {
|
2016-12-20 05:21:07 +00:00
|
|
|
if (body && body.error) {
|
|
|
|
|
return body.error
|
|
|
|
|
}
|
|
|
|
|
return super.parseErrorMessage(response, body)
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 05:54:56 +00:00
|
|
|
stringifyParams(params) {
|
2016-12-20 05:21:07 +00:00
|
|
|
if (Array.isArray(params.inc)) {
|
|
|
|
|
params = {
|
|
|
|
|
...params,
|
|
|
|
|
inc: params.inc.join('+')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (Array.isArray(params.type)) {
|
|
|
|
|
params = {
|
|
|
|
|
...params,
|
|
|
|
|
type: params.type.join('|')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (Array.isArray(params.status)) {
|
|
|
|
|
params = {
|
|
|
|
|
...params,
|
|
|
|
|
status: params.status.join('|')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return qs.stringify(params, {
|
|
|
|
|
skipNulls: true,
|
2017-11-07 05:54:56 +00:00
|
|
|
filter: (key, value) => (value === '' ? undefined : value)
|
2016-12-20 05:21:07 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 05:54:56 +00:00
|
|
|
getURL(path, params) {
|
2016-12-20 05:21:07 +00:00
|
|
|
const query = params ? this.stringifyParams(params) : ''
|
|
|
|
|
return query ? `${path}?${query}` : path
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 05:54:56 +00:00
|
|
|
getLookupURL(entity, id, params) {
|
2016-12-20 05:21:07 +00:00
|
|
|
if (id == null) {
|
|
|
|
|
return this.getBrowseURL(entity, params)
|
|
|
|
|
}
|
|
|
|
|
return this.getURL(`${entity}/${id}`, params)
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 05:54:56 +00:00
|
|
|
lookup(entity, id, params = {}) {
|
2016-12-20 05:21:07 +00:00
|
|
|
const url = this.getLookupURL(entity, id, params)
|
|
|
|
|
return this.get(url, { json: true, qs: { fmt: 'json' } })
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 05:54:56 +00:00
|
|
|
getBrowseURL(entity, params) {
|
2016-12-20 05:21:07 +00:00
|
|
|
return this.getURL(entity, params)
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 05:54:56 +00:00
|
|
|
browse(entity, params = {}) {
|
2016-12-20 05:21:07 +00:00
|
|
|
const url = this.getBrowseURL(entity, params)
|
|
|
|
|
return this.get(url, { json: true, qs: { fmt: 'json' } })
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 05:54:56 +00:00
|
|
|
getSearchURL(entity, query, params) {
|
2016-12-20 05:21:07 +00:00
|
|
|
return this.getURL(entity, { ...params, query })
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 05:54:56 +00:00
|
|
|
search(entity, query, params = {}) {
|
2016-12-20 05:21:07 +00:00
|
|
|
const url = this.getSearchURL(entity, query, params)
|
|
|
|
|
return this.get(url, { json: true, qs: { fmt: 'json' } })
|
|
|
|
|
}
|
|
|
|
|
}
|