4 rpcSessionID
= L
.env
.sessionid
|| '00000000000000000000000000000000',
5 rpcBaseURL
= L
.url('admin/ubus');
7 return L
.Class
.extend({
8 call: function(req
, cb
) {
11 if (Array
.isArray(req
)) {
13 return Promise
.resolve([]);
15 for (var i
= 0; i
< req
.length
; i
++)
16 q
+= '%s%s.%s'.format(
23 q
+= '/%s.%s'.format(req
.params
[1], req
.params
[2]);
26 return L
.Request
.post(rpcBaseURL
+ q
, req
, {
27 timeout
: (L
.env
.rpctimeout
|| 5) * 1000,
32 handleListReply: function(req
, msg
) {
33 var list
= msg
.result
;
35 /* verify message frame */
36 if (typeof(msg
) != 'object' || msg
.jsonrpc
!= '2.0' || !msg
.id
|| !Array
.isArray(list
))
42 handleCallReply: function(req
, res
) {
43 var type
= Object
.prototype.toString
,
47 L
.error('RPCError', 'RPC call failed with HTTP error %d: %s',
48 res
.status
, res
.statusText
|| '?');
52 /* fetch response attribute and verify returned type */
55 /* verify message frame */
56 if (typeof(msg
) == 'object' && msg
.jsonrpc
== '2.0') {
57 if (typeof(msg
.error
) == 'object' && msg
.error
.code
&& msg
.error
.message
)
58 req
.reject(new Error('RPC call failed with error %d: %s'
59 .format(msg
.error
.code
, msg
.error
.message
|| '?')));
60 else if (Array
.isArray(msg
.result
) && msg
.result
[0] == 0)
61 ret
= (msg
.result
.length
> 1) ? msg
.result
[1] : msg
.result
[0];
64 req
.reject(new Error('Invalid message frame received'));
68 for (var key
in req
.expect
) {
69 if (ret
!= null && key
!= '')
72 if (ret
== null || type
.call(ret
) != type
.call(req
.expect
[key
]))
73 ret
= req
.expect
[key
];
80 if (typeof(req
.filter
) == 'function') {
82 req
.priv
[1] = req
.params
;
83 ret
= req
.filter
.apply(this, req
.priv
);
94 params
: arguments
.length
? this.varargs(arguments
) : undefined
97 return this.call(msg
, this.handleListReply
);
100 declare: function(options
) {
101 return Function
.prototype.bind
.call(function(rpc
, options
) {
102 var args
= this.varargs(arguments
, 2);
103 return new Promise(function(resolveFn
, rejectFn
) {
104 /* build parameter object */
107 if (Array
.isArray(options
.params
))
108 for (p_off
= 0; p_off
< options
.params
.length
; p_off
++)
109 params
[options
.params
[p_off
]] = args
[p_off
];
111 /* all remaining arguments are private args */
112 var priv
= [ undefined, undefined ];
113 for (; p_off
< args
.length
; p_off
++)
114 priv
.push(args
[p_off
]);
116 /* store request info */
118 expect
: options
.expect
,
119 filter
: options
.filter
,
126 /* build message object */
140 rpc
.call(msg
, rpc
.handleCallReply
.bind(rpc
, req
));
142 }, this, this, options
);
145 getSessionID: function() {
149 setSessionID: function(sid
) {
153 getBaseURL: function() {
157 setBaseURL: function(url
) {