This repository has been archived by the owner on Aug 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 237
/
client.coffee
297 lines (249 loc) · 8.78 KB
/
client.coffee
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#
# client.coffee: A client for github
#
# Copyright © 2011 Pavan Kumar Sunkara. All rights reserved
#
# Requiring modules
request = require 'request'
url = require 'url'
Promise = require 'bluebird'
pkg = require '../../package.json'
Me = require './me'
User = require './user'
Repo = require './repo'
Org = require './org'
Gist = require './gist'
Team = require './team'
Pr = require './pr'
Project = require './project'
Release = require './release'
Issue = require './issue'
Milestone = require './milestone'
Label = require './label'
Notification = require './notification'
extend = require 'deep-extend'
Search = require './search'
# Specialized error
class HttpError extends Error
constructor: (@message, @statusCode, @headers, @body) ->
# Initiate class
class Client
constructor: (@token, @options) ->
@request = @options and @options.request or request
@requestDefaults =
headers:
'User-Agent': "octonode/#{pkg.version} (https://github.com/pksunkara/octonode) terminal/0.0"
if @token and typeof @token == 'string'
@requestDefaults.headers.Authorization = "token " + @token
# Add http conditional "etag"
conditional: (etag) ->
@etag = etag
return this
# Get authenticated user instance for client
me: ->
Promise.promisifyAll(new Me(@) , {multiArgs: true})
# Get user instance for client
user: (name) ->
Promise.promisifyAll(new User(name, @) , {multiArgs: true})
# Get repository instance for client
repo: (name) ->
Promise.promisifyAll(new Repo(name, @) , {multiArgs: true})
# Get organization instance for client
org: (name) ->
Promise.promisifyAll(new Org(name, @) , {multiArgs: true})
# Get gist instance for client
gist: ->
Promise.promisifyAll(new Gist(@) , {multiArgs: true})
# Get team instance for client
team: (id) ->
Promise.promisifyAll(new Team(id, @) , {multiArgs: true})
# Get pull request instance for client
pr: (repo, number) ->
Promise.promisifyAll(new Pr(repo, number, @) , {multiArgs: true})
release: (repo, number) ->
Promise.promisifyAll(new Release(repo, number, @) , {multiArgs: true})
# Get search instance for client
search: ->
Promise.promisifyAll(new Search(@) , {multiArgs: true})
issue: (repo, number) ->
Promise.promisifyAll(new Issue(repo, number, @) , {multiArgs: true})
project: (repo, number) ->
Promise.promisifyAll(new Project(repo, number, @) , {multiArgs: true})
milestone: (repo, number) ->
Promise.promisifyAll(new Milestone(repo, number, @) , {multiArgs: true})
label: (repo, name) ->
Promise.promisifyAll(new Label(repo, name, @) , {multiArgs: true})
notification: (id) ->
Promise.promisifyAll(new Notification(id, @) , {multiArgs: true})
requestOptions: (params1, params2) =>
params3 = {}
if @etag
params3.headers = {
'If-None-Match': @etag
}
@etag = null
return extend @requestDefaults, params1, params2, params3
# Github api URL builder
buildUrl: (path = '/', pageOrQuery = null, per_page = null, since = null) ->
if pageOrQuery? and typeof pageOrQuery == 'object'
query = pageOrQuery
else
query = {}
if pageOrQuery?
if since? and since == true
query.since = pageOrQuery
else
query.page = pageOrQuery
query.per_page = per_page if per_page?
if @token and typeof @token == 'object' and @token.id
query.client_id = @token.id
query.client_secret = @token.secret
# https://github.com/pksunkara/octonode/issues/87
if query.q
q = query.q
delete query.q
if Object.keys(query).length
separator = '&'
else
separator = '?'
urlFromPath = url.parse path
_url = url.format
protocol: urlFromPath.protocol or @options and @options.protocol or "https:"
auth: urlFromPath.auth or if @token and @token.username and @token.password then "#{@token.username}:#{@token.password}" else ''
hostname: urlFromPath.hostname or @options and @options.hostname or "api.github.com"
port: urlFromPath.port or @options and @options.port
pathname: urlFromPath.pathname
query: query
if q
_url += "#{separator}q=#{q}"
query.q = q
return _url
errorHandle: (res, body, callback) ->
return callback(new HttpError('Error ' + res.statusCode, res.statusCode, res.headers)) if Math.floor(res.statusCode/100) is 5
if typeof body == 'string'
try
body = JSON.parse(body || '{}')
catch err
return callback(err)
return callback(new HttpError(body.message, res.statusCode, res.headers, body)) if body.message and res.statusCode in [400, 401, 403, 404, 410, 422]
callback null, res.statusCode, body, res.headers
# Github api GET request
get: (path, params..., callback) ->
@request @requestOptions(
uri: @buildUrl path, params...
method: 'GET'
), (err, res, body) =>
return callback(err) if err
@errorHandle res, body, callback
# Github api GET request
getNoFollow: (path, params..., callback) ->
@request @requestOptions(
uri: @buildUrl path, params...
method: 'GET'
followRedirect: false
), (err, res, body) =>
return callback(err) if err
@errorHandle res, body, callback
# Github api GET request
getOptions: (path, options, params..., callback) ->
@request @requestOptions({
uri: @buildUrl path, params...
method: 'GET'
}, options), (err, res, body) =>
return callback(err) if err
@errorHandle res, body, callback
# Github api GET request
getAccept: (path, accept, params..., callback) ->
@request @requestOptions(
uri: @buildUrl path, params...
method: 'GET'
headers:
'Content-Type': 'application/json'
'Accept': "application/vnd.github.#{accept}+json"
), (err, res, body) =>
return callback(err) if err
@errorHandle res, body, callback
# Github api POST request
post: (path, content, options, callback) ->
if !callback? and typeof options is 'function'
callback = options
options = {}
reqDefaultOption =
uri: @buildUrl path, options.query
method: 'POST'
headers:
'Content-Type': 'application/json'
if content
reqDefaultOption.body = JSON.stringify content
reqOpt = @requestOptions extend reqDefaultOption, options
@request reqOpt, (err, res, body) =>
return callback(err) if err
@errorHandle res, body, callback
# Github api PATCH request
patch: (path, content, callback) ->
@request @requestOptions(
uri: @buildUrl path
method: 'PATCH'
body: JSON.stringify content
headers:
'Content-Type': 'application/json'
), (err, res, body) =>
return callback(err) if err
@errorHandle res, body, callback
# Github api PATCH request
patchAccept: (path, accept, content, callback) ->
@request @requestOptions(
uri: @buildUrl path
method: 'PATCH'
body: JSON.stringify content
headers:
'Content-Type': 'application/json'
'Accept': "application/vnd.github.#{accept}+json"
), (err, res, body) =>
return callback(err) if err
@errorHandle res, body, callback
# Github api PUT request
put: (path, content, options, callback) ->
if !callback and options
callback = options
options =
'Content-Type': 'application/json'
@request @requestOptions(
uri: @buildUrl path
method: 'PUT'
body: JSON.stringify content
headers: options
), (err, res, body) =>
return callback(err) if err
@errorHandle res, body, callback
# Github api DELETE request
del: (path, content, callback) ->
@request @requestOptions(
uri: @buildUrl path
method: 'DELETE'
body: JSON.stringify content
headers:
'Content-Type': 'application/json'
), (err, res, body) =>
return callback(err) if err
@errorHandle res, body, callback
# Github api DELETE request
delAccept: (path, accept, content, callback) ->
@request @requestOptions(
uri: @buildUrl path
method: 'DELETE'
body: JSON.stringify content
headers:
'Content-Type': 'application/json'
'Accept': "application/vnd.github.#{accept}+json"
), (err, res, body) =>
return callback(err) if err
@errorHandle res, body, callback
limit: (callback) =>
@get '/rate_limit', (err, s, b) ->
return callback(err) if err
if s isnt 200 then callback(new HttpError('Client rate_limit error', s))
else callback null, b.resources.core.remaining, b.resources.core.limit, b.resources.core.reset, b
# Export modules
module.exports = (token, credentials...) ->
Promise.promisifyAll(new Client(token, credentials...) , {multiArgs: true})