backend_srv.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. define([
  2. 'angular',
  3. 'lodash',
  4. '../core_module',
  5. 'app/core/config',
  6. ],
  7. function (angular, _, coreModule, config) {
  8. 'use strict';
  9. coreModule.default.service('backendSrv', function($http, alertSrv, $timeout, $q) {
  10. var self = this;
  11. this.get = function(url, params) {
  12. return this.request({ method: 'GET', url: url, params: params });
  13. };
  14. this.delete = function(url) {
  15. return this.request({ method: 'DELETE', url: url });
  16. };
  17. this.post = function(url, data) {
  18. return this.request({ method: 'POST', url: url, data: data });
  19. };
  20. this.patch = function(url, data) {
  21. return this.request({ method: 'PATCH', url: url, data: data });
  22. };
  23. this.put = function(url, data) {
  24. return this.request({ method: 'PUT', url: url, data: data });
  25. };
  26. this._handleError = function(err) {
  27. return function() {
  28. if (err.isHandled) {
  29. return;
  30. }
  31. var data = err.data || { message: 'Unexpected error' };
  32. if (_.isString(data)) {
  33. data = { message: data };
  34. }
  35. if (err.status === 422) {
  36. alertSrv.set("Validation failed", data.message, "warning", 4000);
  37. throw data;
  38. }
  39. data.severity = 'error';
  40. if (err.status < 500) {
  41. data.severity = "warning";
  42. }
  43. if (data.message) {
  44. alertSrv.set("Problem!", data.message, data.severity, 10000);
  45. }
  46. throw data;
  47. };
  48. };
  49. this.request = function(options) {
  50. options.retry = options.retry || 0;
  51. var requestIsLocal = options.url.indexOf('/') === 0;
  52. var firstAttempt = options.retry === 0;
  53. if (requestIsLocal && !options.hasSubUrl) {
  54. options.url = config.appSubUrl + options.url;
  55. options.hasSubUrl = true;
  56. }
  57. return $http(options).then(function(results) {
  58. if (options.method !== 'GET') {
  59. if (results && results.data.message) {
  60. alertSrv.set(results.data.message, '', 'success', 3000);
  61. }
  62. }
  63. return results.data;
  64. }, function(err) {
  65. // handle unauthorized
  66. if (err.status === 401 && firstAttempt) {
  67. return self.loginPing().then(function() {
  68. options.retry = 1;
  69. return self.request(options);
  70. });
  71. }
  72. $timeout(self._handleError(err), 50);
  73. throw err;
  74. });
  75. };
  76. var datasourceInFlightRequests = {};
  77. var HTTP_REQUEST_ABORTED = -1;
  78. this.datasourceRequest = function(options) {
  79. options.retry = options.retry || 0;
  80. // A requestID is provided by the datasource as a unique identifier for a
  81. // particular query. If the requestID exists, the promise it is keyed to
  82. // is canceled, canceling the previous datasource request if it is still
  83. // in-flight.
  84. var canceler;
  85. if (options.requestID) {
  86. if (canceler = datasourceInFlightRequests[options.requestID]) {
  87. canceler.resolve();
  88. }
  89. canceler = $q.defer();
  90. options.timeout = canceler.promise;
  91. datasourceInFlightRequests[options.requestID] = canceler;
  92. }
  93. var requestIsLocal = options.url.indexOf('/') === 0;
  94. var firstAttempt = options.retry === 0;
  95. if (requestIsLocal && options.headers && options.headers.Authorization) {
  96. options.headers['X-DS-Authorization'] = options.headers.Authorization;
  97. delete options.headers.Authorization;
  98. }
  99. return $http(options).then(null, function(err) {
  100. if (err.status === HTTP_REQUEST_ABORTED) {
  101. // TODO: Hitting refresh before the original request returns cancels
  102. // the "loading" animation on the panes, but it should continue to be
  103. // visible.
  104. err.statusText = "request aborted";
  105. return err;
  106. }
  107. // handle unauthorized for backend requests
  108. if (requestIsLocal && firstAttempt && err.status === 401) {
  109. return self.loginPing().then(function() {
  110. options.retry = 1;
  111. if (canceler) {
  112. canceler.resolve();
  113. }
  114. return self.datasourceRequest(options);
  115. });
  116. }
  117. //populate error obj on Internal Error
  118. if (_.isString(err.data) && err.status === 500) {
  119. err.data = {
  120. error: err.statusText
  121. };
  122. }
  123. // for Prometheus
  124. if (!err.data.message && _.isString(err.data.error)) {
  125. err.data.message = err.data.error;
  126. }
  127. throw err;
  128. });
  129. };
  130. this.loginPing = function() {
  131. return this.request({url: '/api/login/ping', method: 'GET', retry: 1 });
  132. };
  133. this.search = function(query) {
  134. return this.get('/api/search', query);
  135. };
  136. this.getDashboard = function(type, slug) {
  137. return this.get('/api/dashboards/' + type + '/' + slug);
  138. };
  139. this.saveDashboard = function(dash, options) {
  140. options = (options || {});
  141. return this.post('/api/dashboards/db/', {dashboard: dash, overwrite: options.overwrite === true});
  142. };
  143. });
  144. });