[Checkins] SVN: z3c.jsonrpcproxy/trunk/ Implemented JSON-RPC 2.0
specification. Use JSON-RPC 2.0 version as default.
Roger Ineichen
roger at projekt01.ch
Sat Aug 2 08:51:47 EDT 2008
Log message for revision 89217:
Implemented JSON-RPC 2.0 specification. Use JSON-RPC 2.0 version as default.
Changed:
U z3c.jsonrpcproxy/trunk/CHANGES.txt
U z3c.jsonrpcproxy/trunk/src/z3c/jsonrpcproxy/js/z3c.jsonrpcproxy-0.5.0.js
-=-
Modified: z3c.jsonrpcproxy/trunk/CHANGES.txt
===================================================================
--- z3c.jsonrpcproxy/trunk/CHANGES.txt 2008-08-02 12:51:33 UTC (rev 89216)
+++ z3c.jsonrpcproxy/trunk/CHANGES.txt 2008-08-02 12:51:46 UTC (rev 89217)
@@ -5,8 +5,11 @@
Version 0.5.1dev (unreleased)
-----------------------------
-- ...
+- Implemented JSON-RPC 2.0 specification. Use JSON-RPC 2.0 version as default.
+ Optional the version 1.0 and 1.1 can be set. See JSON-RPC 2.0 specification
+ for more information.
+
Version 0.5.0 (2008-04-16)
-------------------------
Modified: z3c.jsonrpcproxy/trunk/src/z3c/jsonrpcproxy/js/z3c.jsonrpcproxy-0.5.0.js
===================================================================
--- z3c.jsonrpcproxy/trunk/src/z3c/jsonrpcproxy/js/z3c.jsonrpcproxy-0.5.0.js 2008-08-02 12:51:33 UTC (rev 89216)
+++ z3c.jsonrpcproxy/trunk/src/z3c/jsonrpcproxy/js/z3c.jsonrpcproxy-0.5.0.js 2008-08-02 12:51:46 UTC (rev 89217)
@@ -2,19 +2,24 @@
/**
* @fileoverview JSON-RPC client implementation
* @author Roger Ineichen dev at projekt01 dot ch
- * @version Initial, not documented
+ * @version 0.6, supports JSON-RPC 1.0, 1.1 and 2.0
*/
//----------------------------------------------------------------------------
-function JSONRPC(url) {
+function JSONRPC(url, version) {
this._url = url;
+ // uses specification version 2.0 by default
+ this._version = '2.0'
+ if (typeof(version) != 'undefined') {
+ this._version = version;
+ }
this._methods = new Array();
this._user = null;
this._password = null;
}
-function getJSONRPCProxy(url) {
- return new JSONRPC(url);
+function getJSONRPCProxy(url, version) {
+ return new JSONRPC(url, version);
}
JSONRPC.prototype.addMethod = function(name, callback, requestId) {
@@ -23,7 +28,7 @@
}
var self = this;
if(!self[name]){
- var method = new JSONRPCMethod(this._url, name, callback, requestId, this._user, this._password);
+ var method = new JSONRPCMethod(this._url, name, callback, requestId, this._user, this._password, this._version);
self[name] = method;
this._methods.push(method);
}
@@ -37,25 +42,36 @@
}
}
-function JSONRPCMethod(url, methodName, callback, requestId, user, pass) {
+function JSONRPCMethod(url, methodName, callback, requestId, user, pass, version) {
this.methodName = methodName;
this.callback = callback;
this.requestId = requestId;
this.url = url;
this.user = user;
this.password = pass;
+ this.version = version
var self = this;
var fn = function(){
- var args = new Array();
- for(var i=0;i<arguments.length;i++){
- args.push(arguments[i]);
+ var oldVersion = false;
+ if (this.version == '1.0' || this.version == '1.1') {
+ oldVersion = true;
}
+ if (!oldVersion && arguments.length == 1 && typeof arguments[0] === "object"){
+ // we've got version 2.0 and an associative array as argument
+ var args = arguments[0]
+ } else {
+ // we've got positional arguments
+ var args = new Array();
+ for(var i=0;i<arguments.length;i++){
+ args.push(arguments[i]);
+ }
+ }
if(self.callback) {
var data = self.jsonRequest(self.requestId, self.methodName, args);
self.postData(self.url, self.user, self.password, data, function(resp){
var res = null;
- var exc =null;
+ var exc = null;
try{
res = self.handleResponse(resp);
}catch(e){
@@ -64,7 +80,7 @@
try{
callback(res, self.requestId, exc);
}catch(e){
- alert("except callback");
+ alert("callback method error: " + e.message);
}
args = null;
resp = null;
@@ -77,7 +93,6 @@
}
}
return fn;
-
}
JSONRPCMethod.prototype.postData = function(url, user, pass, data, callback) {
@@ -99,7 +114,14 @@
var ji = toJSON(id);
var jm = toJSON(methodName);
var ja = toJSON(args);
- return '{"id":' + ji + ', "method":' + jm + ', "params":' + ja + "}";
+ var ver = this.version
+ if (ver == '1.0'){
+ return '{"id":' +ji+ ', "method":' +jm+ ', "params":' +ja+ "}";
+ }else if (ver == '1.1'){
+ return '{"version":"'+ver+'", "id":' +ji+ ', "method":' +jm+ ', "params":' +ja+ "}";
+ }else{
+ return '{"jsonrpc":"'+ver+'", "id":' +ji+ ', "method":' +jm+ ', "params":' +ja+ "}";
+ }
}
JSONRPCMethod.prototype.setAuthentication = function(user, pass){
@@ -117,6 +139,9 @@
}
JSONRPCMethod.prototype.handleResponse = function(resp){
+ // TODO: Implement better error handling support since we have error codes
+ // offer an argument onError which defines a function for custom
+ // error handling.
var status=null;
try{
status = resp.status;
@@ -132,9 +157,16 @@
alert("The server responded with an empty document.");
}else{
var res = this.unmarshall(respTxt);
- if(res.error != null){
- alert("error " + res.error);
+ var oldVersion = false;
+ if (this.version == '1.0' || this.version == '1.1') {
+ oldVersion = true;
}
+ if(oldVersion && res.error != null){
+ alert(res.error);
+ }
+ else if(!oldVersion && res.error != null){
+ alert(res.error.message);
+ }
else if (res.requestId != self.requestId) {
alert("wrong json id returned");
}
More information about the Checkins
mailing list