3 service('ArvadosClient', ArvadosClient);
5 ArvadosClient.$inject = ['arvadosApiToken', 'arvadosDiscoveryUri']
6 function ArvadosClient(arvadosApiToken, arvadosDiscoveryUri) {
8 apiPromise: apiPromise,
9 uniqueNameForManifest: uniqueNameForManifest
12 ////////////////////////////////
18 function apiPromise(controller, action, params) {
19 // Start an API call. Return a promise that will resolve with
21 return getDiscoveryDoc().then(function() {
22 var meth = discoveryDoc.resources[controller].methods[action];
23 var data = $.extend({}, params, {_method: meth.httpMethod});
24 $.each(data, function(k, v) {
25 if (typeof(v) == 'object') {
26 data[k] = JSON.stringify(v);
29 var path = meth.path.replace(/{(.*?)}/, function(_, key) {
32 return encodeURIComponent(val);
35 url: discoveryDoc.baseUrl + path,
41 Authorization: 'OAuth2 ' + arvadosApiToken
47 function uniqueNameForManifest(manifest, streamName, origName) {
48 // Return an (escaped) filename starting with (unescaped)
49 // origName that won't conflict with any existing names in
50 // the manifest if saved under streamName. streamName must
51 // be exactly as given in the manifest, e.g., "." or
52 // "./foo" or "./foo/bar".
56 // unique('./foo [...] 0:0:bar\040baz\n', '.', 'foo/bar baz')
58 // 'foo/bar\\040baz\\040(1)'
60 var nameStub = origName;
65 // Add ' (N)' before the filename extension, if any.
66 newName = (!suffixInt ? nameStub :
67 nameStub.replace(/(\.[^.]*)?$/, ' ('+suffixInt+')$1')).
68 replace(/ /g, '\\040');
69 $.each(manifest.split('\n'), function(_, line) {
70 var i, match, foundName;
71 var toks = line.split(' ');
72 for (var i=1; i<toks.length && ok; i++)
73 if (match = toks[i].match(/^\d+:\d+:(\S+)/))
74 if (toks[0] + '/' + match[1] === streamName + '/' + newName) {
75 suffixInt = (suffixInt || 0) + 1;
83 function getDiscoveryDoc() {
84 if (!promiseDiscovery) {
85 promiseDiscovery = $.ajax({
86 url: arvadosDiscoveryUri,
88 }).then(function(data, status, xhr) {
92 return promiseDiscovery;