| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- 'use strict';
- angular.module('etcd', [])
- .factory('EtcdV2', ['$http', function($http) {
- var keyPrefix = '/v2/keys/'
- var statsPrefix = '/v2/stats/'
- var baseURL = '/v2/'
- delete $http.defaults.headers.common['X-Requested-With'];
- function cleanupPath(path) {
- var parts = path.split('/');
- if (parts.length === 0) {
- return '';
- }
- parts = parts.filter(function(v){return v!=='';});
- parts = parts.join('/');
- return parts
- }
- function newKey(keyName) {
- var self = {};
- self.name = cleanupPath(keyName);
- self.getParent = function() {
- var parts = self.name.split('/');
- if (parts.length === 0) {
- return newKey('');
- }
- parts.pop();
- return newKey(parts.join('/'));
- };
- self.path = function() {
- var path = '/' + cleanupPath(keyPrefix + self.name);
- if (path === keyPrefix.substring(0, keyPrefix.length - 1)) {
- return keyPrefix
- }
- return path
- };
- self.get = function() {
- return $http.get(self.path());
- };
- self.set = function(keyValue) {
- return $http({
- url: self.path(),
- data: $.param({value: keyValue}),
- method: 'PUT',
- headers: {'Content-Type': 'application/x-www-form-urlencoded'}
- });
- };
- self.deleteKey = function(keyValue) {
- return $http({
- url: self.path(),
- method: 'DELETE',
- headers: {'Content-Type': 'application/x-www-form-urlencoded'}
- });
- };
- return self;
- }
- function newStat(statName) {
- var self = {};
- self.name = cleanupPath(statName);
- self.path = function() {
- return '/' + cleanupPath(statsPrefix + self.name);
- };
- self.get = function() {
- return $http.get(self.path());
- };
- return self
- }
- return {
- getStat: newStat,
- getKey: newKey
- }
- }]);
|