Skip to content
Browse files

Add helper methods callApi1 and callApi2

  • Loading branch information...
1 parent 2b3d08b commit 3fb8b3d88a3d2297599bbf0caca7239246eab5a9 @vially committed
Showing with 63 additions and 9 deletions.
  1. +15 −4 README.md
  2. +47 −4 lib/cpanel.js
  3. +1 −1 package.json
View
19 README.md
@@ -5,6 +5,7 @@ Node.js library for the cPanel/WHM API
$ npm install cpanel-lib
## Usage
+ ```js
var cpanel = require('cpanel-lib');
var options = {
@@ -17,10 +18,20 @@ Node.js library for the cPanel/WHM API
var cpanelClient = cpanel.createClient(options);
- cpanelClient.call('version', {}, function (result) {
- console.log('WHM Version: %j', result.version);
+ cpanelClient.call('version', {}, function (err, res) {
+ console.log('WHM Version: %j', res.version);
});
- cpanelClient.call('listaccts', {}, function (result) {
- console.log('Result: %j', result);
+ cpanelClient.call('listaccts', {}, function (err, res) {
+ console.log('Result: %j', res);
});
+
+ cpanelClient.callApi2('AddonDomain', 'listaddondomains', {}, function (err, res) {
+ console.log('Result: %j', res);
+ });
+ ```
+
+## API
+ cpanelClient.call(action, params, callback)
+ cpanelClient.callApi1(module, func, args, [user], callback)
+ cpanelClient.callApi2(module, func, args, [user], callback)
View
51 lib/cpanel.js
@@ -34,19 +34,62 @@ var cpanelJsonClient = function (obj) {
host: obj.host,
port: obj.port,
path: obj.path + action + '?' + params,
- method: 'GET',
headers: headers
};
- var req = obj.connection.request(options, function (res) {
+ var req = obj.connection.get(options, function (res) {
var data = '';
res.on('data', function (chunk) {
data += chunk.toString()
});
res.on('end', function() {
- callback(JSON.parse(data));
+ callback(null, JSON.parse(data));
});
+ }).on('error', function(e) {
+ callback(e);
});
- req.end();
+ };
+
+ obj.callApi1 = function (module, func, args) {
+ var callback;
+ var user;
+
+ if (arguments.length == 4) {
+ user = obj.username;
+ callback = arguments[3];
+ } else {
+ user = arguments[3];
+ callback = arguments[4];
+ }
+
+ var query = {};
+ for (var i = 0; i < args.length; i++) {
+ query['arg-' + i] = args[i];
+ }
+
+ obj.callOldApi(user, module, func, query, 1, callback);
+ };
+
+ obj.callApi2 = function (module, func, query) {
+ var callback;
+ var user;
+
+ if (arguments.length == 4) {
+ user = obj.username;
+ callback = arguments[3];
+ } else {
+ user = arguments[3];
+ callback = arguments[4];
+ }
+
+ obj._callOldApi(user, module, func, query, 2, callback);
+ };
+
+ obj._callOldApi = function (user, module, func, query, version, callback) {
+ query['cpanel_jsonapi_user'] = user;
+ query['cpanel_jsonapi_module'] = module;
+ query['cpanel_jsonapi_func'] = func;
+ query['cpanel_jsonapi_apiversion'] = version;
+ obj.call('cpanel', query, callback);
};
return obj;
View
2 package.json
@@ -2,7 +2,7 @@
"author": "Valentin-Costel Hăloiu <vially.ichb@gmail.com>",
"name": "cpanel-lib",
"description": "Node.js library for the cPanel/WHM API",
- "version": "0.0.1",
+ "version": "0.0.2",
"homepage": "https://github.com/vially/cpanel-lib",
"repository": {
"type": "git",

0 comments on commit 3fb8b3d

Please sign in to comment.
Something went wrong with that request. Please try again.