-
Notifications
You must be signed in to change notification settings - Fork 112
/
arvados_client.js
105 lines (99 loc) · 3.63 KB
/
arvados_client.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
// Copyright (C) The Arvados Authors. All rights reserved.
//
// SPDX-License-Identifier: AGPL-3.0
angular.
module('Arvados', []).
service('ArvadosClient', ArvadosClient);
ArvadosClient.$inject = ['arvadosApiToken', 'arvadosDiscoveryUri']
function ArvadosClient(arvadosApiToken, arvadosDiscoveryUri) {
$.extend(this, {
apiPromise: apiPromise,
uniqueNameForManifest: uniqueNameForManifest
});
return this;
////////////////////////////////
var that = this;
var promiseDiscovery;
var discoveryDoc;
function apiPromise(controller, action, params) {
// Start an API call. Return a promise that will resolve with
// the API response.
return getDiscoveryDoc().then(function() {
var meth = discoveryDoc.resources[controller].methods[action];
var data = $.extend({}, params, {_method: meth.httpMethod});
$.each(data, function(k, v) {
if (typeof(v) === 'object') {
data[k] = JSON.stringify(v);
}
});
var path = meth.path.replace(/{(.*?)}/, function(_, key) {
var val = data[key];
delete data[key];
return encodeURIComponent(val);
});
return $.ajax({
url: discoveryDoc.baseUrl + path,
type: 'POST',
crossDomain: true,
dataType: 'json',
data: data,
headers: {
Authorization: 'OAuth2 ' + arvadosApiToken
}
});
});
}
function uniqueNameForManifest(manifest, newStreamName, origName) {
// Return an (escaped) filename starting with (unescaped)
// origName that won't conflict with any existing names in the
// manifest if saved under newStreamName. newStreamName must
// be exactly as given in the manifest, e.g., "." or "./foo"
// or "./foo/bar".
//
// Example:
//
// uniqueNameForManifest('./foo [...] 0:0:bar\\040baz.txt\n', '.',
// 'foo/bar baz.txt')
// =>
// 'foo/bar\\040baz\\040(1).txt'
var newName;
var nameStub = origName;
var suffixInt = null;
var ok = false;
var lineMatch, linesRe = /(\S+).*/gm;
var fileTokenMatch, fileTokensRe = / \d+:\d+:(\S+)/g;
while (!ok) {
ok = true;
// Add ' (N)' before the filename extension, if any.
newName = (!suffixInt ? nameStub :
nameStub.replace(/(\.[^.]*)?$/, ' ('+suffixInt+')$1')).
replace(/ /g, '\\040');
while (ok && null !==
(lineMatch = linesRe.exec(manifest))) {
// lineMatch is [theEntireLine, streamName]
while (ok && null !==
(fileTokenMatch = fileTokensRe.exec(lineMatch[0]))) {
// fileTokenMatch is [theEntireToken, fileName]
if (lineMatch[1] + '/' + fileTokenMatch[1]
===
newStreamName + '/' + newName) {
ok = false;
}
}
}
suffixInt = (suffixInt || 0) + 1;
}
return newName;
}
function getDiscoveryDoc() {
if (!promiseDiscovery) {
promiseDiscovery = $.ajax({
url: arvadosDiscoveryUri,
crossDomain: true
}).then(function(data, status, xhr) {
discoveryDoc = data;
});
}
return promiseDiscovery;
}
}