-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented copy() and edit() methods. Renamed post() to read create(…
…). Added more examples.
- Loading branch information
Hovhannes Babayan
committed
Jan 8, 2015
1 parent
8f8099b
commit 14868fc
Showing
17 changed files
with
333 additions
and
21 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.