Created
September 13, 2017 14:15
-
-
Save Sojer23/46bf43b04fae8f6a6ce2d8a79868b4c8 to your computer and use it in GitHub Desktop.
promises-exer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
var request = require("request"), | |
Promise = require('bluebird'); | |
var usersL = ['dani8art', 'antgamdia', 'feserafim', 'pafmon', 'antonioruizcortes', 'jesusguerre']; | |
var url = "https://api.github.com/users/"; | |
var token = process.env.GITHUB_TOKEN; | |
/** | |
* @function Se utiliza para autenticación y acceso a url en GITHUB | |
* @param {String} url | |
* @param {*} callback | |
*/ | |
function gh(url, callback) { | |
//err response body | |
request({ | |
url: url, | |
json: true, headers: { | |
'User-Agent': 'js-tutorial' | |
}, | |
'auth': { | |
'user': 'Sojer23', | |
'pass': token | |
} | |
}, callback); | |
return true; | |
} | |
////////////////1. USER ///////////// | |
///////////REQUEST PARALELO////////// | |
///////////TRATAMIENTO SECUENCIAL////////// | |
function getUsers(user) { | |
return new Promise((resolve, reject) => { | |
gh(url + user, (err, response, body) => { | |
if (err) { | |
reject(err); | |
} else { | |
resolve(body); | |
} | |
}) | |
}) | |
} | |
////////////////2. REPO///////////// | |
function getAllRepos(user) { | |
var url = "https://api.github.com/users/" + user + "/repos"; | |
return new Promise((resolve, reject) => { | |
gh(url, (err, response, body) => { | |
if (err) { | |
reject(err); | |
} else { | |
resolve(body); | |
} | |
}); | |
}); | |
} | |
function getIssues(repo) { | |
var url = "https://api.github.com/repos/" + repo.owner.login + "/" + repo.name; | |
return new Promise((resolve, reject) => { | |
gh(url, (err, response, body) => { | |
if (err) { | |
reject(err); | |
} else { | |
resolve(body); | |
} | |
}); | |
}); | |
} | |
getUsers().then(() => { | |
var users = []; | |
var repos = []; | |
var issues = []; | |
var promises = []; | |
var promises2 = []; | |
usersL.forEach(function (element) { | |
promises.push(getUsers(element)) | |
}, this); | |
repos.forEach(function (element) { | |
promises2.push(getIssues(element)); | |
}, this); | |
//REQUEST PARALELO Y TRATAMIENTO SECUENCIAL | |
Promise.each(promises, (body) => { | |
// console.log(body); | |
console.log("Solicitando usuario..."); | |
console.log("User: " + body.login + " || Nombre: " + body.name); | |
users.push(body.login); | |
console.log(users); | |
}).then(() => { | |
console.log("////////////USERS LOADED/////////////"); | |
//REQUEST SECUENCIAL Y TRATAMIENTO SECUENCIAL | |
Promise.each(users, (user) => { | |
console.log(user); | |
return getAllRepos(user).then((body) => { | |
console.log("Getting repos..."); | |
repos.push(body); | |
//console.log(body); | |
}).catch((err) => { | |
if (err) throw new Error('Error getting repo of ' + user + "" + err.toString()) | |
}); | |
}).then(() => { | |
console.log("////////////REPOS LOADED/////////////"); | |
//REQUEST PARALELO Y TRATAMIENTO UNICO | |
Promise.each(repos, (body) => { | |
return getAllRepos(user).then((body) => { | |
console.log("Getting issues..."); | |
issues.push(body.open_issues_count); | |
}).catch((err) => { | |
if (err) throw new Error('Error getting issues of ' + user + " " + err.toString()); | |
}); | |
}).then(); | |
}).catch((err) => { | |
if (err) throw new Error('Error getting repo of ' + "" + err.toString()) | |
}); | |
}).catch((err) => { | |
if (err) throw new Error('Error getting user ' + err.toString()) | |
}); | |
}).catch(err => { | |
if (err) throw new Error("Error requesting users, " + err.toString()) | |
}); | |
////////////////3. ISSUES ///////////// | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment