Fixed crashes and added new requestPromise features (headers, json)

This commit is contained in:
lanxu 2017-07-15 13:58:02 +03:00
parent 57c8220044
commit a07b24bbaa
2 changed files with 38 additions and 22 deletions

View File

@ -15,10 +15,14 @@ module.exports = function(config) {
key: apikey key: apikey
}; };
console.log(videosParams);
var thisResolve = resolve;
var thisReject = reject;
youtube.videos.list(videosParams, function(err, resp) { youtube.videos.list(videosParams, function(err, resp) {
//console.log('respo', resp); //console.log('respo', resp);
if(resp.items.length <= 0) {
reject('No results'); if(typeof resp === 'undefined' || resp === null || typeof resp.items === 'undefined' || resp.items === null || resp.items.length <= 0) {
thisReject('No results');
return; return;
} }
@ -32,7 +36,10 @@ module.exports = function(config) {
statistics: resp.items[0].statistics, statistics: resp.items[0].statistics,
duration: formattedTime duration: formattedTime
}; };
resolve(data); console.log(resp);
console.log(resp.items[0]);
console.log(data);
thisResolve(data);
}); });
}); });
return promise; return promise;
@ -46,7 +53,7 @@ module.exports = function(config) {
order: 'viewCount', order: 'viewCount',
key: apikey key: apikey
}; };
console.log(params);
var promise = new Promise(function(resolve, reject) { var promise = new Promise(function(resolve, reject) {
youtube.search.list(params, function(err, resp) { youtube.search.list(params, function(err, resp) {
if (err) { if (err) {

View File

@ -1,21 +1,30 @@
var request = require('request'); var request = require('request');
var Promise = require('promise'); var Promise = require('promise');
function RequestPromise(url, urlArgs) { function RequestPromise(url, urlArgs, headers, json) {
var promise = new Promise(function(resolve, reject) { if(typeof headers === 'undefined') {
request({ headers = {};
url: url, }
qs: urlArgs,
json: false if(typeof json === 'undefined') {
}, function(error, response, body) { json = false;
if(!error && response.statusCode == 200) { }
resolve(JSON.parse(body));
} else { var promise = new Promise(function(resolve, reject) {
reject(error); request({
} url: url,
}); qs: urlArgs,
}); headers: headers,
return promise; json: json
}; }, function(error, response, body) {
if(!error && response.statusCode == 200) {
resolve(JSON.parse(body));
} else {
reject(error);
}
});
});
return promise;
}
module.exports = RequestPromise; module.exports = RequestPromise;