Skip to content
This repository has been archived by the owner on Jan 5, 2022. It is now read-only.

Commit

Permalink
Refactored UsergridRequest to handle all requests, added additional h…
Browse files Browse the repository at this point in the history
…elpers
  • Loading branch information
brandonscript committed Jan 14, 2016
1 parent 5e9f827 commit 65f7769
Show file tree
Hide file tree
Showing 24 changed files with 419 additions and 239 deletions.
7 changes: 7 additions & 0 deletions helpers/args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

var _ = require('lodash')

module.exports = function(args) {
return _.flattenDeep(Array.prototype.slice.call(args))
}
33 changes: 17 additions & 16 deletions helpers/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ var urljoin = require('url-join'),
_ = require('lodash')

module.exports = {
url: function(client, options) {
uri: function(client, options) {
return urljoin(
client.baseUrl,
client.orgId,
client.appId,
options.type,
options.path || options.type,
_.isString(options.uuidOrName) ? options.uuidOrName : ""
)
},
Expand Down Expand Up @@ -84,16 +84,15 @@ module.exports = {

var options = {
client: client,
method: 'GET'
method: 'GET',
callback: helpers.cb(args)
}

// if a preformatted options argument passed, assign it to options
if (_.isObject(args[0]) && !_.isFunction(args[0]) && args.length <= 2) {
_.assign(options, args[0])
}

options.callback = helpers.cb(_.last(args.filter(_.isFunction)))

options.type = _.first([options.type, ok(args).getIfExists('0._type'), args[0]].filter(_.isString))

options.query = _.first([options.query, args[0]].filter(function(property) {
Expand Down Expand Up @@ -131,16 +130,15 @@ module.exports = {

var options = {
client: client,
method: 'PUT'
method: 'PUT',
callback: helpers.cb(args)
}

// if a preformatted options argument passed, assign it to options
if (_.isObject(args[0]) && !_.isFunction(args[0]) && args.length <= 2) {
_.assign(options, args[0])
}

options.callback = helpers.cb(_.last(args.filter(_.isFunction)))

options.body = _.first([options.entity, options.body, args[2], args[1], args[0]].filter(function(property) {
return _.isObject(property) && !_.isFunction(property) && !(property instanceof UsergridQuery)
}))
Expand Down Expand Up @@ -179,16 +177,15 @@ module.exports = {

var options = {
client: client,
method: 'POST'
method: 'POST',
callback: helpers.cb(args)
}

// if a preformatted options argument passed, assign it to options
if (_.isObject(args[0]) && !_.isFunction(args[0]) && args.length <= 2) {
_.assign(options, args[0])
}

options.callback = helpers.cb(_.last(args.filter(_.isFunction)))

options.body = _.first([options.entities, options.entity, options.body, args[1], args[0]].filter(function(property) {
return _.isArray(property) && _.isObject(property[0]) && !_.isFunction(property[0]) || _.isObject(property) && !_.isFunction(property)
}))
Expand Down Expand Up @@ -219,15 +216,15 @@ module.exports = {

var options = {
client: client,
method: 'DELETE'
method: 'DELETE',
callback: helpers.cb(args)
}

// if a preformatted options argument passed, assign it to options
if (_.isObject(args[0]) && !_.isFunction(args[0]) && args.length <= 2) {
_.assign(options, args[0])
}

options.callback = helpers.cb(_.last(args.filter(_.isFunction)))
options.type = _.first([options.type, ok(options).getIfExists('entity.type'), args[0]._type, args[0]].filter(_.isString))
options.entity = _.first([options.entity, args[0]].filter(function(property) {
return (property instanceof UsergridEntity)
Expand All @@ -245,7 +242,7 @@ module.exports = {

return options
},
connection: function(client, args) {
connection: function(client, method, args) {

/* connect supports the following constructor patterns:
Expand Down Expand Up @@ -277,9 +274,11 @@ module.exports = {
*/

var options = {
client: client,
method: method,
entity: {},
to: {},
callback: helpers.cb(_.last(args.filter(_.isFunction)))
callback: helpers.cb(args)
}

// if a preformatted options argument passed, assign it to options
Expand Down Expand Up @@ -367,7 +366,9 @@ module.exports = {
*/

var options = {
callback: helpers.cb(_.last(args.filter(_.isFunction)))
client: client,
method: 'GET',
callback: helpers.cb(args)
}

// if a preformatted options argument passed, assign it to options
Expand Down
9 changes: 6 additions & 3 deletions helpers/cb.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
'use strict'

var _ = require('lodash')
var ok = require('objectkit'),
_ = require('lodash')

module.exports = function(callback) {
return _.isFunction(callback) ? callback : function() {}
module.exports = function() {
var args = _.flattenDeep(Array.prototype.slice.call(arguments))
var emptyFunc = function() {}
return _.first(_.flattenDeep([args.reverse(), ok(args).getIfExists('0.callback'), emptyFunc]).filter(_.isFunction))
}
2 changes: 1 addition & 1 deletion helpers/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
} else if (Usergrid.isInitialized) {
client = Usergrid
} else {
throw new Error("This method requires a valid UsergridClient instance (or the Usergrid shared instance) to be initialized")
throw new Error("This method requires a valid UsergridClient instance as an argument (or the Usergrid shared instance to be initialized)")
}
return client
}
Expand Down
8 changes: 6 additions & 2 deletions helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
'use strict'

var client = require('./client'),
var args = require('./args'),
client = require('./client'),
cb = require('./cb'),
build = require('./build'),
query = require('./query'),
config = require('./config'),
time = require('./time'),
mutability = require('./mutability'),
user = require('./user'),
_ = require('lodash')

// by mixing this in here, lodash-uuid is available everywhere lodash is used.
_.mixin(require('lodash-uuid'))

module.exports = _.assign(module.exports, {
args: args,
client: client,
cb: cb,
build: build,
query: query,
config: config,
time: time
time: time,
user: user
}, mutability)
9 changes: 9 additions & 0 deletions helpers/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'

var _ = require('lodash')

module.exports = {
uniqueId: function(user) {
return _.first([user.uuid, user.username, user.email].filter(_.isString))
}
}
3 changes: 2 additions & 1 deletion lib/appAuth.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict'

var UsergridAuth = require('./auth'),
helpers = require('../helpers'),
util = require('util'),
_ = require('lodash')

var UsergridAppAuth = function(options) {
var self = this
var args = _.flatten(Array.prototype.slice.call(arguments), true)
var args = _.flattenDeep(helpers.args(arguments))
if (_.isPlainObject(args[0])) {
options = args[0]
}
Expand Down
10 changes: 6 additions & 4 deletions lib/auth.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict'

var UsergridAuth = function() {
var UsergridAuth = function(token, expiry) {
var self = this

self.token = undefined
self.expiry = 0
self.token = token || undefined
self.expiry = expiry || 0

var usingToken = (token) ? true : false

Object.defineProperty(self, "hasToken", {
get: function() {
Expand All @@ -15,7 +17,7 @@ var UsergridAuth = function() {

Object.defineProperty(self, "isExpired", {
get: function() {
return (Date.now() >= self.expiry)
return usingToken || (Date.now() >= self.expiry)
},
configurable: true
})
Expand Down
90 changes: 27 additions & 63 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict'

var UsergridRequest = require('./request'),
request = require('request'),
helpers = require('../helpers'),
var helpers = require('../helpers'),
UsergridRequest = require('./request'),
UsergridResponse = require('./response'),
UsergridResponseError = require('./responseError'),
UsergridAuth = require('./auth'),
Expand Down Expand Up @@ -54,64 +53,32 @@ var UsergridClient = function(options) {

UsergridClient.prototype = {
GET: function() {
return new UsergridRequest(helpers.build.GET(this, Array.prototype.slice.call(arguments)))
return new UsergridRequest(helpers.build.GET(this, helpers.args(arguments)))
},
PUT: function() {
return new UsergridRequest(helpers.build.PUT(this, Array.prototype.slice.call(arguments)))
return new UsergridRequest(helpers.build.PUT(this, helpers.args(arguments)))
},
POST: function() {
return new UsergridRequest(helpers.build.POST(this, Array.prototype.slice.call(arguments)))
return new UsergridRequest(helpers.build.POST(this, helpers.args(arguments)))
},
DELETE: function() {
return new UsergridRequest(helpers.build.DELETE(this, Array.prototype.slice.call(arguments)))
return new UsergridRequest(helpers.build.DELETE(this, helpers.args(arguments)))
},
connect: function() {
var Usergrid = require('../usergrid')
if (this === Usergrid && !Usergrid.isInitialized) {
throw new Error('The Usergrid shared instance has not been initialized')
}
var options = helpers.build.connection(this, Array.prototype.slice.call(arguments))
request({
uri: options.uri,
headers: helpers.build.headers(this),
method: 'POST',
json: true
}, function(error, response) {
var usergridResponse = new UsergridResponse(response)
options.callback(error || usergridResponse.error, usergridResponse, usergridResponse.entities)
})
return new UsergridRequest(helpers.build.connection(this, 'POST', helpers.args(arguments)))
},
disconnect: function() {
var options = helpers.build.connection(this, Array.prototype.slice.call(arguments))
request({
uri: options.uri,
headers: helpers.build.headers(this),
method: 'DELETE',
json: true
}, function(error, response) {
var usergridResponse = new UsergridResponse(response)
options.callback(error || usergridResponse.error, usergridResponse, usergridResponse.entities)
})
return new UsergridRequest(helpers.build.connection(this, 'DELETE', helpers.args(arguments)))
},
getConnections: function() {
var options = helpers.build.getConnections(this, Array.prototype.slice.call(arguments))
request({
uri: options.uri,
headers: helpers.build.headers(this),
method: 'GET',
json: true
}, function(error, response) {
var usergridResponse = new UsergridResponse(response)
options.callback(error || usergridResponse.error, usergridResponse, usergridResponse.entities)
})
getConnections: function() {
return new UsergridRequest(helpers.build.getConnections(this, helpers.args(arguments)))
},
setAppAuth: function(options) {
this.appAuth = (typeof options === 'string') ? Array.prototype.slice.call(arguments) : options
this.appAuth = (typeof options === 'string') ? helpers.args(arguments) : options
},
authenticateApp: function(options, callback) {
authenticateApp: function(options) {
var self = this
callback = helpers.cb(callback || options)

var callback = helpers.cb(helpers.args(arguments))
var auth = (options instanceof UsergridAppAuth) ? options : self.appAuth || new UsergridAppAuth(options)

if (!(auth instanceof UsergridAppAuth)) {
Expand All @@ -120,39 +87,36 @@ UsergridClient.prototype = {
throw new Error('authenticateApp() failed because clientId or clientSecret are missing')
}

auth.type = 'token'

request({
uri: helpers.build.url(self, auth),
headers: helpers.build.headers(self),
body: helpers.build.appLoginBody(auth),
var options = {
client: self,
path: 'token',
method: 'POST',
json: true
}, function(error, response, body) {
if (response.statusCode === 200) {
body: helpers.build.appLoginBody(auth)
}

return new UsergridRequest(options, function(error, usergridResponse, body) {
if (usergridResponse.ok) {
if (!self.appAuth) {
self.appAuth = auth
}
self.appAuth.token = body.access_token
self.appAuth.expiry = helpers.time.expiry(body.expires_in)
self.appAuth.tokenTtl = body.expires_in
} else {
error = new UsergridResponseError(response.body)
}
callback(error, response, body.access_token)
callback(error, usergridResponse, body.access_token)
})
},
authenticateUser: function(options, callback) {
authenticateUser: function(options) {
var self = this
callback = helpers.cb(callback || options)
var callback = helpers.cb(helpers.args(arguments))

var UsergridUser = require('./user')
var currentUser = new UsergridUser(options)
currentUser.login(self, function(error, response, token) {
if (response.statusCode === 200) {
currentUser.login(self, function(error, usergridResponse, token) {
if (usergridResponse.ok) {
self.currentUser = currentUser
}
callback(error, response, token)
callback(error, usergridResponse, token)
})
}
}
Expand Down
Loading

0 comments on commit 65f7769

Please sign in to comment.