2017-07-05 13:26:25 +02:00
|
|
|
import * as Promise from 'bluebird'
|
2017-11-10 14:34:45 +01:00
|
|
|
import { createWriteStream } from 'fs'
|
2018-01-15 09:46:46 +01:00
|
|
|
import { RequestResponse } from 'request'
|
2017-11-10 17:27:49 +01:00
|
|
|
import * as request from 'request'
|
2017-12-28 11:16:08 +01:00
|
|
|
import { ACTIVITY_PUB } from '../initializers'
|
2018-01-15 09:46:46 +01:00
|
|
|
import Bluebird = require('bluebird')
|
2017-12-28 11:16:08 +01:00
|
|
|
|
2018-01-15 09:46:46 +01:00
|
|
|
function doRequest (
|
|
|
|
requestOptions: request.CoreOptions & request.UriOptions & { activityPub?: boolean }
|
|
|
|
): Bluebird<{ response: RequestResponse, body: any }> {
|
2017-12-28 11:16:08 +01:00
|
|
|
if (requestOptions.activityPub === true) {
|
|
|
|
if (!Array.isArray(requestOptions.headers)) requestOptions.headers = {}
|
|
|
|
requestOptions.headers['accept'] = ACTIVITY_PUB.ACCEPT_HEADER
|
|
|
|
}
|
2017-11-09 17:51:58 +01: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 18:03:20 +01:00
|
|
|
|
2017-11-10 14:34:45 +01: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 11:23:23 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2017-05-15 22:22:03 +02:00
|
|
|
export {
|
2017-11-09 17:51:58 +01:00
|
|
|
doRequest,
|
2017-11-10 17:27:49 +01:00
|
|
|
doRequestAndSaveToFile
|
2017-05-15 22:22:03 +02:00
|
|
|
}
|