1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
7 service('ArvadosClient', ArvadosClient);
9 ArvadosClient.$inject = ['arvadosApiToken', 'arvadosDiscoveryUri']
10 function ArvadosClient(arvadosApiToken, arvadosDiscoveryUri) {
12 apiPromise: apiPromise,
13 uniqueNameForManifest: uniqueNameForManifest
16 ////////////////////////////////
21 function apiPromise(controller, action, params) {
22 // Start an API call. Return a promise that will resolve with
24 return getDiscoveryDoc().then(function() {
25 var meth = discoveryDoc.resources[controller].methods[action];
26 var data = $.extend({}, params, {_method: meth.httpMethod});
27 $.each(data, function(k, v) {
28 if (typeof(v) === 'object') {
29 data[k] = JSON.stringify(v);
32 var path = meth.path.replace(/{(.*?)}/, function(_, key) {
35 return encodeURIComponent(val);
38 url: discoveryDoc.baseUrl + path,
44 Authorization: 'OAuth2 ' + arvadosApiToken
50 function uniqueNameForManifest(manifest, newStreamName, origName) {
51 // Return an (escaped) filename starting with (unescaped)
52 // origName that won't conflict with any existing names in the
53 // manifest if saved under newStreamName. newStreamName must
54 // be exactly as given in the manifest, e.g., "." or "./foo"
59 // uniqueNameForManifest('./foo [...] 0:0:bar\\040baz.txt\n', '.',
62 // 'foo/bar\\040baz\\040(1).txt'
64 var nameStub = origName;
67 var lineMatch, linesRe = /(\S+).*/gm;
68 var fileTokenMatch, fileTokensRe = / \d+:\d+:(\S+)/g;
71 // Add ' (N)' before the filename extension, if any.
72 newName = (!suffixInt ? nameStub :
73 nameStub.replace(/(\.[^.]*)?$/, ' ('+suffixInt+')$1')).
74 replace(/ /g, '\\040');
76 (lineMatch = linesRe.exec(manifest))) {
77 // lineMatch is [theEntireLine, streamName]
79 (fileTokenMatch = fileTokensRe.exec(lineMatch[0]))) {
80 // fileTokenMatch is [theEntireToken, fileName]
81 if (lineMatch[1] + '/' + fileTokenMatch[1]
83 newStreamName + '/' + newName) {
88 suffixInt = (suffixInt || 0) + 1;
93 function getDiscoveryDoc() {
94 if (!promiseDiscovery) {
95 promiseDiscovery = $.ajax({
96 url: arvadosDiscoveryUri,
98 }).then(function(data, status, xhr) {
102 return promiseDiscovery;