-
Notifications
You must be signed in to change notification settings - Fork 44
/
gh-api.js
101 lines (86 loc) · 2.57 KB
/
gh-api.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var request = require('request');
var qs = require('querystring');
var sys = require('util');
var GitHubApi = require('github');
module.exports.GHAPI = function(access_token) {
var self = this;
this.api_base = "https://api.github.com/";
this.host = "api.github.com";
this.port = 443;
this.api_path_base = "/";
this.access_token = access_token;
this.client = new GitHubApi({
version: "3.0.0"
});
var requestCallback = function(callback) {
return function(err, res, data) {
if (err) {
callback(err, res, data);
} else {
callback(null, res, JSON.parse(data));
}
};
};
var JSON_stringify = function(s, emit_unicode) {
var json;
json = JSON.stringify(s);
if (emit_unicode) {
return json;
}
return json.replace(/[\u007f-\uffff]/g, function(c) {
return '\\u' + ('0000' + c.charCodeAt(0).toString(16)).slice(-4);
});
};
var get = function(path, params, callback) {
if (arguments.length == 2) {
callback = params;
params = {access_token: self.access_token};
} else {
if (!params.access_token) {
params.access_token = self.access_token;
}
}
var full_path = self.api_base + path + "?" + qs.stringify(params);
console.log(full_path);
request(full_path, requestCallback(callback));
};
var post = function(path, params, callback) {
if (!params.access_token) {
params.access_token = self.access_token;
}
var opts = {
uri: self.api_base + path,
method: "POST",
headers: {
"Authorization": "Bearer " + params.access_token,
"Content-type": "application/json"
},
body: JSON_stringify(params)
};
console.log(sys.inspect(opts));
var finalCallback = requestCallback(callback);
request(opts, finalCallback);
};
var oauth_post = function(path, params, callback) {
var opts = {
uri: path,
method: "POST",
headers: {
"Content-type": "application/json"
},
body: JSON_stringify(params)
};
console.log(sys.inspect(opts));
var finalCallback = requestCallback(callback);
request(opts, finalCallback);
};
this.getAccessToken = function(code, callback) {
oauth_post("https://github.com/login/oauth/access_token", {code: code, client_id: process.env.GH_CONSUMER_KEY, client_secret: process.env.GH_CONSUMER_SECRET}, callback);
};
this.request = function(gh_func, opts, callback) {
if (this.access_token) {
this.client.authenticate({type: "oauth", "token": this.access_token});
}
gh_func(opts, callback);
};
};