UploadedFile should support a callback for upload progress
[WebKit-https.git] / Websites / perf.webkit.org / public / shared / common-remote.js
1 "use strict";
2
3 class CommonRemoteAPI {
4     postJSON(path, data, options)
5     {
6         return this._asJSON(this.sendHttpRequest(path, 'POST', 'application/json', JSON.stringify(data || {}), options));
7     }
8
9     postJSONWithStatus(path, data, options)
10     {
11         return this._checkStatus(this.postJSON(path, data, options));
12     }
13
14     postFormData(path, data, options)
15     {
16         const formData = new FormData();
17         for (let key in data)
18             formData.append(key, data[key]);
19         return this._asJSON(this.sendHttpRequestWithFormData(path, formData, options));
20     }
21
22     postFormDataWithStatus(path, data, options)
23     {
24         return this._checkStatus(this.postFormData(path, data, options));
25     }
26
27     getJSON(path)
28     {
29         return this._asJSON(this.sendHttpRequest(path, 'GET', null, null));
30     }
31
32     getJSONWithStatus(path)
33     {
34         return this._checkStatus(this.getJSON(path));
35     }
36
37     sendHttpRequest(path, method, contentType, content, options = {})
38     {
39         throw 'NotImplemented';
40     }
41
42     sendHttpRequestWithFormData(path, formData, options = {})
43     {
44         throw 'NotImplemented';
45     }
46
47     _asJSON(promise)
48     {
49         return promise.then((result) => {
50             try {
51                 return JSON.parse(result.responseText);
52             } catch (error) {
53                 console.error(result.responseText);
54                 throw `{result.statusCode}: ${error}`;
55             }
56         });
57     }
58
59     _checkStatus(promise)
60     {
61         return promise.then(function (content) {
62             if (content['status'] != 'OK')
63                 throw content['status'];
64             return content;
65         });
66     }
67 }
68
69 if (typeof module != 'undefined')
70     module.exports.CommonRemoteAPI = CommonRemoteAPI;