-
Notifications
You must be signed in to change notification settings - Fork 0
/
wxs.js
113 lines (109 loc) · 2.99 KB
/
wxs.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
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
/* jshint node:true */
var url = require('url');
var http = require('http');
var querystring = require("querystring");
function WXS(properties) {
this.wxs = properties;
if (this.wxs.restResource.indexOf('http://') !== 0) {
this.wxs.restResource = 'http://' + this.wxs.restResource;
}
this.auth = 'Basic ' + new Buffer(this.wxs.username + ':' + this.wxs.password).toString('base64');
this.parsed = url.parse(this.wxs.restResource);
console.log("wxsclient url=" + this.wxs.restResource);
console.log("wxsclient username=" + this.wxs.username);
console.log("wxsclient password=" + this.wxs.password);
}
WXS.prototype = {
put : function(key, value, callback) {
var post_options = {
hostname : this.parsed.hostname,
port : '80',
path : this.parsed.pathname + '/' + this.wxs.gridName + '/' + encodeURIComponent(key),
method : 'POST',
headers : {
'Content-Type' : 'application/json',
'Authorization' : this.auth
},
rejectUnauthorized : false,
agent : false,
};
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
});
res.on('error', function(c) {
console.log('post error: ' + c);
});
res.on('end', function() {
console.log('post status ' + res.statusCode);
callback();
});
});
post_req.write(JSON.stringify(value));
post_req.end();
},
get : function(key, callback) {
var get_options = {
hostname : this.parsed.hostname,
port : '80',
path : this.parsed.pathname + '/' + this.wxs.gridName + '/' + encodeURIComponent(key),
method : 'GET',
headers : {
'Content-Type' : 'application/json',
'Authorization' : this.auth
},
rejectUnauthorized : false,
agent : false,
};
var get_req = http.request(get_options, function(res) {
var resultString = '';
res.on('data', function(chunk) {
console.log('get response: ' + chunk);
resultString += chunk;
});
res.on('error', function(c) {
console.log('get error: ' + c);
});
res.on('end', function() {
console.log('get status ' + res.statusCode);
if (res.statusCode === 200) {
callback(JSON.parse(resultString));
} else {
callback(); // error case
}
});
});
get_req.end();
},
remove : function(key, callback) {
var get_options = {
hostname : this.parsed.hostname,
path : this.parsed.pathname + '/' + this.wxs.gridName + '/' +
encodeURIComponent(key),
port : '80',
method : 'DELETE',
headers : {
'Content-Type' : 'application/json',
'Authorization' : this.auth
},
rejectUnauthorized : false,
agent : false,
};
var get_req = http.request(get_options, function(res) {
var resultString = '';
res.on('data', function(chunk) {
console.log('get response: ' + chunk);
resultString += chunk;
});
res.on('error', function(c) {
console.log('get error: ' + c);
});
res.on('end', function() {
console.log('get status ' + res.statusCode);
callback();
});
});
get_req.end();
}
};
module.exports = WXS;