-
Notifications
You must be signed in to change notification settings - Fork 16
/
chef.js
39 lines (32 loc) · 1.11 KB
/
chef.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
var authenticate = require('./chef/authenticate'),
request = require('request'),
methods = ['delete', 'get', 'post', 'put'];
function Chef(user, key, base) {
this.user = user;
this.key = key;
this.base = base ? base : '';
}
function req(method, uri, body, callback) {
method = method.toUpperCase();
// Add the base property of the client if the request does not specify the
// full URL.
if (uri.indexOf(this.base) !== 0) { uri = this.base + uri; }
// Use the third parameter as the callback if a body was not given (like for
// a GET request.)
if (typeof body === 'function') { callback = body; body = undefined; }
return request({
body: body,
headers: authenticate(this, { body: body, method: method, uri: uri }),
json: true,
method: method,
uri: uri
}, callback);
}
methods.forEach(function (method) {
Chef.prototype[method] = function (uri, body, callback) {
return req.call(this, method, uri, body, callback);
};
});
exports.createClient = function (user, key, server) {
return new Chef(user, key, server);
};