backend_srv.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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) {
  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. this.datasourceRequest = function(options) {
  77. options.retry = options.retry || 0;
  78. var requestIsLocal = options.url.indexOf('/') === 0;
  79. var firstAttempt = options.retry === 0;
  80. if (requestIsLocal && options.headers && options.headers.Authorization) {
  81. options.headers['X-DS-Authorization'] = options.headers.Authorization;
  82. delete options.headers.Authorization;
  83. }
  84. return $http(options).then(null, function(err) {
  85. // handle unauthorized for backend requests
  86. if (requestIsLocal && firstAttempt && err.status === 401) {
  87. return self.loginPing().then(function() {
  88. options.retry = 1;
  89. return self.datasourceRequest(options);
  90. });
  91. }
  92. //populate error obj on Internal Error
  93. if (_.isString(err.data) && err.status === 500) {
  94. err.data = {
  95. error: err.statusText
  96. };
  97. }
  98. // for Prometheus
  99. if (!err.data.message && _.isString(err.data.error)) {
  100. err.data.message = err.data.error;
  101. }
  102. throw err;
  103. });
  104. };
  105. this.loginPing = function() {
  106. return this.request({url: '/api/login/ping', method: 'GET', retry: 1 });
  107. };
  108. this.search = function(query) {
  109. return this.get('/api/search', query);
  110. };
  111. this.getDashboard = function(type, slug) {
  112. return this.get('/api/dashboards/' + type + '/' + slug);
  113. };
  114. this.saveDashboard = function(dash, options) {
  115. options = (options || {});
  116. return this.post('/api/dashboards/db/', {dashboard: dash, overwrite: options.overwrite === true});
  117. };
  118. });
  119. });