/
carriots.js
135 lines (110 loc) · 3.03 KB
/
carriots.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
this is a module to call the carriots iot api
$http.defaultHeaders['carriots.apiKey'] = 'xxxxxxxxxxxxx';
sample post:
var samplePostReq={
host: 'api.carriots.com',
path: '/streams/',
port: '80',
protocol: "v1",
checksum: "",
device: "defaultDevice@afitterling.afitterling",
at: "now",
method:'POST',
params: {a: 3}, // werden zu url params umgewandelt
data: {a: 1}
};
$http(samplePostReq).then(function(data){
// success
console.log('success', data);
});
var sampleGetReq={
host: 'api.carriots.com',
path: '/streams/',
port: '80',
method: 'GET',
params: {_t: 'str', device: 'Commander@afitterling.afitterling'}
};
$http(sampleGetReq).then(function(data){
// success
console.log('success', data);
});
$http(sampleGetReq).then(function(data){
// success
console.log('success', data);
$http(samplePostReq).then(function(data){
// success
console.log('success', data);
});
});
*/
var defaultHeaders = {'Content-Type': 'application/json'};
var debug = false;
function _parseParams(params){
if (!params) return '';
var _paramsString=''; var separator;
Object.keys(params).forEach(function(key,index) {
if (index===0){separator='?'} else {separator='&'}
_paramsString = _paramsString + separator + key + '=' + params[key];
});
return _paramsString;
}
function $http(config) {
var content = null;
var data = null;
var _safeJSONParse = function(data){
return new Promise(function(resolve, reject){
try { resolve(JSON.parse(data)) } catch(e){ reject(e) }
});
};
var httpOptions = {
host: config.host,
path: config.path + _parseParams(config.params),
port: config.port,
method: config.method,
headers: $http.defaultHeaders
};
if (config.method==='GET' || config.method==='DELETE'){
httpOptions.headers['Content-Length'] = 0;
}
if (config.method==='UPDATE' || config.method==='POST'){
data = {};
data.device = config.device;
data.protocol = config.protocol;
data.checksum = config.checksum || null;
data.at = config.at;
// the actual cariots request api data
data.data = config.data;
content = JSON.stringify(data);
httpOptions.headers['Content-Length'] = content.length;
}
if (debug){
console.log('httpOptions=', httpOptions, 'data=', data);
}
return new Promise(function(resolve, reject){
var req = require('http').request(httpOptions, function(res) {
var d = '';
res.on('data', function(data) { d+= data; });
res.on('error', function(e) {
return reject(e)
});
res.on('close', function() {
_safeJSONParse(d).then(function(data){
if (debug) console.log('resolve=', data);
return resolve(data);
}, function(e){
if (debug) console.log('reject=', e);
return reject(e)
});
});
}).end(content)
});
}
$http.defaultHeaders = defaultHeaders;
$http.debug = function(val){
debug = val;
};
exports = {
defaultHeaders: defaultHeaders,
http: $http
};