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
executable file
·155 lines (131 loc) · 4.29 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
#
# client.coffee: A client for github
#
# Copyright © 2011 Pavan Kumar Sunkara. All rights reserved
#
# Requiring modules
@request = request = require 'request'
@url = url = require 'url'
Me = require './me'
User = require './user'
Repo = require './repo'
Org = require './org'
Gist = require './gist'
Team = require './team'
Pr = require './pr'
Issue = require './issue'
Search = require './search'
# Specialized error
class HttpError extends Error
constructor: (@message, @statusCode) ->
# Initiate class
class Client
constructor: (@token) ->
# Get authenticated user instance for client
me: ->
new Me @
# Get user instance for client
user: (name) ->
new User name, @
# Get repository instance for client
repo: (name) ->
new Repo name, @
# Get organization instance for client
org: (name) ->
new Org name, @
# Get gist instance for client
gist: ->
new Gist @
# Get team instance for client
team: (id) ->
new Team id, @
# Get pull request instance for client
pr: (repo, number) ->
new Pr repo, number, @
# Get search instance for client
search: ->
new Search @
issue: (repo, number) ->
new Issue repo, number, @
# Github api URL builder
buildUrl: (path = '/', pageOrQuery = null, per_page = null) ->
if pageOrQuery? and typeof pageOrQuery == 'object'
query = pageOrQuery
else
query = {}
query.page = pageOrQuery if pageOrQuery?
query.per_page = per_page if per_page?
if typeof @token == 'string'
query.access_token = @token
else if typeof @token == 'object' and @token.id
query.client_id = @token.id
query.client_secret = @token.secret
url.format
protocol: "https:"
auth: if typeof @token == 'object' and @token.username then "#{@token.username}:#{@token.password}" else ''
hostname: "api.github.com"
pathname: path
query: query
errorHandle: (res, body, callback) ->
# TODO: More detailed HTTP error message
return callback(new HttpError('Error ' + res.statusCode, res.statusCode)) if Math.floor(res.statusCode/100) is 5
try
body = JSON.parse(body || '{}')
catch err
return callback(err)
return callback(new HttpError(body.message, res.statusCode)) if body.message and res.statusCode is 422
return callback(new HttpError(body.message, res.statusCode)) if body.message and res.statusCode in [400, 401, 404]
callback null, res.statusCode, body, res.headers
# Github api GET request
get: (path, params..., callback) ->
request
uri: @buildUrl path, params...
method: 'GET'
headers:
'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0'
, (err, res, body) =>
return callback(err) if err
@errorHandle res, body, callback
# Github api POST request
post: (path, content, callback) ->
request
uri: @buildUrl path
method: 'POST'
body: JSON.stringify content
headers:
'Content-Type': 'application/json'
'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0'
, (err, res, body) =>
return callback(err) if err
@errorHandle res, body, callback
# Github api PUT request
put: (path, content, callback) ->
request
uri: @buildUrl path
method: 'PUT'
body: JSON.stringify content
headers:
'Content-Type': 'application/json'
'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0'
, (err, res, body) =>
return callback(err) if err
@errorHandle res, body, callback
# Github api DELETE request
del: (path, content, callback) ->
request
uri: @buildUrl path
method: 'DELETE'
body: JSON.stringify content
headers:
'Content-Type': 'application/json'
'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0'
, (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.rate.remaining, b.rate.limit
# Export modules
module.exports = (token, credentials...) ->
new Client(token, credentials...)