migrate github badges to use got instead of request; affects [github librariesio] (#7212)
* migrate github badges to use got instead of request * simplify creation of requestFetcher in libraries.io base * improve libraries.io connection error test Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
This commit is contained in:
@@ -3,6 +3,7 @@ import log from '../../core/server/log.js'
|
||||
import { TokenPool } from '../../core/token-pooling/token-pool.js'
|
||||
import { userAgent } from '../../core/base-service/legacy-request-handler.js'
|
||||
import { nonNegativeInteger } from '../validators.js'
|
||||
import { ImproperlyConfigured } from '../index.js'
|
||||
|
||||
const headerSchema = Joi.object({
|
||||
'x-ratelimit-limit': nonNegativeInteger,
|
||||
@@ -139,10 +140,7 @@ class GithubApiProvider {
|
||||
}
|
||||
}
|
||||
|
||||
// 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, options = {}, callback) {
|
||||
async fetch(requestFetcher, url, options = {}) {
|
||||
const { baseUrl } = this
|
||||
|
||||
let token
|
||||
@@ -151,8 +149,10 @@ class GithubApiProvider {
|
||||
try {
|
||||
token = this.tokenForUrl(url)
|
||||
} catch (e) {
|
||||
callback(e)
|
||||
return
|
||||
log.error(e)
|
||||
throw new ImproperlyConfigured({
|
||||
prettyMessage: 'Unable to select next Github token from pool',
|
||||
})
|
||||
}
|
||||
tokenString = token.id
|
||||
} else {
|
||||
@@ -162,8 +162,6 @@ class GithubApiProvider {
|
||||
const mergedOptions = {
|
||||
...options,
|
||||
...{
|
||||
url,
|
||||
baseUrl,
|
||||
headers: {
|
||||
'User-Agent': userAgent,
|
||||
Authorization: `token ${tokenString}`,
|
||||
@@ -171,31 +169,15 @@ class GithubApiProvider {
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
request(mergedOptions, (err, res, buffer) => {
|
||||
if (err === null) {
|
||||
if (this.withPooling) {
|
||||
if (res.statusCode === 401) {
|
||||
this.invalidateToken(token)
|
||||
} else if (res.statusCode < 500) {
|
||||
this.updateToken({ token, url, res })
|
||||
}
|
||||
}
|
||||
const response = await requestFetcher(`${baseUrl}${url}`, mergedOptions)
|
||||
if (this.withPooling) {
|
||||
if (response.res.statusCode === 401) {
|
||||
this.invalidateToken(token)
|
||||
} else if (response.res.statusCode < 500) {
|
||||
this.updateToken({ token, url, res: response.res })
|
||||
}
|
||||
callback(err, res, buffer)
|
||||
})
|
||||
}
|
||||
|
||||
requestAsPromise(request, url, options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.request(request, url, options, (err, res, buffer) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
} else {
|
||||
resolve({ res, buffer })
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
return response
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user