-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager-api.js
63 lines (54 loc) · 1.41 KB
/
manager-api.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
import { HTTP } from 'meteor/http';
export const name = 'smaltcreation:manager-api';
export class Client {
constructor({ protocol = 'https', serverIp, serverPort = 80, serviceName, servicePassword }) {
this.protocol = protocol;
this.serverIp = serverIp;
this.serverPort = serverPort;
this.serviceName = serviceName;
this.servicePassword = servicePassword;
this.authToken = null;
this.userId = null;
}
getUrl(route) {
return `${this.protocol}://${this.serverIp}:${this.serverPort}/api/${route}`;
}
getAuthenticationHeader() {
return {
'X-Auth-Token': this.authToken,
'X-User-Id': this.userId,
};
}
logIn(callback) {
HTTP.post(this.getUrl('login'), {
data: {
username: this.serviceName,
password: this.servicePassword
}
}, (error, result) => {
if (!error) {
this.authToken = result.data.data.authToken;
this.userId = result.data.data.userId;
}
callback(error);
});
}
logOut(callback) {
HTTP.post(this.getUrl('logout'), {
headers: this.getAuthenticationHeader()
}, error => {
callback(error);
});
}
addChartPoint(chartId, yValue, callback) {
HTTP.post(this.getUrl('chartPoints'), {
headers: this.getAuthenticationHeader(),
data: {
chartId: parseInt(chartId),
y: yValue
}
}, error => {
callback(error);
});
}
}