- import wbApplyPromiseOrdering from 'wb-apply-promise-ordering';
-
- const requestPromiseOrdering = {};
-
- function makeArvadosRequest(arvHost, arvToken, endpoint, params={}) {
- const defaultParams = {
- 'method': 'GET',
- 'data': null,
- 'contentType': 'application/json;charset=utf-8',
- 'responseType': 'json',
- 'useSsl': true,
- 'requireToken': true,
- 'onProgress': () => {},
- 'promiseOrdering': true,
- 'expectedStatus': 200
- };
-
- Object.keys(defaultParams).map(k => (params[k] =
- (k in params ? params[k] : defaultParams[k])));
- let { method, data, contentType, responseType,
- useSsl, requireToken, onProgress, promiseOrdering,
- expectedStatus } = params;
-
- if (!(arvHost && (arvToken || !requireToken)))
- return new Promise((accept, reject) => reject());
-
- let xhr = new XMLHttpRequest();
- xhr.open(method, (useSsl ? 'https://' : 'http://') + arvHost + endpoint);
- if (arvToken)
- xhr.setRequestHeader('Authorization', 'OAuth2 ' + arvToken);
- if (data !== null)
- xhr.setRequestHeader('Content-Type', contentType);
- xhr.responseType = responseType;
-
- xhr.onprogress = onProgress;
-
- let prom = new Promise((accept, reject) => {
- xhr.onreadystatechange = () => {
- if (xhr.readyState !== 4)
- return;
-
- if ((expectedStatus instanceof Array) &&
- expectedStatus.indexOf(xhr.status) !== -1) {
- accept(xhr);
- } else if (expectedStatus === xhr.status) {
- accept(xhr);
- } else {
- reject(xhr);
- }
- };
- xhr.send(data);
- });
-
- if (promiseOrdering)
- prom = wbApplyPromiseOrdering(prom, requestPromiseOrdering);
-
- return prom;
- }
-
- export default makeArvadosRequest;
|