-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working exemple with login and account creation
- Loading branch information
Showing
6 changed files
with
222 additions
and
18 deletions.
There are no files selected for viewing
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 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
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'); | ||
} | ||
}); | ||
|
||
|
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,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; |
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,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(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
|