1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
/*
Script: Rpc.js
A JSON-RPC proxy built ontop of mootools.
*
* Copyright (C) Damien Churchill 2008 <damoxc@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, write to:
* The Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301, USA.
*
Class: JSON.RPC
Class to create a proxy to a json-rpc interface on a server.
Example:
client = new JSON.RPC('/json/rpc');
client.hello_world({
onSuccess: function(result) {
alert(result);
}
});
alert(client.hello_world({async: false;}));
client.add_name('Damien', {
onSuccess: function(result) {
alert(result);
}
});
Returns:
The proxy that can be used to directly call methods on the server.
*/
JSON.RPC = new Class({
Implements: Options,
options: {
async: true,
methods: []
},
initialize: function(url, options) {
this.setOptions(options)
this.url = url
if (this.options.methods.length == 0) {
this._execute('system.listMethods', {async: false}).each(function(method) {
this[method] = function() {
var options = this._parseargs(arguments)
return this._execute(method, options)
}.bind(this)
}, this)
}
},
/*
Property: _parseargs
Internal method for parsing the arguments given to the method
Arguments:
args - A list of the methods arguments
Returns:
An options object with the arguments set as options.params
*/
_parseargs: function(args) {
var params = $A(args), options = params.getLast()
if ($type(options) == 'object') {
var option_keys = ['async', 'onRequest', 'onComplete',
'onSuccess', 'onFailure', 'onException', 'onCancel'], keys =
new Hash(options).getKeys(), is_option = false
option_keys.each(function(key) {
if (keys.contains(key)) {
is_option = true
}
})
if (is_option) {
params.erase(options)
} else {
options = {}
}
} else { options = {} }
options.params = params
return options
},
/*
Property: _execute
An internal method to make the call to the rpc page
Arguements:
method - the name of the method
options - An options dict providing any additional options for the
call.
Example:
alert(client.hello_world({async: false;}));
Returns:
If not async returns the json result
*/
_execute: function(method, options) {
options = $pick(options, {})
options.params = $pick(options.params, [])
options.async = $pick(options.async, this.options.async)
data = JSON.encode({
method: method,
params: options.params,
id: 1
})
var request = new Request.JSON({
url: this.url,
async: options.async,
onRequest: options.onRequest,
onComplete: options.onComplete,
onSuccess: function(response) {
if (options.onSuccess) {options.onSuccess(response.result)}
},
onFailure: options.onFailure,
onException: options.onException,
onCancel: options.onCancel
}).send(data)
if (!options.async) {
return request.response.json.result
}
}
})
|