Commit 6d6d770d authored by Patricio Bruna's avatar Patricio Bruna

New option to return an Object instead of an Array in getAll operations

parent bd42b71e
...@@ -125,6 +125,29 @@ returning an `account.TOO_MANY_SEARCH_RESULTS` error., ...@@ -125,6 +125,29 @@ returning an `account.TOO_MANY_SEARCH_RESULTS` error.,
* `countOnly`: Whether response should be count only. Default is 0 (false), * `countOnly`: Whether response should be count only. Default is 0 (false),
* `attrs`: Comma separated list of attributes to ask for * `attrs`: Comma separated list of attributes to ask for
### Result as Object
If you need to get the result as an Object with the resource `id` or `name` as the Key
of the object you need to initialize the Api like this:
```javascript
var zimbraApi = new ZimbraAdminApi({
'url': 'http://zimbra.zboxapp.dev:8000/service/admin/soap',
'user': 'admin@zboxapp.dev',
'password':'12345678',
'arrayAsObject': true,
'arrayAsObjectKey': 'name' // By default is 'id';
});
var callback = function(err, data) {
if (err) return console.log(err);
console.log(data);
};
zimbraApi.getAllDomains(callback);
// Object {total: 261, more: false, domain: Object} <== Object!!
```
#### Examples #### Examples
##### 1. Get All Accounts without a query_object ##### 1. Get All Accounts without a query_object
......
...@@ -97,6 +97,8 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -97,6 +97,8 @@ return /******/ (function(modules) { // webpackBootstrap
this.password = auth_object.password; this.password = auth_object.password;
this._client = new jszimbra.Communication({ url: auth_object.url }); this._client = new jszimbra.Communication({ url: auth_object.url });
this.dictionary = new Dictionary(); this.dictionary = new Dictionary();
this.arrayAsObject = auth_object.arrayAsObject || false;
this.arrayAsObjectKey = auth_object.arrayAsObjectKey || 'id';
} }
(0, _createClass3.default)(ZimbraAdminApi, [{ (0, _createClass3.default)(ZimbraAdminApi, [{
...@@ -30820,7 +30822,7 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -30820,7 +30822,7 @@ return /******/ (function(modules) { // webpackBootstrap
module.exports = { module.exports = {
"name": "zimbra-admin-api-js", "name": "zimbra-admin-api-js",
"version": "0.2.13", "version": "0.3.0",
"main": "src/index.js", "main": "src/index.js",
"dependencies": { "dependencies": {
"crypto-browserify": "^3.11.0", "crypto-browserify": "^3.11.0",
...@@ -32791,15 +32793,41 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -32791,15 +32793,41 @@ return /******/ (function(modules) { // webpackBootstrap
}, { }, {
key: 'allResponse', key: 'allResponse',
value: function allResponse(data, request_data, callback) { value: function allResponse(data, request_data, callback) {
var result = null;
var resource = request_data.resource.toLowerCase(); var resource = request_data.resource.toLowerCase();
var response_name = ResponseParser.dictionary().resourceResponseName(resource); var response_name = ResponseParser.dictionary().resourceResponseName(resource);
var response_object = data.get()[request_data.response_name][response_name]; var response_object = data.get()[request_data.response_name][response_name];
var response_array = []; var apiClient = request_data.client;
if (apiClient.arrayAsObject) {
result = ResponseParser.allResponseAsObject(response_object, resource, apiClient);
} else {
result = ResponseParser.allResponseAsArray(response_object, resource, apiClient);
}
return callback(null, result);
}
}, {
key: 'allResponseAsArray',
value: function allResponseAsArray(response_object, resource, apiClient) {
var result = [];
if (response_object) response_object.forEach(function (r) {
var element = ResponseParser.dictionary().classFactory(resource, r, apiClient);
result.push(element);
});
return result;
}
}, {
key: 'allResponseAsObject',
value: function allResponseAsObject(response_object, resource, apiClient) {
var result = {};
if (response_object) response_object.forEach(function (r) { if (response_object) response_object.forEach(function (r) {
var element = ResponseParser.dictionary().classFactory(resource, r, request_data.client); var element = ResponseParser.dictionary().classFactory(resource, r, apiClient);
response_array.push(element); if (apiClient.client.arrayAsObjectKey === 'name') {
result[element.name] = element;
} else {
result[element.id] = element;
}
}); });
return callback(null, response_array); return result;
} }
}, { }, {
key: 'batchResponse', key: 'batchResponse',
...@@ -32883,17 +32911,23 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -32883,17 +32911,23 @@ return /******/ (function(modules) { // webpackBootstrap
}, { }, {
key: 'searchResponse', key: 'searchResponse',
value: function searchResponse(data, request_data, callback) { value: function searchResponse(data, request_data, callback) {
var response_types = ResponseParser.dictionary().searchResponseTypes(); var resource_types = ResponseParser.dictionary().searchResponseTypes();
var response_object = data.get()[request_data.response_name]; var response_object = data.get()[request_data.response_name];
var result = { total: response_object.searchTotal, more: response_object.more }; var result = { total: response_object.searchTotal, more: response_object.more };
response_types.forEach(function (type) { resource_types.forEach(function (resource) {
var resources = []; if (typeof response_object[resource] !== 'undefined') {
if (typeof response_object[type] !== 'undefined') { var resources = null;
response_object[type].forEach(function (resource) { if (request_data.client.arrayAsObject) {
var object = ResponseParser.dictionary().classFactory(type, resource, request_data.client); resources = ResponseParser.allResponseAsObject(response_object[resource], resource, request_data, callback);
resources.push(object); } else {
}); resources = ResponseParser.allResponseAsArray(response_object[resource], resource, request_data, callback);
result[type] = resources; }
// const resources = [];
// response_object[type].forEach((resource) => {
// const object = ResponseParser.dictionary().classFactory(type, resource, request_data.client);
// resources.push(object);
// });
result[resource] = resources;
} }
}); });
return callback(null, result); return callback(null, result);
......
{ {
"name": "zimbra-admin-api-js", "name": "zimbra-admin-api-js",
"version": "0.2.13", "version": "0.3.0",
"main": "src/index.js", "main": "src/index.js",
"dependencies": { "dependencies": {
"crypto-browserify": "^3.11.0", "crypto-browserify": "^3.11.0",
......
...@@ -17,6 +17,8 @@ class ZimbraAdminApi { ...@@ -17,6 +17,8 @@ class ZimbraAdminApi {
this.password = auth_object.password; this.password = auth_object.password;
this._client = new jszimbra.Communication({url: auth_object.url}); this._client = new jszimbra.Communication({url: auth_object.url});
this.dictionary = new Dictionary(); this.dictionary = new Dictionary();
this.arrayAsObject = auth_object.arrayAsObject || false;
this.arrayAsObjectKey = auth_object.arrayAsObjectKey || 'id';
} }
static version() { static version() {
......
...@@ -13,15 +13,39 @@ class ResponseParser { ...@@ -13,15 +13,39 @@ class ResponseParser {
} }
static allResponse(data, request_data, callback){ static allResponse(data, request_data, callback){
let result = null;
const resource = request_data.resource.toLowerCase(); const resource = request_data.resource.toLowerCase();
const response_name = ResponseParser.dictionary().resourceResponseName(resource); const response_name = ResponseParser.dictionary().resourceResponseName(resource);
const response_object = data.get()[request_data.response_name][response_name]; const response_object = data.get()[request_data.response_name][response_name];
const response_array = []; const apiClient = request_data.client;
if (apiClient.arrayAsObject) {
result = ResponseParser.allResponseAsObject(response_object, resource, apiClient);
} else {
result = ResponseParser.allResponseAsArray(response_object, resource, apiClient);
}
return callback(null, result);
}
static allResponseAsArray(response_object, resource, apiClient) {
const result = [];
if (response_object) response_object.forEach((r) => { if (response_object) response_object.forEach((r) => {
let element = ResponseParser.dictionary().classFactory(resource, r, request_data.client); let element = ResponseParser.dictionary().classFactory(resource, r, apiClient);
response_array.push(element); result.push(element);
}); });
return callback(null, response_array); return result;
}
static allResponseAsObject(response_object, resource, apiClient) {
const result = {};
if (response_object) response_object.forEach((r) => {
const element = ResponseParser.dictionary().classFactory(resource, r, apiClient);
if (apiClient.client.arrayAsObjectKey === 'name') {
result[element.name] = element;
} else {
result[element.id] = element;
}
});
return result;
} }
static batchResponse(data, callback) { static batchResponse(data, callback) {
...@@ -92,17 +116,23 @@ class ResponseParser { ...@@ -92,17 +116,23 @@ class ResponseParser {
} }
static searchResponse(data, request_data, callback) { static searchResponse(data, request_data, callback) {
const response_types = ResponseParser.dictionary().searchResponseTypes(); const resource_types = ResponseParser.dictionary().searchResponseTypes();
const response_object = data.get()[request_data.response_name]; const response_object = data.get()[request_data.response_name];
const result = { total: response_object.searchTotal, more: response_object.more }; const result = { total: response_object.searchTotal, more: response_object.more };
response_types.forEach((type) => { resource_types.forEach((resource) => {
const resources = []; if (typeof response_object[resource] !== 'undefined') {
if (typeof response_object[type] !== 'undefined') { let resources = null;
response_object[type].forEach((resource) => { if (request_data.client.arrayAsObject) {
const object = ResponseParser.dictionary().classFactory(type, resource, request_data.client); resources = ResponseParser.allResponseAsObject(response_object[resource], resource, request_data, callback);
resources.push(object); } else {
}); resources = ResponseParser.allResponseAsArray(response_object[resource], resource, request_data, callback);
result[type] = resources; }
// const resources = [];
// response_object[type].forEach((resource) => {
// const object = ResponseParser.dictionary().classFactory(type, resource, request_data.client);
// resources.push(object);
// });
result[resource] = resources;
} }
}); });
return callback(null, result); return callback(null, result);
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
}; };
describe('Basic tests', function() { describe('Basic tests', function() {
this.timeout(5000); this.timeout(10000);
it('should return the Delegated Token', function(done){ it('should return the Delegated Token', function(done){
...@@ -43,10 +43,21 @@ ...@@ -43,10 +43,21 @@
}); });
}); });
it('should get all domains as an Object', function(done) {
let auth_data_tmp = Object.assign({}, auth_data)
auth_data_tmp.arrayAsObject = true;
auth_data_tmp.arrayAsObjectKey = 'name';
let api = new ZimbraAdminApi(auth_data_tmp);
api.getAllDomains(function(err, data){
if (err) console.log(err);
expect(data.domain['customer.dev']).to.exist;
done();
});
});
it('should get all accounts', function(done) { it('should get all accounts', function(done) {
// var callback = sinon.spy(); // var callback = sinon.spy();
let api = new ZimbraAdminApi(auth_data); let api = new ZimbraAdminApi(auth_data);
// var proxy = api.getAllAccounts(callback);
api.getAllAccounts(function(err, data){ api.getAllAccounts(function(err, data){
if (err) console.log(err); if (err) console.log(err);
expect(data.account[0].constructor.name).to.equal('Account'); expect(data.account[0].constructor.name).to.equal('Account');
...@@ -54,6 +65,17 @@ ...@@ -54,6 +65,17 @@
}); });
}); });
it('should get all accounts as an Object', function(done) {
let auth_data_tmp2 = Object.assign({}, auth_data)
auth_data_tmp2.arrayAsObject = true;
let api = new ZimbraAdminApi(auth_data_tmp2);
api.getAllAccounts(function(err, data){
if (err) console.log(err);
expect(Object.keys(data.account)[0]).to.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i);
done();
});
});
it('should get all distribution_lists', function(done) { it('should get all distribution_lists', function(done) {
let api = new ZimbraAdminApi(auth_data); let api = new ZimbraAdminApi(auth_data);
api.getAllDistributionLists(function(err, data){ api.getAllDistributionLists(function(err, data){
...@@ -184,7 +206,6 @@ ...@@ -184,7 +206,6 @@
const getAllDomains = api.directorySearch({types: 'domains'}); const getAllDomains = api.directorySearch({types: 'domains'});
api.login(function(err, data){ api.login(function(err, data){
api.makeBatchRequest([deleteAccount, getAllDomains, getAllAccounts], function(err, data){ api.makeBatchRequest([deleteAccount, getAllDomains, getAllAccounts], function(err, data){
console.log(data);
expect(data.errors.length).to.be.above(1); expect(data.errors.length).to.be.above(1);
expect(data.errors[0].constructor.name).to.equal('Error'); expect(data.errors[0].constructor.name).to.equal('Error');
expect(data.errors[0].extra.code).to.exist; expect(data.errors[0].extra.code).to.exist;
...@@ -625,6 +646,7 @@ ...@@ -625,6 +646,7 @@
let domain_admin = Date.now() + '@customer.dev'; let domain_admin = Date.now() + '@customer.dev';
let resource_name = Date.now() + '.dev'; let resource_name = Date.now() + '.dev';
api.createAccount(domain_admin, '12dda.222', {}, function(err, account){ api.createAccount(domain_admin, '12dda.222', {}, function(err, account){
if (err) return console.error(err);
api.createDomain(resource_name, {}, function(err, data){ api.createDomain(resource_name, {}, function(err, data){
if (err) console.error(err); if (err) console.error(err);
let domain = data; let domain = 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