-
Notifications
You must be signed in to change notification settings - Fork 5
/
request_to_GIT.js
54 lines (43 loc) · 1.49 KB
/
request_to_GIT.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* For GIT we need to use https */
var https = require("https");
//Wrapping this code into a function to reuse it for different user and adding callback as it takes time to get response
function getRepos(username , callback){
/* We have to pass some options into the request,Creating a seperate object for that*/
var options = {
host: 'api.github.com',
path: '/users/' + username + '/repos', // From API Documentation http://developer.github.com/v3/ || Goto-> Repositories -> List user repositories
method: 'GET',
//Request requires user-agent
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0'
}
};
/* Create our request and get response */
var request = https.request(options,function (response){
var body = '';
//Getting data in chunks and concatinating it to create data object (JSON)
response.on("data",function(chunk){
body+= chunk.toString('utf8');
});
response.on("end",function(){
//Create a new array to get response
var repos = [];
var json = JSON.parse(body);
json.forEach(function(repo){
repos.push({
name: repo.name,
description: repo.description
});
});
//Passing result after receiving request
callback(repos);
});
});
//To finish our request
request.end();
}
//Calling the above function to get repose
getRepos("nishantmendiratta",function(repos){
console.log("Nishant Mendiratta has " + repos.length + " repos");
console.log("Repos : ", repos);
});