2017-07-05 06:26:25 -05:00
|
|
|
import * as Promise from 'bluebird'
|
2017-11-10 07:34:45 -06:00
|
|
|
import { createWriteStream } from 'fs'
|
2017-11-10 10:27:49 -06:00
|
|
|
import * as request from 'request'
|
2017-12-28 04:16:08 -06:00
|
|
|
import { ACTIVITY_PUB } from '../initializers'
|
|
|
|
|
|
|
|
function doRequest (requestOptions: request.CoreOptions & request.UriOptions & { activityPub?: boolean }) {
|
|
|
|
if (requestOptions.activityPub === true) {
|
|
|
|
if (!Array.isArray(requestOptions.headers)) requestOptions.headers = {}
|
|
|
|
requestOptions.headers['accept'] = ACTIVITY_PUB.ACCEPT_HEADER
|
|
|
|
}
|
2017-11-09 10:51:58 -06:00
|
|
|
|
|
|
|
return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => {
|
|
|
|
request(requestOptions, (err, response, body) => err ? rej(err) : res({ response, body }))
|
|
|
|
})
|
|
|
|
}
|
2016-02-05 11:03:20 -06:00
|
|
|
|
2017-11-10 07:34:45 -06:00
|
|
|
function doRequestAndSaveToFile (requestOptions: request.CoreOptions & request.UriOptions, destPath: string) {
|
|
|
|
return new Promise<request.RequestResponse>((res, rej) => {
|
|
|
|
request(requestOptions)
|
|
|
|
.on('response', response => res(response as request.RequestResponse))
|
|
|
|
.on('error', err => rej(err))
|
|
|
|
.pipe(createWriteStream(destPath))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-05 11:03:20 -06:00
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
export {
|
2017-11-09 10:51:58 -06:00
|
|
|
doRequest,
|
2017-11-10 10:27:49 -06:00
|
|
|
doRequestAndSaveToFile
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|