backend_srv.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import config from 'app/core/config';
  5. import coreModule from 'app/core/core_module';
  6. export class BackendSrv {
  7. inFlightRequests = {};
  8. HTTP_REQUEST_CANCELLED = -1;
  9. /** @ngInject */
  10. constructor(private $http, private alertSrv, private $rootScope, private $q, private $timeout) {
  11. }
  12. get(url, params?) {
  13. return this.request({ method: 'GET', url: url, params: params });
  14. }
  15. delete(url) {
  16. return this.request({ method: 'DELETE', url: url });
  17. }
  18. post(url, data) {
  19. return this.request({ method: 'POST', url: url, data: data });
  20. };
  21. patch(url, data) {
  22. return this.request({ method: 'PATCH', url: url, data: data });
  23. }
  24. put(url, data) {
  25. return this.request({ method: 'PUT', url: url, data: data });
  26. }
  27. requestErrorHandler(err) {
  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. this.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. this.alertSrv.set("Problem!", data.message, data.severity, 10000);
  45. }
  46. throw data;
  47. }
  48. request(options) {
  49. options.retry = options.retry || 0;
  50. var requestIsLocal = options.url.indexOf('/') === 0;
  51. var firstAttempt = options.retry === 0;
  52. if (requestIsLocal && !options.hasSubUrl) {
  53. options.url = config.appSubUrl + options.url;
  54. options.hasSubUrl = true;
  55. }
  56. return this.$http(options).then(results => {
  57. if (options.method !== 'GET') {
  58. if (results && results.data.message) {
  59. if (options.showSuccessAlert !== false) {
  60. this.alertSrv.set(results.data.message, '', 'success', 3000);
  61. }
  62. }
  63. }
  64. return results.data;
  65. }, err => {
  66. // handle unauthorized
  67. if (err.status === 401 && firstAttempt) {
  68. return this.loginPing().then(() => {
  69. options.retry = 1;
  70. return this.request(options);
  71. });
  72. }
  73. this.$timeout(this.requestErrorHandler.bind(this, err), 50);
  74. throw err;
  75. });
  76. };
  77. datasourceRequest(options) {
  78. options.retry = options.retry || 0;
  79. // A requestID is provided by the datasource as a unique identifier for a
  80. // particular query. If the requestID exists, the promise it is keyed to
  81. // is canceled, canceling the previous datasource request if it is still
  82. // in-flight.
  83. var canceler;
  84. if (options.requestId) {
  85. canceler = this.inFlightRequests[options.requestId];
  86. if (canceler) {
  87. canceler.resolve();
  88. }
  89. // create new canceler
  90. canceler = this.$q.defer();
  91. options.timeout = canceler.promise;
  92. this.inFlightRequests[options.requestId] = canceler;
  93. }
  94. var requestIsLocal = options.url.indexOf('/') === 0;
  95. var firstAttempt = options.retry === 0;
  96. if (requestIsLocal && !options.hasSubUrl && options.retry === 0) {
  97. options.url = config.appSubUrl + options.url;
  98. }
  99. if (requestIsLocal && options.headers && options.headers.Authorization) {
  100. options.headers['X-DS-Authorization'] = options.headers.Authorization;
  101. delete options.headers.Authorization;
  102. }
  103. return this.$http(options).catch(err => {
  104. if (err.status === this.HTTP_REQUEST_CANCELLED) {
  105. throw {err, cancelled: true};
  106. }
  107. // handle unauthorized for backend requests
  108. if (requestIsLocal && firstAttempt && err.status === 401) {
  109. return this.loginPing().then(() => {
  110. options.retry = 1;
  111. if (canceler) {
  112. canceler.resolve();
  113. }
  114. return this.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. response: err.data,
  122. };
  123. }
  124. // for Prometheus
  125. if (!err.data.message && _.isString(err.data.error)) {
  126. err.data.message = err.data.error;
  127. }
  128. throw err;
  129. }).finally(() => {
  130. // clean up
  131. if (options.requestId) {
  132. delete this.inFlightRequests[options.requestId];
  133. }
  134. });
  135. };
  136. loginPing() {
  137. return this.request({url: '/api/login/ping', method: 'GET', retry: 1 });
  138. }
  139. search(query) {
  140. return this.get('/api/search', query);
  141. }
  142. getDashboard(type, slug) {
  143. return this.get('/api/dashboards/' + type + '/' + slug);
  144. }
  145. saveDashboard(dash, options) {
  146. options = (options || {});
  147. return this.post('/api/dashboards/db/', {dashboard: dash, overwrite: options.overwrite === true});
  148. }
  149. }
  150. coreModule.service('backendSrv', BackendSrv);