diff --git a/lib/iControl.js b/lib/iControl.js index 335089a..7f2bff9 100644 --- a/lib/iControl.js +++ b/lib/iControl.js @@ -24,50 +24,79 @@ var iControl = function(opts) { iControl.prototype.list = function(path, opts, cb) { // No options have been specified - if (arguments.length === 2) { + if (typeof opts === 'function') { cb = opts; opts = {}; } + opts = opts || {}; - // Options have been specified - else { + // Parse options + // TODO: support other options + var queryComponents = []; + if (opts.expandAll) queryComponents.push('$expand=*'); + if (opts.includeStats) queryComponents.push('$stats=true'); - // Parse options - // TODO: support other options - var queryComponents = []; - if (opts.expandAll) queryComponents.push('$expand=*'); - if (opts.includeStats) queryComponents.push('$stats=true'); - - // Build query string - if (queryComponents.length !== 0) { - opts.query = '?' + queryComponents.join('&'); - } + // Build query string + if (queryComponents.length !== 0) { + opts.query = '?' + queryComponents.join('&'); } + opts.path = path; opts.method = 'GET'; this._request(opts, cb); }; // Create -iControl.prototype.create = function(path, body, cb) { - var opts = { path: path, body: body, method: 'POST' }; +iControl.prototype.create = function(path, body, opts, cb) { + // No options have been specified + if (typeof opts === 'function') { + cb = opts; + opts = {}; + } + opts = opts || {}; + + opts.path = path; + opts.body = body; + opts.method = 'POST'; + this._request(opts, cb); }; // Modify -iControl.prototype.modify = function(path, body, cb) { - var opts = { path: path, body: body, method: 'PUT' }; +iControl.prototype.modify = function(path, body, opts, cb) { + // No options have been specified + if (typeof opts === 'function') { + cb = opts; + opts = {}; + } + opts = opts || {}; + + opts.path = path; + opts.body = body; + opts.method = 'PUT'; + this._request(opts, cb); }; // Delete -iControl.prototype.delete = function(path, cb) { - var opts = { path: path, method: 'DELETE' }; +iControl.prototype.delete = function(path, opts, cb) { + // No options have been specified + if (typeof opts === 'function') { + cb = opts; + opts = {}; + } + opts = opts || {}; + + opts.path = path; + opts.method = 'DELETE'; + this._request(opts, cb); }; // Execute request iControl.prototype._request = function(opts, cb) { + var header; + if (typeof opts.path !== 'string') return cb('URL must be specified', null); this.url = '/mgmt/tm' + opts.path; this.uri = this.proto + '://' + this.host + ':' + this.port + this.url; @@ -87,11 +116,28 @@ iControl.prototype._request = function(opts, cb) { }; // If body has been specified, add to request options - if (typeof opts.body === 'object') this.requestOpts.body = opts.body; + if (typeof opts.body === 'object' || typeof opts.body === 'string') this.requestOpts.body = opts.body; // If query string has been specified, add to request options if (typeof opts.query === 'string') this.requestOpts.qs = opts.query; + // If headers have been specified, add to request options + if (typeof opts.headers === 'object') { + this.requestOpts.headers = opts.headers; + + // If we are specifying the content-type and it's not json, don't + // set json as a request option + for (header in opts.headers) { + if (opts.headers.hasOwnProperty(header)) { + if (header.toLowerCase() === 'content-type') { + if (opts.headers[header] !== 'application/json') { + this.requestOpts.json = false; + } + } + } + } + } + // Do request request(this.requestOpts, function(err, res, body) { @@ -112,7 +158,7 @@ iControl.prototype._request = function(opts, cb) { return cb(msg, false); } - + // If retrieving via GET, handle pagination if (this.method === 'GET') { @@ -126,7 +172,7 @@ iControl.prototype._request = function(opts, cb) { return cb(false, body); } } - + // For POST/PUT/DELETE, return body else { return cb(false, body);