-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode_helper.js
101 lines (94 loc) · 3.83 KB
/
node_helper.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
const request = require('request');
var NodeHelper = require('node_helper')
var pathlib = require('path');
const api_base_url = '/index.php/apps/deck/api/v1.0'
function requestNcDeckApi(host, user, pass, path, query, callback, error_callback) {
const options = {
url: host + pathlib.join(api_base_url, path) + query,
method: 'GET',
headers: {
'OCS-APIRequest': 'true',
'Content-Type': 'application/json'
},
auth: {
'user': user,
'pass': pass
}
};
//console.log("REQUEST(" + options.method + "): " + options.url)
try {
request.get(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
callback(JSON.parse(body))
} else {
if (error) {
error_callback(error, response, body);
}
}
});
} catch (e) {
console.error(e)
error_callback(e, { "statusCode": "NonHttp" }, { "message": "Nodejs Error" });
};
}
module.exports = NodeHelper.create({
requestNcDeckObjects: function (ncConfig, instanceId, path, callback) {
var self = this
//console.log(this.name + ": Request " + path + "@" + ncConfig.host)
requestNcDeckApi(ncConfig.host, ncConfig.user, ncConfig.pass, path, "", callback, function (error, response, body) {
var error_payload = {}
error_payload.moduleInstanceId = instanceId
if (response) {
switch (response.statusCode) {
case 401:
error_payload.message == "Nexcloud Login wrong?(401)";
break;
case 403:
error_payload.message == "You do not have permission for this Deckboard(403)";
break;
default:
error_payload.message == body.message;
}
}
else {
error_payload.message = error.toString()
}
console.error(self.name + ": " + error);
self.sendSocketNotification("NC_DECK_API_ERROR", error_payload);
});
},
sendBoardInfo: function (ncConfig, boardId, instanceId) {
const self = this;
var board = {}
board.boardId = boardId
var api_path_board = pathlib.join("boards", boardId.toString())
// https://deck.readthedocs.io/en/latest/API/#get-boardsboardid-get-board-details
this.requestNcDeckObjects(ncConfig, instanceId, api_path_board, function (response_body_board_info) {
board.data = response_body_board_info
board.moduleInstanceId = instanceId
self.sendSocketNotification("NC_DECK_BOARD_INFO", board);
});
},
sendBoardStacks: function (ncConfig, boardId, instanceId) {
const self = this;
var stacks = {}
stacks.boardId = boardId
var api_path_stacks = pathlib.join("boards", boardId.toString(), "stacks")
// https://deck.readthedocs.io/en/latest/API/#get-boardsboardidstacks-get-stacks
this.requestNcDeckObjects(ncConfig, instanceId, api_path_stacks, function (response_body_stacks) {
stacks.data = response_body_stacks
stacks.moduleInstanceId = instanceId
self.sendSocketNotification("NC_DECK_BOARD_STACKS", stacks);
});
},
socketNotificationReceived: function (notification, payload) {
switch (notification) {
case "GET_NC_DECK_BOARD_INFO":
this.sendBoardInfo(payload.ncConfig, payload.boardId, payload.instanceId);
break;
case "GET_NC_DECK_BOARD_STACKS":
this.sendBoardStacks(payload.ncConfig, payload.boardId, payload.instanceId);
break;
}
}
});