Merge branch '4598-crunch-failure-stats'
[arvados.git] / apps / workbench / app / assets / javascripts / arvados_client.js
1 angular.
2     module('Arvados', []).
3     service('ArvadosClient', ArvadosClient);
4
5 ArvadosClient.$inject = ['arvadosApiToken', 'arvadosDiscoveryUri']
6 function ArvadosClient(arvadosApiToken, arvadosDiscoveryUri) {
7     $.extend(this, {
8         apiPromise: apiPromise,
9         uniqueNameForManifest: uniqueNameForManifest
10     });
11     return this;
12     ////////////////////////////////
13
14     var that = this;
15     var promiseDiscovery;
16     var discoveryDoc;
17
18     function apiPromise(controller, action, params) {
19         // Start an API call. Return a promise that will resolve with
20         // the API response.
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);
27                 }
28             });
29             var path = meth.path.replace(/{(.*?)}/, function(_, key) {
30                 var val = data[key];
31                 delete data[key];
32                 return encodeURIComponent(val);
33             });
34             return $.ajax({
35                 url: discoveryDoc.baseUrl + path,
36                 type: 'POST',
37                 crossDomain: true,
38                 dataType: 'json',
39                 data: data,
40                 headers: {
41                     Authorization: 'OAuth2 ' + arvadosApiToken
42                 }
43             });
44         });
45     }
46
47     function uniqueNameForManifest(manifest, newStreamName, origName) {
48         // Return an (escaped) filename starting with (unescaped)
49         // origName that won't conflict with any existing names in the
50         // manifest if saved under newStreamName. newStreamName must
51         // be exactly as given in the manifest, e.g., "." or "./foo"
52         // or "./foo/bar".
53         //
54         // Example:
55         //
56         // uniqueNameForManifest('./foo [...] 0:0:bar\\040baz.txt\n', '.',
57         //                       'foo/bar baz.txt')
58         // =>
59         // 'foo/bar\\040baz\\040(1).txt'
60         var newName;
61         var nameStub = origName;
62         var suffixInt = null;
63         var ok = false;
64         var lineMatch, linesRe = /(\S+).*/gm;
65         var fileTokenMatch, fileTokensRe = / \d+:\d+:(\S+)/g;
66         while (!ok) {
67             ok = true;
68             // Add ' (N)' before the filename extension, if any.
69             newName = (!suffixInt ? nameStub :
70                        nameStub.replace(/(\.[^.]*)?$/, ' ('+suffixInt+')$1')).
71                 replace(/ /g, '\\040');
72             while (ok && null !==
73                    (lineMatch = linesRe.exec(manifest))) {
74                 // lineMatch is [theEntireLine, streamName]
75                 while (ok && null !==
76                        (fileTokenMatch = fileTokensRe.exec(lineMatch[0]))) {
77                     // fileTokenMatch is [theEntireToken, fileName]
78                     if (lineMatch[1] + '/' + fileTokenMatch[1]
79                         ===
80                         newStreamName + '/' + newName) {
81                         ok = false;
82                     }
83                 }
84             }
85             suffixInt = (suffixInt || 0) + 1;
86         }
87         return newName;
88     }
89
90     function getDiscoveryDoc() {
91         if (!promiseDiscovery) {
92             promiseDiscovery = $.ajax({
93                 url: arvadosDiscoveryUri,
94                 crossDomain: true
95             }).then(function(data, status, xhr) {
96                 discoveryDoc = data;
97             });
98         }
99         return promiseDiscovery;
100     }
101 }