/
premCodec.js
63 lines (58 loc) · 1.37 KB
/
premCodec.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
const request = require('superagent');
const xml2jsParser = require('superagent-xml2jsparser');
const { registerFeedbackXml, deregisterFeedback } = require('./utils');
class Codec {
constructor({ username, password, ip }) {
this.username = username;
this.password = password;
this.ip = ip;
}
async registerFeedback(url) {
let res;
try {
res = await request.post(`http://${this.ip}/putxml`)
.auth(this.username, this.password)
.accept('json')
.type('xml')
.send(registerFeedbackXml(url))
return res
}
catch (e) {
console.error('Error in registering feedback')
throw e
}
}
async deregisterFeedback(url) {
let res;
try {
res = await request.post(`http://${this.ip}/putxml`)
.auth(this.username, this.password)
.accept('json')
.type('xml')
.send(deregisterFeedbackXml(url))
return res
}
catch (e) {
console.error('Error in registering feedback')
throw e
}
}
async getStatus() {
let res;
try {
res = await request.get(`http://${this.ip}/status.xml`)
.auth(this.username, this.password)
.accept('xml')
.buffer(true)
.parse(xml2jsParser)
return res;
}
catch (e) {
console.error('Error getting status')
throw e
}
}
}
module.exports = {
Codec
};