Rewrite of UsergridResponse to assign response properties to self, added loadNextPage()
diff --git a/lib/client.js b/lib/client.js
index 4fc5c87..2af1567 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -81,10 +81,7 @@
json: true
}, function(error, response) {
var usergridResponse = new UsergridResponse(response)
- if (response.statusCode !== 200) {
- error = new UsergridResponseError(response.body)
- }
- options.callback(error, usergridResponse, usergridResponse.entities)
+ options.callback(error || usergridResponse.error, usergridResponse, usergridResponse.entities)
})
},
disconnect: function() {
@@ -96,10 +93,7 @@
json: true
}, function(error, response) {
var usergridResponse = new UsergridResponse(response)
- if (response.statusCode !== 200) {
- error = new UsergridResponseError(response.body)
- }
- options.callback(error, usergridResponse, usergridResponse.entities)
+ options.callback(error || usergridResponse.error, usergridResponse, usergridResponse.entities)
})
},
getConnections: function() {
@@ -111,10 +105,7 @@
json: true
}, function(error, response) {
var usergridResponse = new UsergridResponse(response)
- if (response.statusCode !== 200) {
- error = new UsergridResponseError(response.body)
- }
- options.callback(error, usergridResponse, usergridResponse.entities)
+ options.callback(error || usergridResponse.error, usergridResponse, usergridResponse.entities)
})
},
setAppAuth: function(options) {
diff --git a/lib/query.js b/lib/query.js
index 792290b..a1d46b4 100644
--- a/lib/query.js
+++ b/lib/query.js
@@ -27,26 +27,35 @@
self._limit = value
return self
},
+ cursor: function(value) {
+ self._cursor = value
+ return self
+ },
eq: function(key, value) {
query = self.andJoin(util.format('%s = %s', key, helpers.query.useQuotesIfRequired(value)))
return self
},
+ equal: this.eq,
gt: function(key, value) {
query = self.andJoin(util.format('%s > %s', key, helpers.query.useQuotesIfRequired(value)))
return self
},
+ greaterThan: this.gt,
gte: function(key, value) {
query = self.andJoin(util.format('%s >= %s', key, helpers.query.useQuotesIfRequired(value)))
return self
},
+ greaterThanOrEqual: this.gte,
lt: function(key, value) {
query = self.andJoin(util.format('%s < %s', key, helpers.query.useQuotesIfRequired(value)))
return self
},
+ lessThan: this.lt,
lte: function(key, value) {
query = self.andJoin(util.format('%s <= %s', key, helpers.query.useQuotesIfRequired(value)))
return self
},
+ lessThanOrEqual: this.lte,
contains: function(key, value) {
query = self.andJoin(util.format('%s contains %s', key, helpers.query.useQuotesIfRequired(value)))
return self
@@ -95,7 +104,11 @@
// public accessors
Object.defineProperty(self, '_ql', {
get: function() {
- return queryString || util.format('select * where %s%s', query || '', sort || '')
+ if (queryString !== undefined) {
+ return queryString
+ } else {
+ return (query.length > 0 || sort !== undefined) ? util.format('select * where %s%s', query || '', sort || '') : ""
+ }
}
})
diff --git a/lib/request.js b/lib/request.js
index 1eb2223..ccc2250 100644
--- a/lib/request.js
+++ b/lib/request.js
@@ -19,14 +19,16 @@
authorization: util.format("Bearer %s", options.client.appAuth.token)
})
}
+
request(helpers.build.url(options.client, options), {
headers: headers,
body: options.body,
json: true,
method: options.method,
qs: (options.query instanceof UsergridQuery) ? {
- ql: options.query._ql,
- limit: options.query._limit
+ ql: options.query._ql || undefined,
+ limit: options.query._limit,
+ cursor: options.query._cursor
} : undefined
}, function(error, response) {
var usergridResponse = new UsergridResponse(response)
diff --git a/lib/response.js b/lib/response.js
index 31273db..6cc6997 100644
--- a/lib/response.js
+++ b/lib/response.js
@@ -1,14 +1,16 @@
'use strict'
var ok = require('objectkit'),
- UsergridResponseError = require('./responseError.js'),
+ UsergridQuery = require('./query'),
+ UsergridResponseError = require('./responseError'),
helpers = require('../helpers'),
_ = require('lodash')
-function UsergridResponse(response) {
+var UsergridResponse = function(response) {
+ var self = this
if (!response) {
return
- } else if (ok(response.body).has('entities')) {
+ } else if (ok(response.body).has('entities') && response.statusCode < 400) {
var UsergridEntity = require('./entity.js'),
UsergridUser = require('./user.js')
@@ -19,30 +21,48 @@
}
return entity
})
- _.assign(response, {
+
+ _.assign(self, response, {
metadata: _.cloneDeep(response.body),
entities: entities
})
- delete response.metadata.entities
- response.first = _.first(entities) || undefined
- response.entity = response.first
- response.last = _.last(entities) || undefined
- if (ok(response).getIfExists('metadata.path') === '/users') {
- response.user = response.first
- response.users = response.entities
+ delete self.metadata.entities
+ self.first = _.first(entities) || undefined
+ self.entity = self.first
+ self.last = _.last(entities) || undefined
+ if (ok(self).getIfExists('metadata.path') === '/users') {
+ self.user = self.first
+ self.users = self.entities
}
- Object.defineProperty(response, 'hasNextPage', {
+ Object.defineProperty(self, 'hasNextPage', {
get: function() {
- return ok(response).has('metadata.cursor')
+ return ok(self).has('metadata.cursor')
}
})
- helpers.setReadOnly(response.metadata)
+ helpers.setReadOnly(self.metadata)
} else {
- response.error = new UsergridResponseError(response.body)
+ _.assign(self, response, {
+ error: new UsergridResponseError(response.body)
+ })
}
- return response;
+ return self;
+}
+
+UsergridResponse.prototype = {
+ loadNextPage: function() {
+ var args = Array.prototype.slice.call(arguments)
+ var callback = helpers.cb(_.last(args.filter(_.isFunction)))
+ if (!this.metadata.cursor) {
+ callback()
+ }
+ var client = helpers.client.validate(args)
+ var type = _.last(ok(this).getIfExists('metadata.path').split('/'))
+ var limit = _.first(ok(this).getIfExists('metadata.params.limit'))
+ var query = new UsergridQuery(type).cursor(this.metadata.cursor).limit(limit)
+ return client.GET(query, callback)
+ }
}
module.exports = UsergridResponse
\ No newline at end of file
diff --git a/tests/lib/client.test.js b/tests/lib/client.test.js
index 4ca0772..5fac08a 100644
--- a/tests/lib/client.test.js
+++ b/tests/lib/client.test.js
@@ -339,7 +339,7 @@
it('should support updating a set of entities by passing an UsergridQuery object', function(done) {
this.slow(_slow + 1000)
- this.timeout(_timeout + 2000)
+ this.timeout(_timeout + 4000)
var query = new UsergridQuery(config.test.collection).eq('cuisine', 'pizza').limit(2)
var body = {
@@ -395,11 +395,17 @@
client.DELETE(config.test.collection, _uuid)
})
- it('should return a 200 ok', function() {
- // This should check for 404, but because of a Usergrid bug, it returns 401 instead of 404.
+ if (config.target === '1.0') {
+ // This should check for 404, but because of a Usergrid 1.0 bug, 401 instead of 404.
// see https://issues.apache.org/jira/browse/USERGRID-1128
- response.statusCode.should.not.equal(200)
- })
+ it('should return a 4XX status code', function() {
+ response.statusCode.should.be.greaterThanOrEqual(401)
+ })
+ } else {
+ it('should return a 404 not found', function() {
+ response.statusCode.should.equal(404)
+ })
+ }
if (config.target === '1.0') {
it('response.error.name should equal "service_resource_not_found"', function() {
diff --git a/tests/lib/query.test.js b/tests/lib/query.test.js
index ae39d53..446cb02 100644
--- a/tests/lib/query.test.js
+++ b/tests/lib/query.test.js
@@ -21,6 +21,11 @@
})
describe('_limit', function() {
+ it('_limit should equal 2', function() {
+ var query = new UsergridQuery('cats').limit(2)
+ query.should.have.property('_limit').equal(2)
+ })
+
it('_limit should equal 10', function() {
var query = new UsergridQuery('cats').limit(10)
query.should.have.property('_limit').equal(10)
@@ -28,6 +33,11 @@
})
describe('_ql', function() {
+ it('should be an empty string if query or sort are empty or underfined', function() {
+ var query = new UsergridQuery('cats')
+ query.should.have.property('_ql').equal("")
+ })
+
it('should support complex builder pattern syntax (chained constructor methods)', function() {
var query = new UsergridQuery('cats')
.gt('weight', 2.4)
diff --git a/tests/lib/response.test.js b/tests/lib/response.test.js
index 4e77443..d328eed 100644
--- a/tests/lib/response.test.js
+++ b/tests/lib/response.test.js
@@ -5,6 +5,8 @@
UsergridClient = require('../../lib/client'),
UsergridEntity = require('../../lib/entity'),
UsergridUser = require('../../lib/user'),
+ UsergridQuery = require('../../lib/query'),
+ UsergridResponse = require('../../lib/response'),
UsergridResponseError = require('../../lib/responseError'),
_ = require('lodash')
@@ -122,7 +124,7 @@
})
it('response.entity should be a reference to response.first', function() {
- _response.entity.should.deepEqual(_response.first)
+ _response.should.have.property('entity').deepEqual(_response.first)
})
})
@@ -150,4 +152,40 @@
done()
})
})
+})
+
+describe('loadNextPage()', function() {
+ this.slow(_slow + 800)
+ this.timeout(_timeout + 2000)
+
+ var firstResponse
+
+ before(function(done) {
+
+ this.slow(_slow)
+ this.timeout(_timeout)
+
+ var query = new UsergridQuery(config.test.collection).limit(2)
+
+ client.GET(query, function(err, usergridResponse) {
+ firstResponse = usergridResponse
+ done()
+ })
+ })
+
+ it('should load a new page of entities using the Usergrid shared instance', function(done) {
+ firstResponse.loadNextPage(function(err, usergridResponse) {
+ usergridResponse.first.uuid.should.not.equal(firstResponse.first.uuid)
+ usergridResponse.entities.length.should.equal(2)
+ done()
+ })
+ })
+
+ it('should load a new page of entities using a UsergridClient instance argument', function(done) {
+ firstResponse.loadNextPage(client, function(err, usergridResponse) {
+ usergridResponse.first.uuid.should.not.equal(firstResponse.first.uuid)
+ usergridResponse.entities.length.should.equal(2)
+ done()
+ })
+ })
})
\ No newline at end of file