backendSrv.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 && firstAttempt) {
  51. options.url = config.appSubUrl + options.url;
  52. }
  53. return $http(options).then(function(results) {
  54. if (options.method !== 'GET') {
  55. if (results && results.data.message) {
  56. alertSrv.set(results.data.message, '', 'success', 3000);
  57. }
  58. }
  59. return results.data;
  60. }, function(err) {
  61. // handle unauthorized
  62. if (err.status === 401 && firstAttempt) {
  63. return self.loginPing().then(function() {
  64. options.retry = 1;
  65. return self.request(options);
  66. });
  67. }
  68. $timeout(self._handleError(err), 50);
  69. throw err;
  70. });
  71. };
  72. this.datasourceRequest = function(options) {
  73. options.retry = options.retry || 0;
  74. var requestIsLocal = options.url.indexOf('/') === 0;
  75. var firstAttempt = options.retry === 0;
  76. if (requestIsLocal && firstAttempt) {
  77. options.url = config.appSubUrl + options.url;
  78. }
  79. return $http(options).then(null, function(err) {
  80. // handle unauthorized for backend requests
  81. if (requestIsLocal && firstAttempt && err.status === 401) {
  82. return self.loginPing().then(function() {
  83. options.retry = 1;
  84. return self.datasourceRequest(options);
  85. });
  86. }
  87. throw err;
  88. });
  89. };
  90. this.loginPing = function() {
  91. return this.request({url: '/api/login/ping', method: 'GET', retry: 1 });
  92. };
  93. this.search = function(query) {
  94. return this.get('/api/search', query);
  95. };
  96. this.getDashboard = function(slug) {
  97. return this.get('/api/dashboards/db/' + slug);
  98. };
  99. this.saveDashboard = function(dash, options) {
  100. options = (options || {});
  101. return this.post('/api/dashboards/db/', {dashboard: dash, overwrite: options.overwrite === true});
  102. };
  103. });
  104. });