Skip to content
Browse files

Version 0.0.1

  • Loading branch information...
1 parent 512a78e commit 2b3d08b5f39c563948e0ebecb63fa59d88868e18 @vially committed Nov 11, 2011
Showing with 82 additions and 2 deletions.
  1. +1 −0 .gitignore
  2. +24 −0 README.md
  3. +53 −0 lib/cpanel.js
  4. +4 −2 package.json
View
1 .gitignore
@@ -0,0 +1 @@
+/node_modules
View
24 README.md
@@ -1,2 +1,26 @@
Node.js library for the cPanel/WHM API
=====
+
+## Instalation
+ $ npm install cpanel-lib
+
+## Usage
+ var cpanel = require('cpanel-lib');
+
+ var options = {
+ host: 'whm.example.com',
+ port: 2087,
+ secure: true,
+ username: 'WHM_USERNAME',
+ accessKey: 'YOUR_ACCESS_KEY'
+ };
+
+ var cpanelClient = cpanel.createClient(options);
+
+ cpanelClient.call('version', {}, function (result) {
+ console.log('WHM Version: %j', result.version);
+ });
+
+ cpanelClient.call('listaccts', {}, function (result) {
+ console.log('Result: %j', result);
+ });
View
53 lib/cpanel.js
@@ -0,0 +1,53 @@
+var qs = require('qs')
+
+exports.createClient = function (options) {
+ options = options || {};
+
+ var client = cpanelJsonClient({
+ host: options.host,
+ port: options.port || 2087,
+ secure: options.secure,
+ username: options.username,
+ accessKey: options.accessKey,
+ path: options.path || '/json-api/'
+ });
+
+ return client;
+};
+
+var cpanelJsonClient = function (obj) {
+ if (obj.secure == null)
+ obj.secure = true;
+
+ obj.connection = obj.secure ? require('https') : require('http');
+ obj.call = function (action, query, callback) {
+ if (obj.host == null || obj.username == null || obj.accessKey == null) {
+ throw("Host, username and accessKey must be set")
+ }
+
+ var params = qs.stringify(query);
+ var headers = {
+ "Authorization": "WHM " + obj.username + ":" + obj.accessKey
+ };
+
+ var options = {
+ host: obj.host,
+ port: obj.port,
+ path: obj.path + action + '?' + params,
+ method: 'GET',
+ headers: headers
+ };
+ var req = obj.connection.request(options, function (res) {
+ var data = '';
+ res.on('data', function (chunk) {
+ data += chunk.toString()
+ });
+ res.on('end', function() {
+ callback(JSON.parse(data));
+ });
+ });
+ req.end();
+ };
+
+ return obj;
+};
View
6 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.0",
+ "version": "0.0.1",
"homepage": "https://github.com/vially/cpanel-lib",
"repository": {
"type": "git",
@@ -12,6 +12,8 @@
"engines": {
"node": "~0.6.0"
},
- "dependencies": {},
+ "dependencies": {
+ "qs": ">= 0.3.2"
+ },
"devDependencies": {}
}

0 comments on commit 2b3d08b

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