Skip to content

Instantly share code, notes, and snippets.

@Groxx
Created November 8, 2010 03:18
Show Gist options
  • Save Groxx/667344 to your computer and use it in GitHub Desktop.
Save Groxx/667344 to your computer and use it in GitHub Desktop.
Fairly simple lacuna API caller + batch capabilities. Part of a much larger project.
var api = {
calls: 0
, requests: 0
, buildRequest: function buildRequest(methodName, args) {
if (args === undefined){args = [];}
var request = {jsonrpc: "2.0", method: methodName, params: []};
// Build a request based on a method.
// First non-value in args is assumed to be a success callback (so a fail callback can be specified without a success callback. just pass null.)
// Second function (and all subsequent) in args is assumed to be an error callback
// id will be generated, and callbacks spawned as encountered via ID.
var hasSuccessCallback = false;
for(var i = 0; i < args.length; i++){
if (typeof(args[i]) == "string" || typeof(args[i]) == "number") request.params.push(args[i]);
if (hasSuccessCallback && typeof(args[i]) == "function") request.fail = args[i];
if (!hasSuccessCallback && (typeof(args[i]) == "function" || args[i] === undefined || args[i] === null)) {
request.success = args[i];
hasSuccessCallback = true;
}
}
if (methodName !== "login") {
if (sessionID === undefined) throw("Must have a session!")
request.params.unshift(sessionID);
}
return request;
}
, get: function get(moduleName, methodName, args, options){
var out;
if(args === undefined){args = [];}
args.unshift(methodName);
out = this.batch(moduleName, [args], options); // single requests get popped from array by server
return out;
}
, batch: function batch(moduleName, requests, options) {
var out; // result if synchronous
var performAsyncCallback = function(data, request){
if(data.error){
console.error("API request error: " + JSON.stringify(data));
if(typeof(request.fail) == "function"){
request.fail(data);
}
} else {
if(typeof(request.success) == "function"){
requests.success(data);
}
}
};
// handle both request objects and on-the-fly methodName + params
for(var i = 0; i < requests.length; i++){
if(requests[i].params === undefined) {
var methodName = requests[i].shift();
requests[i] = this.buildRequest(methodName, requests[i]);
}
requests[i].id = i;
}
this.calls++;
this.requests += requests.length;
options = $.extend({
server: localStorage["server"]
, session: sessionID
, success: function success(data){
if (options.ajax.async){
for(var i = 0; i < data.length; i++){
performAsyncCallback(data[i], requests[data[i].id]);
}
} else {
out = data;
}
}
, error: function error(data){
console.error("AJAX event failed: " + JSON.stringify(data));
if (options.ajax.async){
for(var i = 0; i < data.length; i++){
if(typeof(requests[data[i].id].fail) == "function"){
requests[data[i].id].fail(data[i]);
}
}
} else {
out = data;
}
}
, ajax: {}
}, options);
options.ajax = $.extend({
type: 'POST'
,url: options.server + moduleName
,data: JSON.stringify(requests)
,success: options.success
,error: options.error
,async: false
}, options.ajax);
if (options.ajax.async){
$.ajax(options.ajax);
} else {
$.ajax(options.ajax);
for(var i = 0; i < out.length; i++){
// Call callbacks asynchronously :3
setTimeout(performAsyncCallback(out[i], requests[out[i].id]), 1);
}
}
localStorage["sessionActivity"] = new Date().getTime();
return out;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment