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

44 lines
1.3 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-09-02 16:24:25 +02:00
import { getWebserverUrl } from '../initializers/constants'
2020-03-04 15:32:39 +01:00
import { waitMs } from './core-utils'
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
2020-09-02 16:24:25 +02:00
requestOptions.headers['User-Agent'] = `PeerTube search index (+${getWebserverUrl()})`
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 }> {
2020-11-03 09:06:33 +01:00
const updatedRequestOptions = Object.assign({}, requestOptions, { timeout: 10000 })
const res = await doRequest<T>(updatedRequestOptions)
2020-03-04 15:32:39 +01:00
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
}