2019-08-13 19:37:22 +03:00
|
|
|
var request = require('request')
|
|
|
|
var Promise = require('promise')
|
2017-03-12 18:36:32 +02:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
function RequestPromise (url, urlArgs, headers, json) {
|
|
|
|
if (typeof headers === 'undefined') {
|
|
|
|
headers = {}
|
2017-07-15 13:58:02 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
if (typeof json === 'undefined') {
|
|
|
|
json = false
|
2017-07-15 13:58:02 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
var promise = new Promise(function (resolve, reject) {
|
2017-07-15 13:58:02 +03:00
|
|
|
request({
|
|
|
|
url: url,
|
|
|
|
qs: urlArgs,
|
|
|
|
headers: headers,
|
|
|
|
json: json
|
2019-08-13 19:37:22 +03:00
|
|
|
}, function (error, response, body) {
|
|
|
|
if (!error && response.statusCode === 200) {
|
|
|
|
if (json === false) {
|
|
|
|
resolve(JSON.parse(body)) /// wtf
|
2018-10-27 14:50:31 +03:00
|
|
|
} else {
|
|
|
|
resolve(body)
|
|
|
|
}
|
2017-07-15 13:58:02 +03:00
|
|
|
} else {
|
2019-08-13 19:37:22 +03:00
|
|
|
reject(error)
|
2017-07-15 13:58:02 +03:00
|
|
|
}
|
2019-08-13 19:37:22 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
return promise
|
2017-07-15 13:58:02 +03:00
|
|
|
}
|
2016-05-22 00:24:02 +03:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
module.exports = RequestPromise
|