flavien peyre 93ebbc4dfd Config host and config Template pages
Change-Id: I3b22147685b401f9424bf4f80334bf2b99da71d4
2015-08-10 12:21:34 -04:00

56 lines
1.8 KiB
JavaScript

/*global jQuery */
'use strict';
angular.module('bansho.surveil')
.service('surveilConfig', ['$http', '$q','surveilQuery', 'componentsConfig', 'surveilApiConfig',
function ($http, $q, surveilQuery, componentsConfig, surveilApiConfig) {
var executeQuery = function (url, method, query) {
return $http({
url: url,
method: method,
data: query
}).error(function () {
throw new Error('executeQuery : ' + method + ' Request failed');
});
};
var getData = function (fields, filters, endpoint) {
var promise = $q.defer();
if (!queryEndpoint[endpoint]) {
throw new Error('getData in surveilConfig : Invalid endpoint ' + endpoint);
}
queryEndpoint[endpoint](fields, filters, function (data) {
promise.resolve(data);
});
return promise.promise;
};
var queryEndpoint = {
"hosts": function (fields, filters, callback) {
var hostQuery = surveilQuery(fields, filters.hosts),
method = 'POST',
hostUrl = surveilApiConfig.endpoint('config') + '/hosts/';
executeQuery(hostUrl, method, hostQuery)
.success(function (hosts) {
var response = [];
angular.forEach(hosts, function (host) {
response.push(host);
});
callback(response);
});
}
};
return {
getData: getData
};
}]);