3781: Merge branch 'master' into 3781-browser-upload
[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, 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".
53         //
54         // Example:
55         //
56         // unique('./foo [...] 0:0:bar\040baz\n', '.', 'foo/bar baz')
57         // =>
58         // 'foo/bar\\040baz\\040(1)'
59         var newName;
60         var nameStub = origName;
61         var suffixInt = null;
62         var ok = false;
63         while (!ok) {
64             ok = true;
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;
76                             ok = false;
77                         }
78             });
79         }
80         return newName;
81     }
82
83     function getDiscoveryDoc() {
84         if (!promiseDiscovery) {
85             promiseDiscovery = $.ajax({
86                 url: arvadosDiscoveryUri,
87                 crossDomain: true
88             }).then(function(data, status, xhr) {
89                 discoveryDoc = data;
90             });
91         }
92         return promiseDiscovery;
93     }
94 }