Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. export var Api = {
  2.  
  3. initializationStarted: false,
  4. pendingRequests: 0,
  5.  
  6. prepareInitialization() {
  7. Api.initializationStarted = false;
  8. Api.pendingRequests = 0;
  9. },
  10.  
  11. initializationCompleted() {
  12. return Api.initializationStarted && Api.pendingRequests === 0;
  13. },
  14.  
  15. post(context, path, data, onSuccess, onError, options) {
  16. Api.request(context, path, data, 'POST', onSuccess, onError, options);
  17. },
  18.  
  19. get(context, path, onSuccess, onError, options) {
  20. Api.request(context, path, {}, 'GET', onSuccess, onError, options);
  21. },
  22.  
  23. async syncGet(context, path, onSuccess, onError) {
  24. Api.pendingRequests = Api.pendingRequests + 1;
  25. Api.initializationStarted = true;
  26.  
  27. const url = process.env.VUE_APP_API_URL + path;
  28. await context.$http.get(url).then(response => {
  29. context.errorResponse = undefined;
  30. context.submitSuccess = true;
  31. window.console.debug('Request success');
  32. Api.pendingRequests = Api.pendingRequests - 1;
  33. if (typeof onSuccess === 'function') {
  34. onSuccess(response.body);
  35. }
  36. }, errorResponse => {
  37. context.errorResponse = errorResponse.body;
  38. context.submitSuccess = false;
  39. window.console.error('Request failed: ' + errorResponse.body.message);
  40. Api.pendingRequests = Api.pendingRequests - 1;
  41. if (typeof onError === 'function') {
  42. onError(errorResponse.body);
  43. }
  44. });
  45. },
  46.  
  47. getWithParam(context, path, data, onSuccess, onError, options) {
  48. Api.request(context, path, data, 'GET', onSuccess, onError, options);
  49. },
  50.  
  51. put(context, path, data, onSuccess, onError, options) {
  52. Api.request(context, path, data, 'PUT', onSuccess, onError, options);
  53. },
  54.  
  55. delete(context, path, data, onSuccess, onError, options) {
  56. Api.request(context, path, data, 'DELETE', onSuccess, onError, options);
  57. },
  58.  
  59. postMultipart(context, path, file, data, onSuccess, onError, options) {
  60. options = options ? options : {};
  61. options.headers = options.headers ? options.headers : {};
  62. options.headers['Content-Type'] = 'multipart/form-data';
  63.  
  64. let formData = new FormData();
  65. formData.set('file', file);
  66.  
  67. for (const [key, value] of Object.entries(data)) {
  68. formData.set(key, value);
  69. }
  70.  
  71. Api.post(context, path, formData, onSuccess, onError, options);
  72. },
  73.  
  74. request(context, path, data, method, onSuccess, onError, options) {
  75. Api.pendingRequests = Api.pendingRequests + 1;
  76. Api.initializationStarted = true;
  77.  
  78. const url = process.env.VUE_APP_API_URL + path;
  79.  
  80. window.console.debug('Sending response to: ' + url);
  81. window.console.debug('Body: ' + JSON.stringify(data));
  82. const successHandler = response => {
  83. context.errorResponse = undefined;
  84. context.submitSuccess = true;
  85. window.console.debug('Request success');
  86. Api.pendingRequests = Api.pendingRequests - 1;
  87. if (typeof onSuccess === 'function') {
  88. onSuccess(response.body);
  89. }
  90. };
  91.  
  92. const errorHandler = errorResponse => {
  93. context.errorResponse = errorResponse.body;
  94. context.submitSuccess = false;
  95. window.console.error('Request failed: ' + errorResponse.body.message);
  96. Api.pendingRequests = Api.pendingRequests - 1;
  97. if (typeof onError === 'function') {
  98. onError(errorResponse.body);
  99. }
  100. };
  101.  
  102. if (method === 'POST') {
  103. context.$http.post(url, data, options).then(successHandler, errorHandler);
  104. } else if (method === 'GET') {
  105. context.$http.get(url, {params: data}, options).then(successHandler, errorHandler);
  106. } else if (method === 'DELETE') {
  107. context.$http.delete(url, {params: data}, options).then(successHandler, errorHandler);
  108. } else if (method === 'PUT') {
  109. context.$http.put(url, data, options).then(successHandler, errorHandler);
  110. }
  111. },
  112.  
  113.  
  114. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement