sepia-search-motore-di-rice.../server/helpers/requests.ts

43 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-02-13 11:49:03 +01:00
import * as Bluebird from 'bluebird'
import * as request from 'request'
2020-03-04 15:32:39 +01:00
import { waitMs } from './core-utils'
2020-06-05 11:19:50 +02:00
import { CONFIG } from 'server/initializers/constants'
2020-02-13 11:49:03 +01:00
2020-02-13 16:06:52 +01:00
function doRequest <T> (
2020-02-13 11:49:03 +01:00
requestOptions: request.CoreOptions & request.UriOptions
2020-02-13 16:06:52 +01:00
): Bluebird<{ response: request.RequestResponse, body: T }> {
2020-02-18 15:36:38 +01:00
if (!(requestOptions.headers)) requestOptions.headers = {}
2020-06-05 11:19:50 +02:00
const webserverUrl = CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
requestOptions.headers['User-Agent'] = `PeerTube search index (+${webserverUrl})`
2020-02-18 15:36:38 +01:00
2020-02-13 16:06:52 +01:00
return new Bluebird<{ response: request.RequestResponse, body: T }>((res, rej) => {
2020-02-13 11:49:03 +01:00
request(requestOptions, (err, response, body) => err ? rej(err) : res({ response, body }))
})
}
2020-03-04 15:32:39 +01:00
async function doRequestWithRetries<T> (
requestOptions: request.CoreOptions & request.UriOptions,
maxRetries: number,
msToWait: number,
currentRetry = 0
): Promise<{ response: request.RequestResponse, body: T }> {
const res = await doRequest<T>(requestOptions)
if (res.response.statusCode === 429) {
if (currentRetry < maxRetries) {
await waitMs(msToWait)
return doRequestWithRetries(requestOptions, maxRetries, msToWait, currentRetry + 1)
}
throw new Error('Exceeded max retries for request ' + requestOptions.uri)
}
return res
}
2020-02-13 11:49:03 +01:00
export {
2020-03-04 15:32:39 +01:00
doRequest,
doRequestWithRetries
2020-02-13 11:49:03 +01:00
}