graphbrainz/src/extensions/mediawiki/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

54 lines
1.4 KiB
JavaScript

import URL from 'url'
import Client from '../../api/client'
export default class MediaWikiClient extends Client {
constructor ({
limit = 10,
period = 1000,
...options
} = {}) {
super({ limit, period, ...options })
}
imageInfo (page) {
const pageURL = URL.parse(page, true)
const ClientError = this.errorClass
if (!pageURL.pathname.startsWith('/wiki/')) {
return Promise.reject(new ClientError(
`MediaWiki page URL does not have the expected /wiki/ prefix: ${page}`
))
}
const apiURL = URL.format({
protocol: pageURL.protocol,
auth: pageURL.auth,
host: pageURL.host,
pathname: '/w/api.php',
query: {
action: 'query',
titles: pageURL.pathname.slice(6),
prop: 'imageinfo',
iiprop: 'url|size|canonicaltitle|user|extmetadata',
format: 'json'
}
})
return this.get(apiURL, { json: true })
.then(body => {
const pageIDs = Object.keys(body.query.pages)
if (pageIDs.length !== 1) {
throw new ClientError(
`Query returned multiple pages: [${pageIDs.join(', ')}]`
)
}
const imageInfo = body.query.pages[pageIDs[0]].imageinfo
if (imageInfo.length !== 1) {
throw new ClientError(
`Query returned info for ${imageInfo.length} images, expected 1.`
)
}
return imageInfo[0]
})
}
}