graphbrainz/src/extensions/cover-art-archive/client.js
Brian Beck 898ec78a6f Add a schema extension API and several extensions (#42)
* Add a schema extension API and several extensions
* Update graphql-markdown to use new diffSchema function
* Update Node support
2017-10-19 01:00:21 -07:00

37 lines
1.1 KiB
JavaScript

import Client from '../../api/client'
export default class CoverArtArchiveClient extends Client {
constructor ({
baseURL = process.env.COVER_ART_ARCHIVE_BASE_URL || 'http://coverartarchive.org/',
limit = 10,
period = 1000,
...options
} = {}) {
super({ baseURL, limit, period, ...options })
}
/**
* Sinfully attempt to parse HTML responses for the error message.
*/
parseErrorMessage (response, body) {
if (typeof body === 'string' && body.startsWith('<!')) {
const heading = /<h1>([^<]+)<\/h1>/i.exec(body)
const message = /<p>([^<]+)<\/p>/i.exec(body)
return `${heading ? heading[1] + ': ' : ''}${message ? message[1] : ''}`
}
return super.parseErrorMessage(response, body)
}
images (entityType, mbid) {
return this.get(`${entityType}/${mbid}`, { json: true })
}
imageURL (entityType, mbid, typeOrID = 'front', size) {
let url = `${entityType}/${mbid}/${typeOrID}`
if (size != null) {
url += `-${size}`
}
return this.get(url, { method: 'HEAD', followRedirect: false })
.then(headers => headers.location)
}
}