Skip to content

Commit

Permalink
working exemple with login and account creation
Browse files Browse the repository at this point in the history
  • Loading branch information
nadej committed Jun 5, 2013
1 parent f009e3c commit f205122
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 18 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,38 @@ node-sugarcrm-client
SugarCRM REST API Client in node.js

https://travis-ci.org/jcnade/node-sugarcrm-client.png


* Tested on SugarCRM Community Edition 6.5.* on RESP API v4.1


Provide URL, login and password:

var sugar = require('node-sugarcrm-client');
sugar.init(
{
apiURL: "http://*********/sugarcrm/service/v4_1/rest.php"
,login: "*******",
,passwd: "*******"
}
);


Config Check:

console.log(sugar.configInfo());


Create a new Account :

params = {
session: sessionID
,module_name : "Accounts"
,name_value_list : [
{ "name": "name", "value": "Account from Node-SugarCRM-Client" }
]
};
sugar.call("set_entry", params, function(res,err){
console.log(res,err)
});

8 changes: 0 additions & 8 deletions bin/sleep-sort

This file was deleted.

62 changes: 62 additions & 0 deletions bin/sugar
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env node



var sugar = require('../lib/main');


// Provide URL, login and password
sugar.init(
{
apiURL: "http://*******/sugarcrm/service/v4_1/rest.php",
login: "*********",
passwd: "*********"
}
);

// Config Check
console.log(sugar.configInfo());


// get a Session ID (test)
/*
sugar.login(function(sessionID){
if (sessionID != 'undefined') {
console.log('Your session ID is', sessionID);
} else {
console.log('Access denied, check your config');
}
});
*/



// get a Session ID
sugar.login(function(sessionID){

if (sessionID != 'undefined') {


//
// Create a new Account
//

params = {
session: sessionID
,module_name : "Accounts"
,name_value_list : [
{ "name": "name", "value": "Account from Node-SugarCRM-Client" }
]
};
sugar.call("set_entry", params, function(res,err){
console.log(res,err)
});


} else {
console.log('Access denied, check your config');
}
});


103 changes: 100 additions & 3 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,104 @@
"use strict";

/* SugarCRM REST API module in node.js */

function sleepsort() {
return [];
var request = require('request');


/* Global */

var apiURL = "";
var login = "";
var passwd = "";
var sessionID = "";


/* Config Initialisation */
var init = function (initArray) {
apiURL = initArray.apiURL;
login = initArray.login;
passwd = initArray.passwd;
}
exports.init = init;


/* Show Config Info */
var configInfo = function (initArray) {

return {
apiURL: apiURL
,login : login
,passwd : passwd
}
}
exports.configInfo = configInfo;


/* get a session ID */
exports.login = function (callback) {

var subargs = {
user_auth: {
"user_name" : login,
"password" : passwd,
encryption:'PLAIN'
},
application: "SugarCRM RestAPI Example"
}

var subargsInString = JSON.stringify(subargs);

var data = {
method: "login",
input_type: "JSON",
response_type: "JSON",
rest_data: subargsInString
};

request.post(apiURL, { form: data }, function(e,r,body){
sessionID = JSON.parse(body).id;
callback(sessionID);
});
}



/* pure POST call function */
var call = function (method, parameters, callback) {

var data = {
method: method,
input_type: "JSON",
response_type: "JSON",
rest_data: JSON.stringify(parameters)
};

request.post(apiURL, { form: data }, function(e,r,body){
//console.log('Error: ',e);
//console.log('Return: ',body);

try {
var res = JSON.parse(body);
callback(res, false);
} catch (e) {
callback(false, { error: body } );
}


})
}
exports.call = call;














module.exports = sleepsort;
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"scripts": {
"test": "mocha test/*.js"
},
"bin": { "sleep-sort": "./bin/sleep-sort" },
"repository": {
"type": "git",
"url": "https://github.com/jcnade/node-sugarcrm-client.git"
Expand All @@ -17,6 +18,9 @@
"sugarcrm",
""
],
"dependencies" : {
"request" : "*"
},
"devDependencies": {
"mocha": "",
"should": ""
Expand Down
28 changes: 21 additions & 7 deletions test/main.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
"use strict";

var should = require('should');
var sleepsort = require('../lib/main');

describe('sleepsort', function() {
describe('with no arguments', function() {
it('returns an empty array', function() {
var result = sleepsort();
result.should.eql([]);
var sugar = require('../lib/main');


describe('sugar.init()', function() {
describe('Checking sugar.init() and sugar.configInfo()', function() {
it('without any argument', function(done) {
sugar.init(
{
apiURL: "test1",
login: "test2",
passwd: "test3"
}
);
sugar.configInfo()
var result = sugar.configInfo();
result.should.eql({apiURL:"test1",login:"test2",passwd:"test3"});
done();
});
});
});
});


0 comments on commit f205122

Please sign in to comment.