IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

make-arvados-request.js 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import wbApplyPromiseOrdering from 'wb-apply-promise-ordering';
  2. const requestPromiseOrdering = {};
  3. function makeArvadosRequest(arvHost, arvToken, endpoint, params={}) {
  4. const defaultParams = {
  5. 'method': 'GET',
  6. 'data': null,
  7. 'contentType': 'application/json;charset=utf-8',
  8. 'responseType': 'json',
  9. 'useSsl': true,
  10. 'requireToken': true,
  11. 'onProgress': () => {},
  12. 'promiseOrdering': true,
  13. 'expectedStatus': 200
  14. };
  15. Object.keys(defaultParams).map(k => (params[k] =
  16. (k in params ? params[k] : defaultParams[k])));
  17. let { method, data, contentType, responseType,
  18. useSsl, requireToken, onProgress, promiseOrdering,
  19. expectedStatus } = params;
  20. if (!(arvHost && (arvToken || !requireToken)))
  21. return new Promise((accept, reject) => reject());
  22. let xhr = new XMLHttpRequest();
  23. xhr.open(method, (useSsl ? 'https://' : 'http://') + arvHost + endpoint);
  24. if (arvToken)
  25. xhr.setRequestHeader('Authorization', 'OAuth2 ' + arvToken);
  26. if (data !== null)
  27. xhr.setRequestHeader('Content-Type', contentType);
  28. xhr.responseType = responseType;
  29. xhr.onprogress = onProgress;
  30. let prom = new Promise((accept, reject) => {
  31. xhr.onreadystatechange = () => {
  32. if (xhr.readyState !== 4)
  33. return;
  34. if ((expectedStatus instanceof Array) &&
  35. expectedStatus.indexOf(xhr.status) !== -1) {
  36. accept(xhr);
  37. } else if (expectedStatus === xhr.status) {
  38. accept(xhr);
  39. } else {
  40. reject(xhr);
  41. }
  42. };
  43. xhr.send(data);
  44. });
  45. if (promiseOrdering)
  46. prom = wbApplyPromiseOrdering(prom, requestPromiseOrdering);
  47. return prom;
  48. }
  49. export default makeArvadosRequest;