backend_srv.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. this.alertSrv.set(results.data.message, '', 'success', 3000);
  60. }
  61. }
  62. return results.data;
  63. }, err => {
  64. // handle unauthorized
  65. if (err.status === 401 && firstAttempt) {
  66. return this.loginPing().then(() => {
  67. options.retry = 1;
  68. return this.request(options);
  69. });
  70. }
  71. this.$timeout(this.requestErrorHandler.bind(this, err), 50);
  72. throw err;
  73. });
  74. };
  75. datasourceRequest(options) {
  76. options.retry = options.retry || 0;
  77. // A requestID is provided by the datasource as a unique identifier for a
  78. // particular query. If the requestID exists, the promise it is keyed to
  79. // is canceled, canceling the previous datasource request if it is still
  80. // in-flight.
  81. var canceler;
  82. if (options.requestId) {
  83. canceler = this.inFlightRequests[options.requestId];
  84. if (canceler) {
  85. canceler.resolve();
  86. }
  87. // create new canceler
  88. canceler = this.$q.defer();
  89. options.timeout = canceler.promise;
  90. this.inFlightRequests[options.requestId] = canceler;
  91. }
  92. var requestIsLocal = options.url.indexOf('/') === 0;
  93. var firstAttempt = options.retry === 0;
  94. if (requestIsLocal && options.headers && options.headers.Authorization) {
  95. options.headers['X-DS-Authorization'] = options.headers.Authorization;
  96. delete options.headers.Authorization;
  97. }
  98. return this.$http(options).catch(err => {
  99. if (err.status === this.HTTP_REQUEST_CANCELLED) {
  100. throw {err, cancelled: true};
  101. }
  102. // handle unauthorized for backend requests
  103. if (requestIsLocal && firstAttempt && err.status === 401) {
  104. return this.loginPing().then(() => {
  105. options.retry = 1;
  106. if (canceler) {
  107. canceler.resolve();
  108. }
  109. return this.datasourceRequest(options);
  110. });
  111. }
  112. //populate error obj on Internal Error
  113. if (_.isString(err.data) && err.status === 500) {
  114. err.data = {
  115. error: err.statusText
  116. };
  117. }
  118. // for Prometheus
  119. if (!err.data.message && _.isString(err.data.error)) {
  120. err.data.message = err.data.error;
  121. }
  122. throw err;
  123. }).finally(() => {
  124. // clean up
  125. if (options.requestId) {
  126. delete this.inFlightRequests[options.requestId];
  127. }
  128. });
  129. };
  130. loginPing() {
  131. return this.request({url: '/api/login/ping', method: 'GET', retry: 1 });
  132. }
  133. search(query) {
  134. return this.get('/api/search', query);
  135. }
  136. getDashboard(type, slug) {
  137. return this.get('/api/dashboards/' + type + '/' + slug);
  138. }
  139. saveDashboard(dash, options) {
  140. options = (options || {});
  141. return this.post('/api/dashboards/db/', {dashboard: dash, overwrite: options.overwrite === true});
  142. }
  143. }
  144. coreModule.service('backendSrv', BackendSrv);