etcd.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 'use strict';
  2. angular.module('etcd', [])
  3. .factory('EtcdV1', ['$http', function($http) {
  4. var keyPrefix = '/v1/keys/'
  5. var statsPrefix = '/v1/stats/'
  6. var baseURL = '/v1/'
  7. delete $http.defaults.headers.common['X-Requested-With'];
  8. function cleanupPath(path) {
  9. var parts = path.split('/');
  10. if (parts.length === 0) {
  11. return '';
  12. }
  13. parts = parts.filter(function(v){return v!=='';});
  14. return parts.join('/');
  15. }
  16. function newKey(keyName) {
  17. var self = {};
  18. self.name = cleanupPath(keyName);
  19. self.getParent = function() {
  20. var parts = self.name.split('/');
  21. if (parts.length === 0) {
  22. return newKey('');
  23. }
  24. parts.pop();
  25. return newKey(parts.join('/'));
  26. };
  27. self.path = function() {
  28. return '/' + cleanupPath(keyPrefix + self.name);
  29. };
  30. self.get = function() {
  31. return $http.get(self.path());
  32. };
  33. self.set = function(keyValue) {
  34. return $http({
  35. url: self.path(),
  36. data: $.param({value: keyValue}),
  37. method: 'POST',
  38. headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  39. });
  40. };
  41. self.deleteKey = function(keyValue) {
  42. return $http({
  43. url: self.path(),
  44. method: 'DELETE',
  45. headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  46. });
  47. };
  48. return self;
  49. }
  50. function newStat(statName) {
  51. var self = {};
  52. self.name = cleanupPath(statName);
  53. self.path = function() {
  54. return '/' + cleanupPath(statsPrefix + self.name);
  55. };
  56. self.get = function() {
  57. return $http.get(self.path());
  58. };
  59. return self
  60. }
  61. return {
  62. getStat: newStat,
  63. getKey: newKey
  64. }
  65. }]);