-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollabConnector.ts
253 lines (219 loc) · 6.83 KB
/
CollabConnector.ts
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**---LICENSE-BEGIN - DO NOT CHANGE OR MOVE THIS HEADER
* This file is part of the Neurorobotics Platform software
* Copyright (C) 2014,2015,2016,2017 Human Brain Project
* https://www.humanbrainproject.eu
*
* The Human Brain Project is a European Commission funded project
* in the frame of the Horizon2020 FET Flagship plan.
* http://ec.europa.eu/programmes/horizon2020/en/h2020-section/fet-flagships
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* ---LICENSE-END**/
'use strict';
const q = require('q'),
_ = require('lodash'),
path = require('path');
// mocked in the tests
// tslint:disable-next-line: prefer-const
let request = require('request-promise');
// wraps the collab connection
export default class CollabConnector {
static get REQUEST_TIMEOUT() {
return 30 * 1000;
} // ms
static get COLLAB_API_URL() {
return 'https://services.humanbrainproject.eu/storage/v1/api';
}
static get instance() {
return this._instance;
}
private static _instance = new CollabConnector();
private _getMemoizedCollabs?;
private _getMemoizedCollab?;
handleError(err) {
console.error(`[Collab error] ${err}`);
const errType = Object.prototype.toString.call(err).slice(8, -1);
if (errType === 'Object' && err.statusCode) {
if (
err.statusCode === 403 ||
(err.statusCode === 401 &&
(err.message.indexOf('OpenId response: token is not valid') >= 0 ||
err.message.indexOf('invalid_token') >= 0))
)
return q.reject({
code: 477,
msg:
'https://services.humanbrainproject.eu/oidc/authorize?prompt=login&response_type=token'
});
} else if (errType === 'String') {
err = `[Collab error] ${err}`;
}
return q.reject(err);
}
executeRequest(options, token) {
_.extend(options, {
resolveWithFullResponse: true,
timeout: CollabConnector.REQUEST_TIMEOUT,
headers: { Authorization: 'Bearer ' + token }
});
return request(options)
.then(res => {
if (res.statusCode < 200 || res.statusCode >= 300)
throw 'Status code: ' + res.statusCode;
if (options.encoding === null)
return { headers: res.headers, body: res.body };
return res.body;
})
.catch(this.handleError);
}
post(url, data, token, jsonType = false) {
const operation = () =>
this.executeRequest(
{
method: 'POST',
uri: url,
body: data,
json: !!jsonType
},
token
);
return operation().catch(e => {
if (e.message && e.message === 'Error: ESOCKETTIMEDOUT')
return operation();
throw e;
});
}
get(url, token) {
return this.executeRequest(
{
method: 'GET',
uri: url
},
token
);
}
delete(url, token) {
return this.executeRequest(
{
method: 'DELETE',
uri: url
},
token
);
}
getCollabEntity(token, collabId) {
if (!this._getMemoizedCollabs)
this._getMemoizedCollabs = _.memoize(
this.getEntity,
(token, collabId) => collabId
);
return this._getMemoizedCollabs(token, collabId);
}
getEntity(token, collabId, ...entityPath) {
if (!collabId) return q.reject('No collab id specified');
const fullpath = encodeURIComponent(
path.join('/', collabId + '', entityPath.join('/'))
);
const COLLAB_STORAGE_URL = `${CollabConnector.COLLAB_API_URL}/entity/?path=${fullpath}`;
return this.get(COLLAB_STORAGE_URL, token).then(res => JSON.parse(res));
}
projectFolders(token, project) {
return this.jsonApi(token, 'project', project, 'children').then(res =>
res.results.map(f => ({
uuid: f.uuid,
name: f.name,
parent: f.parent
}))
);
}
createFile(token, parent, name, contentType) {
const COLLAB_FILE_URL = `${CollabConnector.COLLAB_API_URL}/file/`;
return this.post(
COLLAB_FILE_URL,
{
name,
parent,
content_type: contentType
},
token,
true
);
}
deleteEntity(token, folder, entityUuid, type = 'file') {
const COLLAB_FILE_URL = `${CollabConnector.COLLAB_API_URL}/${type}/${entityUuid}/`;
return this.delete(COLLAB_FILE_URL, token);
}
createFolder(token, parent, name) {
const COLLAB_FILE_URL = `${CollabConnector.COLLAB_API_URL}/folder/`;
return this.post(
COLLAB_FILE_URL,
{
name,
parent
},
token,
true
);
}
uploadContent(token, entityUuid, content) {
const COLLAB_FILE_URL = `${CollabConnector.COLLAB_API_URL}/file/${entityUuid}/content/upload/`;
return this.post(COLLAB_FILE_URL, content, token).then(() => ({
uuid: entityUuid
}));
}
folderContent(token, folder) {
return this.jsonApi(token, 'folder', folder, 'children').then(res =>
res.results.map(f => ({
uuid: f.uuid,
name: f.name,
parent: f.parent,
contentType: f.content_type,
type: f.entity_type,
modifiedOn: f.modified_on
}))
);
}
entityContent(token, entityUuid) {
const COLLAB_ENTITY_URL = `${CollabConnector.COLLAB_API_URL}/file/${entityUuid}/content/`;
return this.executeRequest(
{
method: 'GET',
uri: COLLAB_ENTITY_URL,
encoding: null
},
token
);
}
jsonApi(token, entityType, entityUuid, requestType) {
return this.api(token, entityType, entityUuid, requestType).then(res =>
JSON.parse(res)
);
}
api(token, entityType, entityUuid, requestType) {
const COLLAB_ENTITY_URL = `${CollabConnector.COLLAB_API_URL}/${entityType}/${entityUuid}/${requestType}/`;
return this.get(COLLAB_ENTITY_URL, token);
}
getContextIdCollab(token, contextId) {
if (!this._getMemoizedCollab) {
this._getMemoizedCollab = _.memoize((token, contextId) => {
const COLLAB_URL = `https://services.humanbrainproject.eu/collab/v0/collab/context/${contextId}/`;
return this.get(COLLAB_URL, token)
.then(res => JSON.parse(res))
.then(({ collab: { id } }) => id);
}, (token, contextId) => token + contextId);
}
return this._getMemoizedCollab(token, contextId);
}
}