-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathangular-json-rpc.js
67 lines (53 loc) · 1.47 KB
/
angular-json-rpc.js
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
angular.module('angular-json-rpc', []);
//
// json rpc for angular js. JSON-RPC-2.0 compatible.
// spec - http://www.jsonrpc.org/specification
//
var jsonRpc = angular.module('angular-json-rpc', []).factory("jsonRpc", ['$http', function($http) {
this.version = "2.0";
this.url = null;
//
// setup rpc
//
this.setup = function(params){
// check params
check_params(params);
this.url = params.url;
return this;
}
success = function(data, status, headers, config){
}
error = function(data, status, headers, config){
}
//
// json-rpc request
//
this.request = function(method, options){
if(options === undefined)
options = { id: 1 };
if (options.id === undefined)
options.id = 1;
// make request
var bodyRequest = JSON.stringify({"jsonrpc": this.version, "method": method, "params": options.params, "id" : options.id});
var headers = {'Content-Type': 'application/json'};
$http({'url': this.url, 'method': 'POST', 'data' : bodyRequest, 'headers': headers})
.success(function (data, status, headers, config){
return data;
}).error(function (data, status, headers, config) {
return data;
});
// return
return true;
}
//
// Check params
//
check_params = function(params){
if (params == undefined){
throw("Wrong params");
}
if (typeof(params.url) !== 'string' || params.url == '')
throw("Wrong url parameter");
}
return this;
}]);