Skip to content

Instantly share code, notes, and snippets.

@aktowns
Created April 26, 2011 12:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aktowns/942163 to your computer and use it in GitHub Desktop.
Save aktowns/942163 to your computer and use it in GitHub Desktop.
Wrapper around the internode api, for node.js
// internode-usage.js
// written by ashley towns <ashleyis@me.com>
var _ = require("underscore");
var request = require("request");
var xml2js = require("xml2js-expat");
//
var account_id = null;
var auth = null;
exports.setLogin = function (login) { auth = 'Basic ' + new Buffer(login.user + ':' + login.pass).toString('base64'); };
exports.getUsage = function(callback) {
getAccountId(function (id) {
request({uri: "https://customer-webtools-api.internode.on.net/api/v1.5/"+id+"/usage", headers: {'Authorization' : auth}}, function (error, response, body) {
if (error) error;
var parser = new xml2js.Parser(function(result, error) {
if (error) throw console.error(error);
stats = result.api.traffic['@'];
stats.usage = parseInt(result.api.traffic['#']);
stats.usage_kb = Math.ceil(stats.usage / 1024);
stats.usage_mb = Math.ceil(stats.usage_kb / 1024);
callback(stats);
}).parseString(body);
});
});
};
exports.getHistory = function(callback) {
getAccountId(function (id) {
request({uri: "https://customer-webtools-api.internode.on.net/api/v1.5/"+id+"/history", headers: {'Authorization' : auth}}, function (error, response, body) {
if (error) error;
var parser = new xml2js.Parser(function(result, error) {
if (error) throw console.error(error);
callback(_.map(result.api.usagelist.usage, function (e) {
return {
day: e['@'].day,
traffic: parseInt(e.traffic['#']),
traffic_kb: Math.ceil(e.traffic['#'] / 1024),
traffic_mb: Math.ceil((e.traffic['#'] / 1024) / 1024)
}
}));
}).parseString(body);
});
});
};
// private
getAccountId = function (callback) {
if (account_id == null)
request({uri: "https://customer-webtools-api.internode.on.net/api/v1.5/", headers: {'Authorization' : auth}}, function (error, response, body) {
if (error) error;
var parser = new xml2js.Parser(function(result, error) {
if (error) throw console.error(error);
account_id = result.api.services.service['#']
callback(account_id);
}).parseString(body);
});
else
callback(account_id);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment