Added additional initialization properties, updated objectkit
diff --git a/config.json b/config.json
index 646f277..15fd550 100644
--- a/config.json
+++ b/config.json
@@ -2,10 +2,12 @@
"config": {
"usergrid": {
"appId": "sandbox",
+ "authFallback": "none",
"baseUrl": "https://api.usergrid.com",
"clientId": "YXA6GXSAACS2EeOYd20aP4G6Lw",
"clientSecret": "YXA66BeEvgNpJBwc4PAbvZZGTVS_SSw",
"orgId": "brandon.apigee",
+ "tokenTtl": 3600,
"version": "2.0"
}
}
diff --git a/lib/client.js b/lib/client.js
index b7541b1..104f6dd 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -2,60 +2,70 @@
var UsergridRequest = require('./request'),
ok = require('objectkit'),
+ config = require('../config.json').config,
helpers = require('../helpers'),
url = require('url')
-var UsergridClient = function(orgId, appId) {
+var UsergridClient = function(opts) {
var self = this
- if (ok(orgId).exists() && ok(appId).exists()) {
- self.orgId = orgId
- self.appId = appId
+
+ // orgId and appId are required
+ self.orgId = ok(arguments).getIfExists('0') || ok(opts).getIfExists('orgId') || ok(config).getIfExists('usergrid.orgId')
+ self.appId = ok(arguments).getIfExists('1') || ok(opts).getIfExists('appId') || ok(config).getIfExists('usergrid.appId')
+
+ self.baseUrl = ok(opts).getIfExists('baseUrl') || ok(config).getIfExists('usergrid.baseUrl') || "https://api.usergrid.com" // default: https://api.usergrid.com
+ self.clientId = ok(opts).getIfExists('clientId') || ok(config).getIfExists('usergrid.clientId') || "" // default: undefined
+ self.clientSecret = ok(opts).getIfExists('clientSecret') || ok(config).getIfExists('usergrid.clientSecret') || "", // default: undefined
+ self.tokenTtl = ok(opts).getIfExists('tokenTtl') || ok(config).getIfExists('usergrid.tokenTtl') || 3600, // default: 3600
+ self.authFallback = ok(opts).getIfExists('authFallback') || ok(config).getIfExists('usergrid.authFallback') || "none" // ("none"|"app") default: "none"
+
+ if (ok(self.orgId).exists() && ok(self.appId).exists()) {
return self;
} else {
- throw new Error('\'orgId\' and \'appId\' parameters must be passed during UsergridClient initialization')
+ throw new Error('\'orgId\' and \'appId\' parameters are required when instantiating UsergridClient')
}
}
-UsergridClient.prototype.GET = function(type, uuid, callback) {
+UsergridClient.prototype.GET = function(type, uuidOrName, callback) {
return new UsergridRequest({
client: this,
method: 'GET',
type: type,
- uuid: typeof uuid === 'string' ? uuid : undefined
- }, callback || uuid)
+ uuidOrName: typeof uuidOrName === 'string' ? uuidOrName : undefined
+ }, callback || uuidOrName)
}
-UsergridClient.prototype.PUT = function(type, uuid, body, callback) {
- if (typeof uuid !== 'string')
- throw new Error('\'uuid\' (or \'name\') parameter must be defined when making a PUT request')
+UsergridClient.prototype.PUT = function(type, uuidOrName, body, callback) {
+ if (typeof uuidOrName !== 'string')
+ throw new Error('\'uuidOrName\' parameter is required when making a PUT request')
return new UsergridRequest({
client: this,
method: 'PUT',
type: type,
- uuid: typeof uuid === 'string' ? uuid : undefined,
- body: typeof body === 'object' ? body : typeof uuid === 'object' ? uuid : undefined
- }, callback || body || uuid)
+ uuidOrName: typeof uuidOrName === 'string' ? uuidOrName : undefined,
+ body: typeof body === 'object' ? body : typeof uuidOrName === 'object' ? uuidOrName : undefined
+ }, callback || body || uuidOrName)
}
-UsergridClient.prototype.POST = function(type, uuid, body, callback) {
+UsergridClient.prototype.POST = function(type, uuidOrName, body, callback) {
return new UsergridRequest({
client: this,
method: 'POST',
type: type,
- uuid: typeof uuid === 'string' ? uuid : undefined,
- body: typeof body === 'object' ? body : typeof uuid === 'object' ? uuid : undefined
- }, callback || body || uuid)
+ uuidOrName: typeof uuidOrName === 'string' ? uuidOrName : undefined,
+ body: typeof body === 'object' ? body : typeof uuidOrName === 'object' ? uuidOrName : undefined
+ }, callback || body || uuidOrName)
}
-UsergridClient.prototype.DELETE = function(type, uuid, callback) {
- if (typeof uuid !== 'string')
- throw new Error('\'uuid\' (or \'name\') parameter must be defined when making a DELETE request')
+UsergridClient.prototype.DELETE = function(type, uuidOrName, callback) {
+ if (typeof uuidOrName !== 'string')
+ throw new Error('\'uuidOrName\' parameter is required when making a DELETE request')
return new UsergridRequest({
client: this,
method: 'DELETE',
type: type,
- uuid: typeof uuid === 'string' ? uuid : undefined
- }, callback || uuid)
+ uuidOrName: typeof uuidOrName === 'string' ? uuidOrName : undefined
+ }, callback || uuidOrName)
}
module.exports = UsergridClient
\ No newline at end of file
diff --git a/lib/request.js b/lib/request.js
index b1ee1ea..fbf7894 100644
--- a/lib/request.js
+++ b/lib/request.js
@@ -11,22 +11,32 @@
var UsergridRequest = function(opts, callback) {
if (typeof opts.type !== 'string')
- throw new Error('\'type\' (or \'collection\') parameter must be defined when making a request')
+ throw new Error('\'type\' (or \'collection\') parameter is required when making a request')
- // uuid and body are optional and not always passed; setting callback to the last defined argument
- var uri = urljoin(config.usergrid.baseUrl, opts.client.orgId, opts.client.appId, opts.type, (typeof opts.uuid === 'string') ? opts.uuid : "")
+ // uuidOrName and body are optional and not always passed; setting callback to the last defined argument
+ var uri = urljoin(
+ ok(config).getIfExists('usergrid.baseUrl'),
+ opts.client.orgId,
+ opts.client.appId,
+ opts.type, (typeof opts.uuidOrName === 'string') ? opts.uuidOrName : ""
+ )
+
var params = request.initParams(uri, {
headers: {
'User-Agent': util.format("usergrid-nodejs/v%s", config.usergrid.version),
},
- body: opts.body
+ body: opts.body,
+ qs: (opts.client.clientId !== undefined && opts.client.clientSecret !== undefined) ? {
+ clientId: opts.client.clientId,
+ clientSecret: opts.client.clientSecret
+ } : undefined
}, callback)
params.method = opts.method
// default to using JSON, since we only ever do that with Usergrid
params.json = true
-
+
request(params, function(error, response) {
response = new UsergridResponse(response)
params.callback(error, response)
diff --git a/lib/response.js b/lib/response.js
index 9104fdf..cae6df6 100644
--- a/lib/response.js
+++ b/lib/response.js
@@ -7,7 +7,7 @@
_.mixin(require('underscore.string'));
function UsergridResponse(response) {
- if (ok(response.body).check('entities')) {
+ if (ok(response.body).has('entities')) {
var entities = response.body.entities
extend(response, {
metadata: extend({}, response.body),
diff --git a/tests/usergrid.js b/tests/usergrid.js
index 0052412..612c39f 100644
--- a/tests/usergrid.js
+++ b/tests/usergrid.js
@@ -1,6 +1,7 @@
'use strict'
var should = require('should'),
+ ok = require('objectkit'),
config = require('../config.json').config,
Usergrid = require('../usergrid'),
UsergridClient = require('../lib/client')
@@ -8,28 +9,53 @@
const _collection = 'tests'
var _uuid = null
-describe('Usergrid.initialize', function() {
+describe('Usergrid.initSharedInstance', function() {
it('should fail to initialize without an orgId and appId', function() {
should(function() {
- Usergrid.initialize()
+ Usergrid.init()
}).fail
})
- it('should initialize when using an orgId and appId', function(done) {
- Usergrid.initialize(config.usergrid.orgId, config.usergrid.appId)
+ it('should initialize when passing an orgId and appId', function(done) {
+ Usergrid.init(config.usergrid.orgId, config.usergrid.appId)
done()
})
- it('Usergrid.orgId should match the orgId in package.json', function() {
+ it('should initialize using orgId and appId from config.json', function(done) {
+ Usergrid.init()
+ done()
+ })
+
+ it('Usergrid.orgId should match the orgId in config.json', function() {
Usergrid.orgId.should.equal(config.usergrid.orgId)
})
- it('Usergrid.appId should match the appId in package.json', function() {
+ it('Usergrid.appId should match the appId in config.json', function() {
Usergrid.appId.should.equal(config.usergrid.appId)
})
- it('Usergird should be an instance of UsergridClient', function() {
+ it('Usergrid.clientId should match the clientId in config.json', function() {
+ Usergrid.clientId.should.equal(config.usergrid.clientId)
+ })
+
+ it('Usergrid.clientSecret should match the clientSecret in config.json', function() {
+ Usergrid.clientSecret.should.equal(config.usergrid.clientSecret)
+ })
+
+ it('Usergrid.baseUrl should match the baseUrl in config.json', function() {
+ Usergrid.baseUrl.should.equal(config.usergrid.baseUrl)
+ })
+
+ it('Usergrid.tokenTtl should match the tokenTtl in config.json', function() {
+ Usergrid.tokenTtl.should.equal(config.usergrid.tokenTtl)
+ })
+
+ it('Usergrid.authFallback should match the authFallback in config.json', function() {
+ Usergrid.authFallback.should.equal(config.usergrid.authFallback)
+ })
+
+ it('Usergrid should be an instance of UsergridClient', function() {
Usergrid.should.be.an.instanceof(UsergridClient)
})
});
diff --git a/usergrid.js b/usergrid.js
index 0975a91..20ed448 100644
--- a/usergrid.js
+++ b/usergrid.js
@@ -5,11 +5,11 @@
util = require("util")
var Usergrid = {
- initialize: function(orgId, appId) {
+ initSharedInstance: function(orgId, appId) {
Object.setPrototypeOf(Usergrid, new UsergridClient(orgId, appId))
}
}
-Usergrid.init = Usergrid.initialize
+Usergrid.init = Usergrid.initSharedInstance
module.exports = Usergrid
\ No newline at end of file