backendSrv.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'config',
  5. ],
  6. function (angular, _, config) {
  7. 'use strict';
  8. var module = angular.module('grafana.services');
  9. module.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.put = function(url, data) {
  21. return this.request({ method: 'PUT', url: url, data: data });
  22. };
  23. this._handleError = function(err) {
  24. return function() {
  25. if (err.isHandled) {
  26. return;
  27. }
  28. if (err.status === 422) {
  29. alertSrv.set("Validation failed", "", "warning", 4000);
  30. throw err.data;
  31. }
  32. var data = err.data || { message: 'Unexpected error' };
  33. if (_.isString(data)) {
  34. data = { message: data };
  35. }
  36. data.severity = 'error';
  37. if (err.status < 500) {
  38. data.severity = "warning";
  39. }
  40. if (data.message) {
  41. alertSrv.set("Problem!", data.message, data.severity, 10000);
  42. }
  43. throw data;
  44. };
  45. };
  46. this.request = function(options) {
  47. options.retry = options.retry || 0;
  48. var requestIsLocal = options.url.indexOf('/') === 0;
  49. var firstAttempt = options.retry === 0;
  50. if (requestIsLocal && !options.hasSubUrl) {
  51. options.url = config.appSubUrl + options.url;
  52. options.hasSubUrl = true;
  53. }
  54. return $http(options).then(function(results) {
  55. if (options.method !== 'GET') {
  56. if (results && results.data.message) {
  57. alertSrv.set(results.data.message, '', 'success', 3000);
  58. }
  59. }
  60. return results.data;
  61. }, function(err) {
  62. // handle unauthorized
  63. if (err.status === 401 && firstAttempt) {
  64. return self.loginPing().then(function() {
  65. options.retry = 1;
  66. return self.request(options);
  67. });
  68. }
  69. $timeout(self._handleError(err), 50);
  70. throw err;
  71. });
  72. };
  73. this.datasourceRequest = function(options) {
  74. options.retry = options.retry || 0;
  75. var requestIsLocal = options.url.indexOf('/') === 0;
  76. var firstAttempt = options.retry === 0;
  77. return $http(options).then(null, function(err) {
  78. // handle unauthorized for backend requests
  79. if (requestIsLocal && firstAttempt && err.status === 401) {
  80. return self.loginPing().then(function() {
  81. options.retry = 1;
  82. return self.datasourceRequest(options);
  83. });
  84. }
  85. throw err;
  86. });
  87. };
  88. this.loginPing = function() {
  89. return this.request({url: '/api/login/ping', method: 'GET', retry: 1 });
  90. };
  91. this.search = function(query) {
  92. return this.get('/api/search', query);
  93. };
  94. this.getDashboard = function(slug) {
  95. return this.get('/api/dashboards/db/' + slug);
  96. };
  97. this.saveDashboard = function(dash, options) {
  98. options = (options || {});
  99. return this.post('/api/dashboards/db/', {dashboard: dash, overwrite: options.overwrite === true});
  100. };
  101. });
  102. });