Files
shields/services/github/github-api-provider.js
Paul Melnikow 7a664ca3e8 Run prettier (#1866)
Merging this separately so the commit with the tooling change is readable. This is a follow-on to #1167 which turned prettier on.
2018-08-08 17:57:14 -04:00

45 lines
1.1 KiB
JavaScript

'use strict'
const githubAuth = require('../../lib/github-auth')
// Provide an interface to the Github API. Manages the base URL.
//
// Eventually this class will be responsible for managing headers,
// authentication, and actually making the request. Currently it's delegating
// to legacy code.
class GithubApiProvider {
constructor({ baseUrl }) {
this.baseUrl = baseUrl
}
// Act like request(), but tweak headers and query to avoid hitting a rate
// limit. Inject `request` so we can pass in `cachingRequest` from
// `request-handler.js`.
request(request, url, query, callback) {
const { baseUrl } = this
githubAuth.request(
request,
`${baseUrl}${url}`,
query,
(err, res, buffer) => {
callback(err, res, buffer)
}
)
}
requestAsPromise(request, url, query) {
return new Promise((resolve, reject) => {
this.request(request, url, query, (err, res, buffer) => {
if (err) {
reject(err)
} else {
resolve({ res, buffer })
}
})
})
}
}
module.exports = GithubApiProvider