Skip to content

Commit

Permalink
implemented copy() and edit() methods. Renamed post() to read create(…
Browse files Browse the repository at this point in the history
…). Added more examples.
  • Loading branch information
Hovhannes Babayan committed Jan 8, 2015
1 parent 8f8099b commit 14868fc
Show file tree
Hide file tree
Showing 17 changed files with 333 additions and 21 deletions.
6 changes: 3 additions & 3 deletions dist/attask.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/node/create-and-remove-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ util.log('Logging in ...');
instance.login('new@user.attask', 'user').then(
function(data) {
util.log('Creating new template ...');
instance.post('tmpl', {name: 'API Template', description: 'This template has been created using API'}).then(
instance.create('tmpl', {name: 'API Template', description: 'This template has been created using API'}).then(
function(data) {
util.log('Create success. Received data:');
console.log(util.inspect(data, {colors:true}));
Expand Down
48 changes: 48 additions & 0 deletions examples/node/create-edit-delete-group-pc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Logs in, then creates a group "Api Group", edits the name to read "Api Group 2", then deletes it.
* This examples is similar to create-edit-delete-group.js, but this one makes use of promise chaining (pc).
*/

var ApiFactory = require('./../../').ApiFactory;
var util = require('util');

var instance = ApiFactory.getInstance({
url: 'http://localhost:8080',
version: '4.0'
});

var login = function() {
util.log('Logging in ...');
return instance.login('new@user.attask', 'user');
};

var createGroup = function() {
util.log('Creating new group ...');
return instance.create('group', {name: 'Api Group'});
};

var editGroup = function(data) {
util.log('Create success. Received data:');
console.log(util.inspect(data, {colors:true}));
return instance.edit('group', data.ID, {
name: 'Api Group 2'
}, ['ID'])
};

var deleteGroup = function(data) {
util.log('Edit success. Received data:');
console.log(util.inspect(data, {colors:true}));
return instance.remove('group', data.ID);
};

util.print('Logs in, then creates a group "Api Group", edits the name to read "Api Group 2", then deletes it\n');

login().then(createGroup).then(editGroup).then(deleteGroup).then(
function() {
util.log('Remove success');
},
function(error) {
util.log('Error. Received data:');
console.log(util.inspect(error, {colors:true}));
}
);
54 changes: 54 additions & 0 deletions examples/node/create-edit-delete-group.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Logs in, then creates a group "Api Group", edits the name to read "Api Group 2", then deletes it
*/

var ApiFactory = require('./../../').ApiFactory;
var util = require('util');

var instance = ApiFactory.getInstance({
url: 'http://localhost:8080',
version: '4.0'
});

util.print('Logs in, then creates a group "Api Group", edits the name to read "Api Group 2", then deletes it\n');
util.log('Logging in ...');
instance.login('new@user.attask', 'user').then(
function(data) {
util.log('Creating new group ...');
instance.create('group', {name: 'Api Group'}).then(
function(data) {
util.log('Create success. Received data:');
console.log(util.inspect(data, {colors:true}));
instance.edit('group', data.ID, {
name: 'Api Group 2'
}, ['ID']).then(
function(data) {
util.log('Edit success. Received data:');
console.log(util.inspect(data, {colors:true}));
instance.remove('group', data.ID).then(
function() {
util.log('Remove success');
},
function(error) {
util.log('Remove failure. Received data:');
console.log(util.inspect(error, {colors:true}));
}
);
},
function(error) {
util.log('Edit failure. Received data:');
console.log(util.inspect(error, {colors:true}));
}
);
},
function(error) {
util.log('Create failure. Received data:');
console.log(util.inspect(error, {colors:true}));
}
);
},
function(error) {
util.log('Login failure. Received data:');
console.log(util.inspect(error, {colors:true}));
}
);
2 changes: 1 addition & 1 deletion examples/node/create-new-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ util.log('Logging in ...');
instance.login('new@user.attask', 'user').then(
function(data) {
util.log('Creating new project ...');
instance.post('proj', {name: 'API Project', description: 'This project has been created using API'}).then(
instance.create('proj', {name: 'API Project', description: 'This project has been created using API'}).then(
function(data) {
util.log('Create success. Received data:');
console.log(util.inspect(data, {colors:true}));
Expand Down
43 changes: 43 additions & 0 deletions examples/node/create-project-copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Logs in, then creates a new project with name "API Project", then copies it
*/

var ApiFactory = require('./../../').ApiFactory;
var util = require('util');

var instance = ApiFactory.getInstance({
url: 'http://localhost:8080',
version: '4.0'
});

util.print('Logs in, then creates a new project with name "API Project", then copies it\n');
util.log('Logging in ...');
instance.login('new@user.attask', 'user').then(
function(data) {
util.log('Creating new project ...');
instance.create('proj', {name: 'API Project', description: 'This project has been created using API'}).then(
function(data) {
util.log('Create success. Received data:');
console.log(util.inspect(data, {colors:true}));
instance.copy('proj', data.ID, {name: 'API Project Copy'}).then(
function(data) {
util.log('Copy success. Received data:');
console.log(util.inspect(data, {colors:true}));
},
function(error) {
util.log('Copy failure. Received data:');
console.log(util.inspect(error, {colors:true}));
}
)
},
function(error) {
util.log('Create failure. Received data:');
console.log(util.inspect(error, {colors:true}));
}
);
},
function(error) {
util.log('Login failure. Received data:');
console.log(util.inspect(error, {colors:true}));
}
);
52 changes: 52 additions & 0 deletions examples/node/delete-projects-by-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Logs in, then deletes all projects with name containing "API Project"
*/

var Api = require('./../../').Api;
var ApiConstants = require('./../../').ApiConstants;
var util = require('util');

var instance = new Api({
url: 'http://localhost:8080',
version: '4.0'
});

function deleteProject(projectID) {
instance.remove('proj', projectID).then(
function() {
util.log('Removed '+projectID);
},
function(error) {
util.log('Remove failure. Received data:');
console.log(util.inspect(error, {colors:true}));
}
);
}

util.print('Logs in, then deletes all projects with name containing "API Project"\n');
util.log('Logging in ...');
instance.login('new@user.attask', 'user').then(
function(data) {
util.log('Searching for projects with name containing "API Project" ...');
var query = {};
query['name'] = 'API Project';
query['name' + ApiConstants.MOD] = ApiConstants.Operators.CONTAINS;
instance.search('proj', query, ['ID']).then(
function(data) {
util.log('Search success. Received data:');
console.log(util.inspect(data, {colors:true}));
for(var i=0; i<data.length; ++i) {
deleteProject(data[i].ID);
}
},
function(error) {
util.log('Search failure. Received data:');
console.log(util.inspect(error, {colors:true}));
}
);
},
function(error) {
util.log('Login failure. Received data:');
console.log(util.inspect(error, {colors:true}));
}
);
4 changes: 2 additions & 2 deletions src/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ require('./plugins/login')(Api);
require('./plugins/logout')(Api);
require('./plugins/search')(Api);
require('./plugins/get')(Api);
require('./plugins/post')(Api);
require('./plugins/put')(Api);
require('./plugins/create')(Api);
require('./plugins/edit')(Api);
require('./plugins/remove')(Api);
require('./plugins/report')(Api);
require('./plugins/count')(Api);
Expand Down
19 changes: 17 additions & 2 deletions src/plugins/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
module.exports = function(Api) {
Api.prototype.copy = function () {
throw new Error('Not implemented')
/**
* Copies an existing object with making changes on a copy.
* Copying is supported only for some objects. The {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer} page displays which objects support the Copy action.
* @param {String} objCode One of object codes from {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer}
* @param {String} objID ID of object to copy
* @param {Object} updates Which fields to set on copied object. See {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer} for the list of available fields for the given objCode.
* @param {Object} [fields] Which fields to return. See {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer} for the list of available fields for the given objCode.
* @return {Promise} A promise which will resolved with results if everything went ok and rejected otherwise
*/
Api.prototype.copy = function (objCode, objID, updates, fields) {
var params = {
copySourceID: objID
};
if (updates) {
params.updates = JSON.stringify(updates);
}
return this.request(objCode, params, fields, 'POST');
};
};
4 changes: 2 additions & 2 deletions src/plugins/post.js → src/plugins/create.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module.exports = function(Api) {
/**
* Inserts a new object.
* Creates a new object.
* @param {String} objCode One of object codes from {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer}
* @param {Object} params Values of fields to be set for the new object. See {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer} for the list of available fields for the given objCode.
* @param {String[]} [fields] Which fields of newly created object to return. See {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer} for the list of available fields for the given objCode.
* @returns {Promise} A promise which will resolved with the ID and any other specified fields of newly created object
*/
Api.prototype.post = function (objCode, params, fields) {
Api.prototype.create = function (objCode, params, fields) {
return this.request(objCode, params, fields, 'POST');
};
};
16 changes: 16 additions & 0 deletions src/plugins/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = function(Api) {
/**
* Edits an existing object
* @param {String} objCode One of object codes from {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer}
* @param {String} objID ID of object to modify
* @param {Object} updates Which fields to set. See {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer} for the list of available fields for the given objCode.
* @param {Object} [fields] Which fields to return. See {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer} for the list of available fields for the given objCode.
* @return {Promise} A promise which will resolved with results if everything went ok and rejected otherwise
*/
Api.prototype.edit = function (objCode, objID, updates, fields) {
var params = {
updates: JSON.stringify(updates)
};
return this.request(objCode + '/' + objID, params, fields, 'PUT');
};
};
5 changes: 0 additions & 5 deletions src/plugins/put.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/plugins/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var queryString = require('querystring'),

module.exports = function(Api) {
var requestHasData = function(method) {
return method !== 'GET';
return method !== 'GET' && method !== 'PUT';
};

Api.prototype.request = function(path, params, fields, method) {
Expand Down
4 changes: 2 additions & 2 deletions test/Api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ describe('Create new instance for API', function() {
expect(api).to.respondTo('get');
expect(api).to.respondTo('login');
expect(api).to.respondTo('logout');
expect(api).to.respondTo('post');
expect(api).to.respondTo('put');
expect(api).to.respondTo('create');
expect(api).to.respondTo('edit');
expect(api).to.respondTo('search');
expect(api).to.respondTo('remove');
expect(api).to.respondTo('report');
Expand Down
49 changes: 49 additions & 0 deletions test/plugins/copy.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require('./../common');

var Api = require('./../../').Api;


describe('Api.copy() method', function() {

var api;
var url = 'http://foobar:8080';

beforeEach(function () {
api = new Api({url: url});
sinon.stub(api, "request");
});

afterEach(function () {
api.request.restore(); // Unwraps the spy
});

it('should call request() with proper params and return value received from request() (with updates)', function() {
var updates = {
'foo': 'bar'
};

var objCode = 'baz',
objID = 'baz345';

var expectedReturnValue = 123;
api.request.returns(expectedReturnValue);

var actualReturnedValue = api.copy(objCode, objID, updates);
expect(api.request).to.have.callCount(1);
expect(api.request).to.have.been.calledWith(objCode, {copySourceID: objID, updates: JSON.stringify(updates)}, undefined, "POST");
expect(actualReturnedValue).to.equal(expectedReturnValue);
});

it('should call request() with proper params and return value received from request() (without updates)', function() {
var objCode = 'baz',
objID = 'baz345';

var expectedReturnValue = 123;
api.request.returns(expectedReturnValue);

var actualReturnedValue = api.copy(objCode, objID);
expect(api.request).to.have.callCount(1);
expect(api.request).to.have.been.calledWith(objCode, {copySourceID: objID}, undefined, "POST");
expect(actualReturnedValue).to.equal(expectedReturnValue);
});
});
4 changes: 2 additions & 2 deletions test/plugins/post.spec.js → test/plugins/create.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require('./../common');
var Api = require('./../../').Api;


describe('Api.post() method', function() {
describe('Api.create() method', function() {

var api;
var url = 'http://foobar:8080';
Expand Down Expand Up @@ -31,7 +31,7 @@ describe('Api.post() method', function() {
var expectedReturnValue = 123;
api.request.returns(expectedReturnValue);

var actualReturnedValue = api.post(objCode, params, fields);
var actualReturnedValue = api.create(objCode, params, fields);
expect(api.request).to.have.callCount(1);
expect(api.request).to.have.been.calledWith(objCode, params, fields, "POST");
expect(actualReturnedValue).to.equal(expectedReturnValue);
Expand Down
Loading

0 comments on commit 14868fc

Please sign in to comment.