-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from FunnelEnvy/master
Pagination for Asset Type URL
- Loading branch information
Showing
9 changed files
with
185 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/node_modules/ | ||
/.idea/ | ||
/.history | ||
*.iml | ||
test/helper/config.bk.js | ||
package-lock.json |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
var offset = require('./offset'); | ||
var token = require('./token'); | ||
var util = require('../util'); | ||
var _ = require('lodash'); | ||
|
||
module.exports = getNextPageFn; | ||
|
||
function getNextPageFn(conn, method, args) { | ||
var assetStrategy = { | ||
offset, | ||
token | ||
}; | ||
|
||
var options = _.clone(_.last(args) || {}); | ||
args = _.clone(args); | ||
args.pop(); | ||
|
||
var selectedStrategy = assetStrategy[util.nextPageType(args[0])] | ||
return selectedStrategy(conn, method, args, options); | ||
} |
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,16 @@ | ||
var _ = require('lodash'); | ||
|
||
module.exports = (conn, method, args, options) => { | ||
return () => { | ||
var params = options; | ||
if (method === 'get') { | ||
params = options.query = options.query || {}; | ||
} else if (method === 'post' || method === 'put') { | ||
params = options.data = options.data || {}; | ||
} | ||
|
||
params.offset = (params.offset || 0) + (params.maxReturn || 20); | ||
|
||
return conn._request.apply(conn, _.flatten([method, args, options], true)); | ||
} | ||
}; |
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,16 @@ | ||
var _ = require('lodash'); | ||
|
||
module.exports = (conn, method, args, options) => { | ||
return (nextPageToken) => { | ||
var params = options; | ||
if (method === 'get') { | ||
params = options.query = options.query || {}; | ||
} else if (method === 'post' || method === 'put') { | ||
params = options.data = options.data || {}; | ||
} | ||
|
||
params.nextPageToken = nextPageToken; | ||
|
||
return conn._request.apply(conn, _.flatten([method, args, options], true)); | ||
} | ||
}; |
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
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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
const _ = require('lodash'), | ||
Promise = require('bluebird'), | ||
assert = require('assert'), | ||
util = require('../lib/util'), | ||
nock = require('nock'), | ||
rewire = require('rewire'); | ||
|
||
const Connection = rewire("../lib/connection.js") | ||
Connection.__set__("getNextPageFn", getNextPageFn) | ||
function getNextPageFn(conn, method, args) { | ||
var assetStrategy = { | ||
offset: () => { | ||
return () => 'offset' | ||
}, | ||
token: () => { | ||
return () => 'token' | ||
}, | ||
}; | ||
|
||
var options = _.clone(_.last(args) || {}); | ||
args = _.clone(args); | ||
args.pop(); | ||
|
||
var selectedStrategy = assetStrategy[util.nextPageType(args[0])] | ||
return selectedStrategy(conn, method, args, options); | ||
} | ||
|
||
const assetScope = nock('http://localhost-mock') | ||
.filteringRequestBody(body => { | ||
console.log(body); | ||
return true; | ||
}) | ||
.post('/asset') | ||
.reply(200, { | ||
"success": true, | ||
"errors": [], | ||
"requestId": "6efc#16c8967a21f", | ||
"warnings": [], | ||
"result": [ | ||
{ | ||
"id": 4363, | ||
"name": "Smart List Test 01" | ||
} | ||
] | ||
}); | ||
|
||
const tokenScope = nock('http://localhost-mock') | ||
.filteringRequestBody(body => { | ||
console.log(body); | ||
return true; | ||
}) | ||
.post('/rest/v1') | ||
.reply(200, { | ||
"moreResult": true, | ||
"nextPageToken": "string", | ||
"requestId": "string", | ||
"result": [ | ||
{ | ||
"id": 0, | ||
"status": "string" | ||
} | ||
], | ||
"success": true | ||
}); | ||
|
||
const ASSET_URL = 'asset', | ||
TOKEN_URL = 'rest/v1', | ||
IDENTITY_URL = 'identity'; | ||
|
||
Connection.prototype.getOAuthToken = function() { | ||
return Promise.resolve({access_token: 'test_token'}); | ||
} | ||
|
||
function getUrl(path) { | ||
return 'http://localhost-mock/' + path; | ||
} | ||
|
||
function getConnection() { | ||
var options = { | ||
endpoint: getUrl(''), | ||
identity: getUrl(IDENTITY_URL), | ||
clientId: 'someId', | ||
clientSecret: 'someSecret' | ||
}; | ||
return new Connection(options); | ||
} | ||
|
||
describe('Connection', function() { | ||
it('token type pagination', function() { | ||
return getConnection().post(TOKEN_URL, {data: {_method: 'GET', maxReturn: 200}}).then(resp => { | ||
assert.equal(resp.nextPage(), 'token') | ||
}); | ||
}); | ||
|
||
it('offset type pagination', function() { | ||
return getConnection().post(ASSET_URL, {data: {_method: 'GET', maxReturn: 200}}).then(resp => { | ||
assert.equal(resp.nextPage(), 'offset') | ||
}); | ||
}); | ||
}); | ||
|