# HG changeset patch # User Thierry Florac # Date 1429022170 -7200 # Node ID d366c88a0f8895b2ebf8f1c9dd08c4ce3835597e # Parent 975b545f5ae5325372c29b4f64f5490d887cd081 Added JSON-RPC plug-in diff -r 975b545f5ae5 -r d366c88a0f88 src/pyams_skin/resources/js/ext/jquery-jsonrpc.js --- a/src/pyams_skin/resources/js/ext/jquery-jsonrpc.js Tue Apr 14 16:35:23 2015 +0200 +++ b/src/pyams_skin/resources/js/ext/jquery-jsonrpc.js Tue Apr 14 16:36:10 2015 +0200 @@ -1,36 +1,293 @@ -(function ($) { +(function($, undefined) { + $.extend({ + jsonRPC: { + // RPC Version Number + version: '2.0', + + // End point URL, sets default in requests if not + // specified with the request call + endPoint: null, + + // Default namespace for methods + namespace: null, + + /* + * Provides the RPC client with an optional default endpoint and namespace + * + * @param {object} The params object which can contain + * endPoint {string} The default endpoint for RPC requests + * namespace {string} The default namespace for RPC requests + * cache {boolean} If set to false, it will force requested + * pages not to be cached by the browser. Setting cache + * to false also appends a query string parameter, + * "_=[TIMESTAMP]", to the URL. (Default: true) + */ + setup: function(params) { + this._validateConfigParams(params); + this.endPoint = params.endPoint; + this.namespace = params.namespace; + this.cache = params.cache !== undefined ? params.cache : true; + return this; + }, + + /* + * Convenience wrapper method to allow you to temporarily set a config parameter + * (endPoint or namespace) and ensure it gets set back to what it was before + * + * @param {object} The params object which can contains + * endPoint {string} The default endpoint for RPC requests + * namespace {string} The default namespace for RPC requests + * @param {function} callback The function to call with the new params in place + */ + withOptions: function(params, callback) { + this._validateConfigParams(params); + // No point in running if there isn't a callback received to run + if(callback === undefined) throw("No callback specified"); + + origParams = {endPoint: this.endPoint, namespace: this.namespace}; + this.setup(params); + callback.call(this); + this.setup(origParams); + }, - $.jsonRpc = $.jsonRpc || function (options) { - options.type = options.type || 'GET'; - var ajaxOptions = { - contentType: 'application/json', - dataType: options.type == 'GET' ? 'jsonp' : 'json', - processData: options.type == 'GET' - }; + /* + * Performas a single RPC request + * + * @param {string} method The name of the rpc method to be called + * @param {object} options A collection of object which can contains + * params {array} the params array to send along with the request + * success {function} a function that will be executed if the request succeeds + * error {function} a function that will be executed if the request fails + * url {string} the url to send the request to + * id {string} the provenance id for this request (defaults to 1) + * cache {boolean} If set to false, it will force requested + * pages not to be cached by the browser. Setting cache + * to false also appends a query string parameter, + * "_=[TIMESTAMP]", to the URL. (Default: cache value + * set with the setup method) + * @return {undefined} + */ + request: function(method, options) { + if(options === undefined) { + options = { id: 1 }; + } + if (options.id === undefined) { + options.id = 1; + } + if (options.cache === undefined) { + options.cache = this.cache; + } + + // Validate method arguments + this._validateRequestMethod(method); + this._validateRequestParams(options.params); + this._validateRequestCallbacks(options.success, options.error); + + // Perform the actual request + this._doRequest(JSON.stringify(this._requestDataObj(method, options.params, options.id)), options); + + return true; + }, + + /* + * Submits multiple requests + * Takes an array of objects that contain a method and params + * + * @params {array} requests an array of request object which can contain + * method {string} the name of the method + * param {object} the params object to be sent with the request + * id {string} the provenance id for the request (defaults to an incrementer starting at 1) + * @param {object} options A collection of object which can contains + * success {function} a function that will be executed if the request succeeds + * error {function} a function that will be executed if the request fails + * url {string} the url to send the request to + * @return {undefined} + */ + batchRequest: function(requests, options) { + if(options === undefined) { + options = {}; + } + + // Ensure our requests come in as an array + if(!$.isArray(requests) || requests.length === 0) throw("Invalid requests supplied for jsonRPC batchRequest. Must be an array object that contain at least a method attribute"); + + // Make sure each of our request objects are valid + var _that = this; + $.each(requests, function(i, req) { + _that._validateRequestMethod(req.method); + _that._validateRequestParams(req.params); + if (req.id === undefined) { + req.id = i + 1; + } + }); + this._validateRequestCallbacks(options.success, options.error); + + var data = [], + request; + + // Prepare our request object + for(var i = 0; i 0 && json[0].jsonrpc !== '2.0') || + (!$.isArray(json) && json.jsonrpc !== '2.0')) { + throw 'Version error'; + } + + return json; + } + catch (e) { + return { + error: 'Internal server error: ' + e, + version: '2.0' + } + } + } + } + + } + }); })(jQuery); diff -r 975b545f5ae5 -r d366c88a0f88 src/pyams_skin/resources/js/ext/jquery-jsonrpc.min.js --- a/src/pyams_skin/resources/js/ext/jquery-jsonrpc.min.js Tue Apr 14 16:35:23 2015 +0200 +++ b/src/pyams_skin/resources/js/ext/jquery-jsonrpc.min.js Tue Apr 14 16:36:10 2015 +0200 @@ -1,1 +1,1 @@ -(function(a){a.jsonRpc=a.jsonRpc||function(c){c.type=c.type||"GET";var b={contentType:"application/json",dataType:c.type=="GET"?"jsonp":"json",processData:c.type=="GET"};var d={version:c.version||"1.0",method:c.method||"system.listMethods",params:c.params||[]};a.each(d,function(f){delete c[f]});function e(){c.data=JSON.stringify(d);if(c.type=="GET"){c.data={json:c.data}}a.ajax(a.extend(b,c))}if(typeof JSON=="undefined"){a.getScript("http://www.json.org/json2.js",function(){e()})}else{e()}return a}})(jQuery); \ No newline at end of file +(function($,undefined){$.extend({jsonRPC:{version:"2.0",endPoint:null,namespace:null,setup:function(params){this._validateConfigParams(params);this.endPoint=params.endPoint;this.namespace=params.namespace;this.cache=params.cache!==undefined?params.cache:true;return this},withOptions:function(params,callback){this._validateConfigParams(params);if(callback===undefined){throw ("No callback specified")}origParams={endPoint:this.endPoint,namespace:this.namespace};this.setup(params);callback.call(this);this.setup(origParams)},request:function(method,options){if(options===undefined){options={id:1}}if(options.id===undefined){options.id=1}if(options.cache===undefined){options.cache=this.cache}this._validateRequestMethod(method);this._validateRequestParams(options.params);this._validateRequestCallbacks(options.success,options.error);this._doRequest(JSON.stringify(this._requestDataObj(method,options.params,options.id)),options);return true},batchRequest:function(requests,options){if(options===undefined){options={}}if(!$.isArray(requests)||requests.length===0){throw ("Invalid requests supplied for jsonRPC batchRequest. Must be an array object that contain at least a method attribute")}var _that=this;$.each(requests,function(i,req){_that._validateRequestMethod(req.method);_that._validateRequestParams(req.params);if(req.id===undefined){req.id=i+1}});this._validateRequestCallbacks(options.success,options.error);var data=[],request;for(var i=0;i0&&json[0].jsonrpc!=="2.0")||(!$.isArray(json)&&json.jsonrpc!=="2.0")){throw"Version error"}return json}catch(e){return{error:"Internal server error: "+e,version:"2.0"}}}}}})})(jQuery); \ No newline at end of file