|
409 | 409 | } |
410 | 410 | }; |
411 | 411 |
|
412 | | - var buffer = ''; |
413 | | - |
414 | 412 | if (currentUser != null && !!currentUser["user-token"]) { |
415 | 413 | options.headers["user-token"] = currentUser["user-token"]; |
416 | 414 | } |
|
419 | 417 | throw new Error('Use Async type of request using Backendless with NodeJS. Add Backendless.Async(successCallback, errorCallback) as last argument'); |
420 | 418 | } |
421 | 419 |
|
| 420 | + var buffer; |
422 | 421 | var httpx = require(https ? 'https' : 'http'); |
423 | | - |
424 | 422 | var req = httpx.request(options, function(res) { |
425 | 423 | res.setEncoding('utf8'); |
426 | 424 | res.on('data', function(chunk) { |
427 | | - buffer += chunk; |
| 425 | + buffer = buffer ? buffer + chunk : chunk; |
428 | 426 | }); |
429 | 427 | res.on('end', function() { |
430 | | - var contentType = res.headers['content-type']; |
431 | | - |
432 | | - if (contentType && contentType.indexOf('application/json') !== -1) { |
433 | | - buffer = tryParseJSON(buffer); |
434 | | - } |
435 | | - |
436 | 428 | var callback = config.asyncHandler[res.statusCode >= 200 && res.statusCode < 300 ? "success" : "fault"]; |
437 | 429 |
|
438 | 430 | if (Utils.isFunction(callback)) { |
| 431 | + var contentType = res.headers['content-type']; |
| 432 | + |
| 433 | + if (buffer !== undefined && contentType && contentType.indexOf('application/json') !== -1) { |
| 434 | + buffer = tryParseJSON(buffer); |
| 435 | + } |
| 436 | + |
439 | 437 | callback(buffer); |
440 | 438 | } |
441 | 439 | }); |
|
1499 | 1497 | return isAsync ? result : this._parseResponse(result); |
1500 | 1498 | }, |
1501 | 1499 |
|
1502 | | - roleHelper: function(username, rolename, async, operation) { |
1503 | | - if (!username) { |
1504 | | - throw new Error('Username can not be empty'); |
| 1500 | + roleHelper: function(identity, rolename, async, operation) { |
| 1501 | + if (!identity) { |
| 1502 | + throw new Error('User identity can not be empty'); |
1505 | 1503 | } |
1506 | 1504 |
|
1507 | 1505 | if (!rolename) { |
1508 | 1506 | throw new Error('Rolename can not be empty'); |
1509 | 1507 | } |
1510 | 1508 |
|
1511 | 1509 | var responder = extractResponder(arguments); |
1512 | | - var isAsync = responder != null; |
1513 | | - |
1514 | | - if (responder) { |
1515 | | - responder = this._wrapAsync(responder); |
1516 | | - } |
1517 | | - |
1518 | | - var data = { |
1519 | | - user : username, |
1520 | | - roleName: rolename |
1521 | | - }; |
1522 | 1510 |
|
1523 | 1511 | return Backendless._ajax({ |
1524 | 1512 | method : 'POST', |
1525 | 1513 | url : this.restUrl + '/' + operation, |
1526 | | - isAsync : isAsync, |
| 1514 | + isAsync : !!responder, |
1527 | 1515 | asyncHandler: responder, |
1528 | | - data : JSON.stringify(data) |
| 1516 | + data : JSON.stringify({user : identity, roleName: rolename}) |
1529 | 1517 | }); |
1530 | 1518 | }, |
1531 | 1519 |
|
1532 | | - assignRole: function(username, rolename, async) { |
1533 | | - return this.roleHelper(username, rolename, async, 'assignRole'); |
| 1520 | + assignRole: function(identity, rolename, async) { |
| 1521 | + return this.roleHelper(identity, rolename, async, 'assignRole'); |
1534 | 1522 | }, |
1535 | 1523 |
|
1536 | | - unassignRole: function(username, rolename, async) { |
1537 | | - return this.roleHelper(username, rolename, async, 'unassignRole'); |
| 1524 | + unassignRole: function(identity, rolename, async) { |
| 1525 | + return this.roleHelper(identity, rolename, async, 'unassignRole'); |
1538 | 1526 | }, |
1539 | 1527 |
|
1540 | 1528 | login: function(username, password, stayLoggedIn, async) { |
|
0 commit comments