Commit af3e5980 authored by Patricio Bruna's avatar Patricio Bruna

Added BatchRequests and batchCountAccounts()

parent 307f31f0
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
- [Errors](#errors) - [Errors](#errors)
- [Zimbra Resources](#zimbra-resources) - [Zimbra Resources](#zimbra-resources)
- [Common Functions](#common-functions) - [Common Functions](#common-functions)
- [Batch Request Functions](#batch-request-functions)
- [Creating Resources](#creating-resources) - [Creating Resources](#creating-resources)
- [Modify Resources](#modify-resources) - [Modify Resources](#modify-resources)
- [Remove Resources](#remove-resources) - [Remove Resources](#remove-resources)
...@@ -161,6 +162,18 @@ zimbraApi.getAllAccounts(callback, query_object); ...@@ -161,6 +162,18 @@ zimbraApi.getAllAccounts(callback, query_object);
// Object {total: 29, more: false, account: Array[29]} // Object {total: 29, more: false, account: Array[29]}
``` ```
## Batch Request Functions
### Count Accounts for several Domains
Pass an array of domains ids or names and you get back an array of `countAccounts` responses
objects. The response arrays has the same order of the request array:
```javascript
var domains = ['zboxapp.com', 'example.com', 'zboxnow.com'];
zimbraApi.batchCountAccounts(domains, callback);
// [Object, Object];
```
## Creating Resources ## Creating Resources
The methods are: The methods are:
......
...@@ -196,18 +196,36 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -196,18 +196,36 @@ return /******/ (function(modules) { // webpackBootstrap
}, { }, {
key: 'buildRequest', key: 'buildRequest',
value: function buildRequest() { value: function buildRequest() {
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var request = null; var request = null;
this.client.getRequest({}, function (err, req) { this.client.getRequest(options, function (err, req) {
if (err) return error(err); if (err) return error(err);
request = req; request = req;
}); });
return request; return request;
} }
}, {
key: 'makeBatchRequest',
value: function makeBatchRequest(request_data_array, callback) {
var that = this;
if (request_data_array.length === 0) return;
var request_object = this.buildRequest({ isBatch: true });
request_data_array.forEach(function (request_data) {
request_object.addRequest(request_data.params, function (err, reqid) {
if (err) return that.handleError(err);
});
});
this.client.send(request_object, function (err, data) {
if (err) return callback(that.handleError(err));
that.parseBatchResponse(data, callback);
});
}
}, { }, {
key: 'makeRequest', key: 'makeRequest',
value: function makeRequest(request_data) { value: function makeRequest(request_data) {
var that = this; var that = this;
var request_object = that.buildRequest(); var request_object = this.buildRequest();
request_object.addRequest(request_data.params, function (err) { request_object.addRequest(request_data.params, function (err) {
if (err) { if (err) {
return that.handleError(err); return that.handleError(err);
...@@ -223,30 +241,34 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -223,30 +241,34 @@ return /******/ (function(modules) { // webpackBootstrap
value: function performRequest(request_data) { value: function performRequest(request_data) {
var _this = this; var _this = this;
var batch = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
if (this.client.token) { if (this.client.token) {
if (batch) return this.makeBatchRequest(request_data.requests, request_data.callback);
this.makeRequest(request_data); this.makeRequest(request_data);
} else { } else {
(function () { (function () {
var that = _this; var that = _this;
var getCallback = function getCallback(err, response) { var getCallback = function getCallback(err, response) {
if (err) return this.handleError(err); if (err) return this.handleError(err);
if (batch) return that.makeBatchRequest(request_data.requests, request_data.callback);
that.makeRequest(request_data); that.makeRequest(request_data);
}; };
_this.login(getCallback); _this.login(getCallback);
})(); })();
} }
} }
}, {
key: 'parseBatchResponse',
value: function parseBatchResponse(data, callback) {
var response_object = data.options.response.BatchResponse;
callback(null, response_object);
}
}, { }, {
key: 'parseCountAccountResponse', key: 'parseCountAccountResponse',
value: function parseCountAccountResponse(data, request_data, callback) { value: function parseCountAccountResponse(data, request_data, callback) {
var result = {};
var coses = data.get().CountAccountResponse.cos; var coses = data.get().CountAccountResponse.cos;
if (typeof coses !== 'undefined') coses.forEach(function (cos) { var result = this.dictionary.cosesToCountAccountObject(coses);
result[cos.name] = {
used: parseInt(cos._content),
id: cos.id
};
});
return callback(null, result); return callback(null, result);
} }
}, { }, {
...@@ -675,6 +697,37 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -675,6 +697,37 @@ return /******/ (function(modules) { // webpackBootstrap
this.performRequest(request_data); this.performRequest(request_data);
} }
// TODO: Fix this ugly FCKing Code
}, {
key: 'batchCountAccounts',
value: function batchCountAccounts(domains_ids, callback) {
var _this2 = this;
var that = this;
var request_data = {};
var result = [];
request_data.callback = function (err, data) {
if (err) return callback(err);
data.CountAccountResponse.forEach(function (r) {
var reqid = parseInt(r.requestId);
result[reqid - 1] = that.dictionary.cosesToCountAccountObject(r.cos);
});
return callback(null, result);
};
request_data.requests = [];
domains_ids.forEach(function (domain_id) {
var request = _this2.buildRequestData('CountAccount', callback);
request.parse_response = _this2.parseCountAccountResponse;
request.params.params.domain = {
'by': _this2.dictionary.byIdOrName(domain_id),
'_content': domain_id
};
request_data.requests.push(request);
});
this.performRequest(request_data, true);
}
// TODO: TO ugly // TODO: TO ugly
}, { }, {
...@@ -12423,6 +12476,18 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -12423,6 +12476,18 @@ return /******/ (function(modules) { // webpackBootstrap
}); });
return result; return result;
} }
}, {
key: 'cosesToCountAccountObject',
value: function cosesToCountAccountObject(coses) {
var result = {};
if (typeof coses !== 'undefined') coses.forEach(function (cos) {
result[cos.name] = {
used: parseInt(cos._content),
id: cos.id
};
});
return result;
}
}, { }, {
key: 'resourceResponseName', key: 'resourceResponseName',
value: function resourceResponseName(resource) { value: function resourceResponseName(resource) {
This diff is collapsed.
{ {
"name": "zimbra-admin-api-js", "name": "zimbra-admin-api-js",
"version": "0.0.16", "version": "0.0.17",
"private": true, "private": true,
"main": "lib/zimbra-admin-api.js", "main": "lib/zimbra-admin-api.js",
"dependencies": { "dependencies": {
......
...@@ -100,19 +100,34 @@ export default class ZimbraAdminApi { ...@@ -100,19 +100,34 @@ export default class ZimbraAdminApi {
}); });
} }
buildRequest() {
buildRequest(options = {}) {
let request = null; let request = null;
this.client.getRequest({}, (err, req) => { this.client.getRequest(options, (err, req) => {
if (err) return error(err); if (err) return error(err);
request = req; request = req;
}); });
return request; return request;
} }
makeBatchRequest(request_data_array, callback) {
const that = this;
if (request_data_array.length === 0) return;
let request_object = this.buildRequest({isBatch: true});
request_data_array.forEach((request_data) => {
request_object.addRequest(request_data.params, function(err, reqid){
if (err) return that.handleError(err);
});
});
this.client.send(request_object, function(err, data){
if (err) return callback(that.handleError(err));
that.parseBatchResponse(data, callback);
});
}
makeRequest(request_data) { makeRequest(request_data) {
const that = this; const that = this;
let request_object = that.buildRequest(); let request_object = this.buildRequest();
request_object.addRequest(request_data.params, function(err){ request_object.addRequest(request_data.params, function(err){
if (err) { if (err) {
return that.handleError(err); return that.handleError(err);
...@@ -124,28 +139,29 @@ export default class ZimbraAdminApi { ...@@ -124,28 +139,29 @@ export default class ZimbraAdminApi {
}); });
} }
performRequest(request_data) { performRequest(request_data, batch = false) {
if (this.client.token) { if (this.client.token) {
if (batch) return this.makeBatchRequest(request_data.requests, request_data.callback);
this.makeRequest(request_data); this.makeRequest(request_data);
} else { } else {
const that = this; const that = this;
let getCallback = function(err, response){ let getCallback = function(err, response){
if (err) return this.handleError(err); if (err) return this.handleError(err);
if (batch) return that.makeBatchRequest(request_data.requests, request_data.callback);
that.makeRequest(request_data); that.makeRequest(request_data);
}; };
this.login(getCallback); this.login(getCallback);
} }
} }
parseBatchResponse(data, callback) {
const response_object = data.options.response.BatchResponse;
callback(null, response_object);
}
parseCountAccountResponse(data, request_data, callback) { parseCountAccountResponse(data, request_data, callback) {
const result = {};
const coses = data.get().CountAccountResponse.cos; const coses = data.get().CountAccountResponse.cos;
if (typeof coses !== 'undefined') coses.forEach((cos) => { const result = this.dictionary.cosesToCountAccountObject(coses);
result[cos.name] = {
used: parseInt(cos._content),
id: cos.id
};
});
return callback(null, result); return callback(null, result);
} }
...@@ -478,6 +494,32 @@ export default class ZimbraAdminApi { ...@@ -478,6 +494,32 @@ export default class ZimbraAdminApi {
this.performRequest(request_data); this.performRequest(request_data);
} }
// TODO: Fix this ugly FCKing Code
batchCountAccounts(domains_ids, callback) {
const that = this;
const request_data = {};
const result = [];
request_data.callback = function(err, data) {
if (err) return callback(err);
data.CountAccountResponse.forEach((r) => {
const reqid = parseInt(r.requestId);
result[reqid - 1] = that.dictionary.cosesToCountAccountObject(r.cos);
});
return callback(null, result);
};
request_data.requests = [];
domains_ids.forEach((domain_id) => {
const request = this.buildRequestData(`CountAccount`, callback);
request.parse_response = this.parseCountAccountResponse;
request.params.params.domain = {
'by': this.dictionary.byIdOrName(domain_id),
'_content': domain_id
};
request_data.requests.push(request);
});
this.performRequest(request_data, true);
}
// TODO: TO ugly // TODO: TO ugly
setPassword(zimbra_id, password, callback) { setPassword(zimbra_id, password, callback) {
const request_data = this.buildRequestData(`SetPassword`, callback); const request_data = this.buildRequestData(`SetPassword`, callback);
......
...@@ -80,6 +80,17 @@ export default class Dictionary { ...@@ -80,6 +80,17 @@ export default class Dictionary {
return result; return result;
} }
cosesToCountAccountObject (coses) {
const result = {};
if (typeof coses !== 'undefined') coses.forEach((cos) => {
result[cos.name] = {
used: parseInt(cos._content),
id: cos.id
};
});
return result;
}
resourceResponseName (resource) { resourceResponseName (resource) {
return this.zimbra_resources[resource.toLowerCase()].response_name; return this.zimbra_resources[resource.toLowerCase()].response_name;
} }
......
...@@ -173,6 +173,27 @@ ...@@ -173,6 +173,27 @@
}); });
}); });
it('Should return correct response from BatchRequest', function(done){
let api = new ZimbraAdminApi(auth_data);
api.client.options.timeout = 5000;
let callback = function(err, data) {
if (err) return console.error(err);
expect(data.options.response.BatchResponse).to.exist;
expect(data.options.response.BatchResponse.GetAllAccountsResponse).to.exist;
expect(data.options.response.BatchResponse.GetAllDomainsResponse).to.exist;
expect(data.isBatch).to.be.true;
done();
}
const getAllAccounts = api.buildRequestData('GetAllAccounts', callback);
const getAllDomains = api.buildRequestData('GetAllDomains', callback);
const that = this;
let getCallback = function(err, response){
if (err) return this.handleError(err);
api.makeBatchRequest([getAllAccounts, getAllDomains], callback);
};
api.login(getCallback);
});
}); });
describe('Account tests', function() { describe('Account tests', function() {
...@@ -405,6 +426,16 @@ ...@@ -405,6 +426,16 @@
}); });
}); });
it('Batch Count Account Response Should return the correct', function(done){
let api = new ZimbraAdminApi(auth_data);
const callback = function(err, data){
if (err) console.error(err);
expect(data[0].default.used).to.be.above(1);
done();
};
api.batchCountAccounts(['customer.dev', 'zboxapp.dev'], callback);
});
it('domain.countAccounts() should return the counts', function(done){ it('domain.countAccounts() should return the counts', function(done){
let api = new ZimbraAdminApi(auth_data); let api = new ZimbraAdminApi(auth_data);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment