-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathgithub.js
More file actions
134 lines (125 loc) · 4.12 KB
/
github.js
File metadata and controls
134 lines (125 loc) · 4.12 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const https = require('https')
const settings = require('../config')
const defaultAccessToken = settings.github.accessToken
function send (opts) {
return new Promise((resolve, reject) => {
const accessToken = opts.accessToken || defaultAccessToken
const method = (opts.method || 'GET').toUpperCase()
const path = opts.path
const headers = opts.headers || {}
const body = opts.body
const _headers = {
'user-agent': 'node-red',
accept: 'application/vnd.github.v3',
authorization: 'token ' + accessToken
}
if (body) {
_headers['content-type'] = 'application/json'
}
for (const h in headers) {
_headers[h] = headers[h]
}
const options = {
host: 'api.github.com',
port: 443,
path,
method,
headers: _headers
}
// console.log("---------------");
// console.log(options);
// console.log("---------------");
const req = https.request(options, function (res) {
res.setEncoding('utf8')
let data = ''
res.on('data', function (chunk) {
data += chunk
})
res.on('end', function () {
if (/^application\/json/.test(res.headers['content-type'])) {
data = JSON.parse(data)
data.etag = res.headers.etag
data.rateLimit = {
limit: res.headers['x-ratelimit-limit'],
remaining: res.headers['x-ratelimit-remaining'],
reset: res.headers['x-ratelimit-reset']
}
}
resolve({ statusCode: res.statusCode, headers: res.headers, data })
})
})
req.on('error', function (e) {
console.log('problem with request: ' + e.message)
reject(e)
})
if (body) {
req.write(JSON.stringify(body) + '\n')
}
req.end()
})
}
function getSimple (path, lastEtag) {
return new Promise((resolve, reject) => {
const headers = {}
if (lastEtag) {
headers['If-None-Match'] = lastEtag
}
console.log('github.getSimple', path)
send({ path, headers }).then(function (result) {
if (lastEtag && result.statusCode === 304) {
resolve(null)
return null
} else if (result.statusCode === 404) {
reject(result)
return null
} else {
resolve(result.data)
return null
}
}).catch(function (er) { reject(er) })
})
}
function getGistFile (fileUrl) {
return new Promise((resolve, reject) => {
const req = https.get(fileUrl, function (res) {
res.setEncoding('utf8')
let data = ''
res.on('data', function (chunk) {
data += chunk
})
res.on('end', function () {
resolve(data)
})
})
req.on('error', function (e) {
console.log('problem with request: ' + e.message)
reject(e)
})
req.end()
})
}
module.exports = {
getGistFile,
getAuthedUser: function (accessToken) {
return new Promise((resolve, reject) => {
send({ path: '/user', accessToken }).then(function (result) {
resolve(result.data)
return null
}).catch(function (er) { reject(er) })
})
},
getUser: function (user, lastEtag) {
return getSimple('/users/' + user, lastEtag)
},
getGist: function (id, lastEtag) {
return getSimple('/gists/' + id, lastEtag)
},
createGist: function (gistData, accessToken) {
return new Promise((resolve, reject) => {
send({ path: '/gists', method: 'POST', body: gistData, accessToken }).then(function (result) {
resolve(result.data)
return null
}).catch(function (er) { reject(er) })
})
}
}