-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-glassfrog.js
259 lines (205 loc) · 6.99 KB
/
node-glassfrog.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
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
/*!
* node-glassfrog
* A nodejs library which supports Glassfrog's REST API.
* Author: Roger Pfaff <[email protected]>
* Reference: https://app.glassfrog.com/docs/api/v3
*/
'use strict()';
/**
* Module dependencies
*/
var request = require('request');
var Url = require('url');
////////////////////////////////////////////////////////////////////////////////////
/**
* Glassfrog
*
* @param {String} endpoint, Glassfrog hostname
* @param {Object} config
* - {String} apiKey, API access key for Glassfrog
*/
function Glassfrog(endpoint, config) {
if (!endpoint) throw new Error('endpoint not specified!');
if (typeof endpoint === 'string') {
host = Url.parse(endpoint);
} else if (typeof endpoint !== 'object') {
throw new Error('endpoint should be a string or url object!');
}
var baseUrl = Url.format(endpoint);
this._request = request.defaults({baseUrl: baseUrl});
if (!config || !(config.apiKey)) {
throw new Error('You should provide an API key!');
}
this.config = config;
}
Glassfrog.prototype = {
// get & set property
get apiKey() {
return this.config.apiKey;
},
set apiKey(apiKey) {
this.config.apiKey = apiKey;
},
};
/**
* encodeURL
*/
Glassfrog.prototype.encodeURL = function(path, params) {
if (path.slice(0, 1) != '/') path = '/' + path;
var query = querystring.stringify(params);
if (query) path = path + '?' + query;
return path;
};
/**
* request - request url from Glassfrog
*/
Glassfrog.prototype.request = function(method, path, params, callback) {
var opts = {
method: method,
qs: method === 'GET' ? params : undefined,
body: method === 'PATCH' || method === 'POST' ? params : undefined,
headers: {
'Content-Type': 'application/json'
},
};
if (this.apiKey) {
opts.headers['X-Auth-Token'] = this.config.apiKey;
} else {
throw new Error('No API Key provided !');
}
var req = this._request(path, opts, function(err, res, body) {
if (err) return callback(err);
if (res.statusCode != 200 && res.statusCode != 201) {
var msg = {
ErrorCode: res.statusCode,
Message: res.statusMessage,
Detail: body
};
return callback(JSON.stringify(msg));
}
});
};
/////////////////////////////////////// REST API for Circles ///////////////////////////////////////
/**
* Listing Circles
*/
Glassfrog.prototype.circles = function(params, callback) {
this.request('GET', '/circles', params, callback);
};
/**
* Showing a circle
*/
Glassfrog.prototype.get_circle_by_id = function(id, params, callback) {
if (typeof id !== 'number') throw new Error('Circle ID must be an integer above 0 !');
this.request('GET', '/circles/' + id, params, callback);
};
/**
* Showing a circle including circle members
*/
Glassfrog.prototype.get_circle_including_members = function(id, callback) {
if (typeof id !== 'number') throw new Error('Circle ID must be an integer above 0 !');
params = {include: "members"};
this.request('GET', '/circles/' + id, params, callback);
};
/**
* Showing a circle's people
*/
Glassfrog.prototype.get_circle_roles = function(id, params, callback) {
if (typeof id !== 'number') throw new Error('Circle ID must be an integer above 0 !');
this.request('GET', '/circles/' + id + '/people', params, callback);
};
/**
* Showing a circle's roles
*/
Glassfrog.prototype.get_circle_roles = function(id, params, callback) {
if (typeof id !== 'number') throw new Error('Circle ID must be an integer above 0 !');
this.request('GET', '/circles/' + id + '/roles', params, callback);
};
/**
* Showing a circle's projects
*/
Glassfrog.prototype.get_circle_projects = function(id, params, callback) {
if (typeof id !== 'number') throw new Error('Circle ID must be an integer above 0 !');
this.request('GET', '/circles/' + id + '/projects', params, callback);
};
/**
* Showing a circle's metrics
*/
Glassfrog.prototype.get_circle_metrics = function(id, params, callback) {
if (typeof id !== 'number') throw new Error('Circle ID must be an integer above 0 !');
this.request('GET', '/circles/' + id + '/metrics', params, callback);
};
/**
* Showing a circle's checklist items
*/
Glassfrog.prototype.get_circle_checklist = function(id, params, callback) {
if (typeof id !== 'number') throw new Error('Circle ID must be an integer above 0 !');
this.request('GET', '/circles/' + id + '/checklist_items', params, callback);
};
/////////////////////////////////////// REST API for Projects ///////////////////////////////////////
/**
* Create a project
*/
Glassfrog.prototype.create_project = function(params, callback) {
this.request('POST', '/projects', params, callback);
};
/**
* Update a project
*/
Glassfrog.prototype.update_project = function(id, params, callback) {
if (typeof id !== 'number') throw new Error('Project ID must be an integer above 0 !');
this.request('PATCH', '/projects/' + id, params, callback);
};
/**
* Archive a project
*/
Glassfrog.prototype.archive_project = function(id, params, callback) {
if (typeof id !== 'number') throw new Error('Project ID must be an integer above 0 !');
this.request('PATCH', '/projects/' + id, params, callback);
};
/**
* Delete a project
*/
Glassfrog.prototype.delete_project = function(id, params, callback) {
if (typeof id !== 'number') throw new Error('Project ID must be an integer above 0 !');
this.request('DELETE', '/projects/' + id, params, callback);
};
/////////////////////////////////////// REST API for People ///////////////////////////////////////
/**
* Showing people of the organization
*/
Glassfrog.prototype.get_people = function(params, callback) {
this.request('GET', '/people', params, callback);
};
/**
* Showing a specific person
*/
Glassfrog.prototype.get_person = function(id, params, callback) {
if (typeof id !== 'number') throw new Error('Person ID must be an integer above 0 !');
this.request('GET', '/people/' + id, params, callback);
};
/////////////////////////////////////// REST API for Roles ///////////////////////////////////////
/**
* Get Roles for the organization
*/
Glassfrog.prototype.roles = function(params, callback) {
this.request('GET', '/roles', params, callback);
};
/**
* Get Roles for a circle
*/
Glassfrog.prototype.roles_circle = function(id, params, callback) {
if (typeof id !== 'number') throw new Error('Person ID must be an integer above 0 !');
this.request('GET', '/circles/' + id + '/roles', params, callback);
};
/**
* Get Roles for a person
*/
Glassfrog.prototype.roles_person = function(id, params, callback) {
if (typeof id !== 'number') throw new Error('Person ID must be an integer above 0 !');
this.request('GET', '/people/' + id + '/roles', params, callback);
};
/////////////////////////////////////// REST API for Metrics ///////////////////////////////////////
/////////////////////////////////////// REST API for Checklist Items ///////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
module.exports = Glassfrog;