Skip to content

Instantly share code, notes, and snippets.

@qzaidi
Created October 15, 2011 12:42
Show Gist options
  • Save qzaidi/1289509 to your computer and use it in GitHub Desktop.
Save qzaidi/1289509 to your computer and use it in GitHub Desktop.
node-magento-api
var xmlrpc = require('xmlrpc'),
sys = require('sys');
var config = {
host: 'www.mymagentohost.com',
port: 80,
path: '/api/xmlrpc/',
login: 'apilogin',
pass: 'myapikey'
};
var magento = {
client: xmlrpc.createClient(config),
init: function(cb) {
this._discover(cb);
return this;
},
login: function(next) {
var self = this;
self.client.methodCall('login', [ config.login, config.pass ], function(err,sessionId) {
if (err)
console.log("Login Error: " + err);
else {
console.log('session-id:' + sessionId);
self.client.sessionId = sessionId;
next();
}
});
},
end: function(cb) {
var client = this.client;
client.methodCall('endSession', [ client.sessionId ], cb);
},
_xcall: function(api,apiargs) {
var self = this;
var client = self.client;
var args = Array.prototype.slice.call(apiargs);
var callback = args.pop();
var next = function() {
client.methodCall('call', [ client.sessionId, api, args ], function(err,res) {
if (err) {
// try logging in again
if (err.faultCode == 5 && client.sessionId) {
client.sessionId = undefined;
return self._xcall(api,apiargs);
}
}
callback(err,res);
});
}
if (client.sessionId == undefined)
self.login(next);
else
next();
},
_call: function(apiargs) {
var args = Array.prototype.slice.call(apiargs);
var callback = args.pop();
var api = args.shift();
var client = this.client;
client.methodCall('call', [ client.sessionId, api, args ], callback);
},
_discover: function(cb) {
var self = this;
var client = self.client;
var next = function() {
client.methodCall('resources', [ client.sessionId ], function(err,resources) {
if (!err) {
for (var j =0; j < resources.length; j++) {
var resource = resources[j];
var methods = resource.methods;
self[resource.name] = {};
for (var i = 0; i < methods.length; i++) {
var method = methods[i];
self.define(method.path);
}
}
cb(null);
} else {
sys.log("an error occured " + err);
cb(err);
}
});
}
if (client.sessionId == undefined)
self.login(next);
else
next();
},
call: function() {
console.log("call called with arguments " + arguments);
var self = this;
var client = self.client;
var apiargs = arguments;
var next = function() {
self._call(apiargs);
}
if (client.sessionId == undefined)
self.login(next);
else
next();
},
define: function(apiname) {
var obj = this;
apis = apiname.split('.');
if (obj[apis[0]] == undefined)
obj[apis[0]] = {};
obj[apis[0]][apis[1]] = function() {
console.log('calling xcall for ' + apiname + '(' + arguments[0] + '...);');
obj._xcall.call(obj, apiname, arguments );
}
}
}
module.exports = magento;
if (require.main == module) {
magento.init(function(err) {
magento.sales_order.info('order_id', function(err,order) { sys.log(sys.inspect(order)); });
});
}
@qzaidi
Copy link
Author

qzaidi commented Nov 26, 2011

you should just call it as

magento.catalog_category.level(null,null,3,function(error,result) {
if (error) { throw error; }
response.json(result);
});

The first argument is website_id, second is storeView, and third is parentCategory.

There were a few issues in the gist that I found and fixed later - would update the above soon.

@JumpLink
Copy link

Edit: I've solved my problem.
Note: If you get an error like this: "Login Error: Error: Unknown XML-RPC tag 'TITLE'", is the reason, that your server has hypertext access. To resolve this, configure a extra domain without hypertext access for the domain "your.domain.com/api/xmlrpc". For example you can use authorization header in Nginx to do that.

@JumpLink
Copy link

JumpLink commented Jun 1, 2012

I've began to create a web-application based on your node magento api: https://github.com/JumpLink/magento-nodejs-sync-app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment