diff --git a/history.md b/history.md index 2d9bf3a..57148b8 100644 --- a/history.md +++ b/history.md @@ -1,3 +1,7 @@ +# v1.3.10 - 2018/08/07 + +* Added getProfile and updateProfile commands to provision proxy + # v1.3.9 - 2018/08/01 * Added support for Music/findBroadcastsByStationId route diff --git a/lib/provision.js b/lib/provision.js index 397d455..c890df4 100644 --- a/lib/provision.js +++ b/lib/provision.js @@ -46,6 +46,28 @@ module.exports = function (provisionOptions, ensureAuthHeaders, self) { req.on(EVENT_REQUEST, (data) => (self.emit(EVENT_REQUEST, data))); req.on(EVENT_RESPONSE, (data) => (self.emit(EVENT_RESPONSE, data))); + self.allApplications = (options, callback) => { + // handle any non-specified input params + if (typeof options === 'function') { + callback = options; + options = undefined; + } + + let exec = co(function *() { + + let headers = yield ensureAuthHeaders(options); + + return yield req.get({ + headers : headers, + pathname : '/v2/applications', + query : options, + rawStream : true + }); + }); + + return validation.promiseOrCallback(exec, callback); + }; + self.allOrders = (options, callback) => { // handle any non-specified input params if (typeof options === 'function') { @@ -66,6 +88,26 @@ module.exports = function (provisionOptions, ensureAuthHeaders, self) { return validation.promiseOrCallback(exec, callback); }; + self.allDevices = (options, callback) => { + // handle any non-specified input params + if (typeof options === 'function') { + callback = options; + options = undefined; + } + + let exec = co(function *() { + let headers = yield ensureAuthHeaders(options); + + return yield req.get({ + headers : headers, + pathname : '/v2/devices', + query : options + }); + }); + + return validation.promiseOrCallback(exec, callback); + }; + self.call = (options, callback) => { if (typeof options === 'function') { callback = options; @@ -98,6 +140,55 @@ module.exports = function (provisionOptions, ensureAuthHeaders, self) { return validation.promiseOrCallback(exec, callback); }; + self.checkUpdate = (options, callback) => { + // handle any non-specified input params + if (typeof options === 'function') { + callback = options; + options = undefined; + } + + let exec = co(function *() { + + let headers = yield ensureAuthHeaders(options); + + return yield req.head({ + headers : headers, + pathname : '/v2/applications', + query : options, + rawStream : true + }); + }); + + return validation.promiseOrCallback(exec, callback); + }; + + self.createDevice = (device, callback) => { + // handle any non-specified input params + if (typeof device === 'function') { + callback = device; + device = undefined; + } + + let exec = co(function *() { + if (validation.isEmpty(device)) { + return yield Promise.reject(new Error('device is required')); + } + + if (validation.isEmpty(device.deviceId)) { + return yield Promise.reject(new Error('deviceId is required')); + } + + let headers = yield ensureAuthHeaders(); + + return yield req.post({ + headers : headers, + pathname : '/v2/devices' + }, device); + }); + + return validation.promiseOrCallback(exec, callback); + }; + self.createOrder = (order, callback) => { // handle any non-specified input params if (typeof order === 'function') { @@ -137,29 +228,42 @@ module.exports = function (provisionOptions, ensureAuthHeaders, self) { return validation.promiseOrCallback(exec, callback); }; - self.checkUpdate = (options, callback) => { + self.createProfile = (profile, callback) => { // handle any non-specified input params - if (typeof options === 'function') { - callback = options; - options = undefined; + if (typeof profile === 'function') { + callback = profile; + profile = undefined; } let exec = co(function *() { + if (validation.isEmpty(profile)) { + return yield Promise.reject(new Error('profile is required')); + } - let headers = yield ensureAuthHeaders(options); + if (validation.isEmpty(profile.clientId)) { + return yield Promise.reject(new Error('clientId is required')); + } - return yield req.head({ + if (validation.isEmpty(profile.applications)) { + return yield Promise.reject(new Error('applications are required')); + } + + if (validation.isEmpty(profile.template)) { + return yield Promise.reject(new Error('template is required')); + } + + let headers = yield ensureAuthHeaders(); + + return yield req.post({ headers : headers, - pathname : '/v2/applications', - query : options, - rawStream : true - }); + pathname : '/v2/profiles' + }, profile); }); return validation.promiseOrCallback(exec, callback); }; - self.allApplications = (options, callback) => { + self.getClientCredentials = (deviceId, options, callback) => { // handle any non-specified input params if (typeof options === 'function') { callback = options; @@ -167,27 +271,35 @@ module.exports = function (provisionOptions, ensureAuthHeaders, self) { } let exec = co(function *() { + if (validation.isEmpty(deviceId)) { + return yield Promise.reject(new Error('deviceId is required')); + } let headers = yield ensureAuthHeaders(options); return yield req.get({ headers : headers, - pathname : '/v2/applications', - query : options, - rawStream : true + pathname : ['/v2/devices/', deviceId.replace(/\:/g, ''), '/activation'].join(''), + query : options }); }); return validation.promiseOrCallback(exec, callback); }; - self.getClientCredentials = (deviceId, options, callback) => { + self.getDevice = (deviceId, options, callback) => { // handle any non-specified input params if (typeof options === 'function') { callback = options; options = undefined; } + if (typeof deviceId === 'function') { + callback = deviceId; + deviceId = undefined; + options = undefined; + } + let exec = co(function *() { if (validation.isEmpty(deviceId)) { return yield Promise.reject(new Error('deviceId is required')); @@ -197,7 +309,7 @@ module.exports = function (provisionOptions, ensureAuthHeaders, self) { return yield req.get({ headers : headers, - pathname : ['/v2/devices/', deviceId.replace(/\:/g, ''), '/activation'].join(''), + pathname : `/v2/devices/${deviceId}`, query : options }); }); @@ -235,8 +347,65 @@ module.exports = function (provisionOptions, ensureAuthHeaders, self) { return validation.promiseOrCallback(exec, callback); }; + self.getProfile = (profileAlias, options, callback) => { + // handle any non-specified input params + if (typeof options === 'function') { + callback = options; + options = undefined; + } + + if (typeof profileAlias === 'function') { + callback = profileAlias; + profileAlias = undefined; + options = undefined; + } + + let exec = co(function *() { + if (validation.isEmpty(profileAlias)) { + return yield Promise.reject(new Error('profileAlias is required')); + } + + let headers = yield ensureAuthHeaders(options); + + return yield req.get({ + headers : headers, + pathname : `/v2/profiles/${profileAlias}`, + query : options + }); + }); + + return validation.promiseOrCallback(exec, callback); + }; + self.settings = () => (settings); + self.updateDevice = (device, callback) => { + // handle any non-specified input params + if (typeof device === 'function') { + callback = device; + device = undefined; + } + + let exec = co(function *() { + if (validation.isEmpty(device)) { + return yield Promise.reject(new Error('device is required')); + } + + if (validation.isEmpty(device.deviceId)) { + return yield Promise.reject(new Error('deviceId is required')); + } + + let headers = yield ensureAuthHeaders(); + + return yield req.put({ + headers : headers, + pathname : `/v2/devices/${device.deviceId}` + }, device); + }); + + return validation.promiseOrCallback(exec, callback); + }; + self.updateOrder = (order, callback) => { // handle any non-specified input params if (typeof order === 'function') { @@ -264,6 +433,48 @@ module.exports = function (provisionOptions, ensureAuthHeaders, self) { return validation.promiseOrCallback(exec, callback); }; + self.updateProfile = (profileAlias, profile, callback) => { + // handle any non-specified input params + if (typeof profile === 'function') { + callback = profile; + profile = undefined; + } + + if (typeof profileAlias === 'object') { + profile = profileAlias; + profileAlias = undefined; + } + + if (typeof profileAlias === 'function') { + callback = profileAlias; + profileAlias = undefined; + profile = undefined; + } + + let exec = co(function *() { + if (validation.isEmpty(profile)) { + return yield Promise.reject(new Error('profile is required')); + } + + if (validation.isEmpty(profileAlias)) { + profileAlias = profile.profileId + } + + if (validation.isEmpty(profileAlias)) { + return yield Promise.reject(new Error('profileAlias is required')); + } + + let headers = yield ensureAuthHeaders(); + + return yield req.put({ + headers : headers, + pathname : `/v2/profiles/${profileAlias}` + }, profile); + }); + + return validation.promiseOrCallback(exec, callback); + }; + self.version = (callback) => { let exec = co(function *() { return yield req.get({ diff --git a/package.json b/package.json index e39de09..851cd98 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "playnetwork-sdk", - "version": "1.3.9", + "version": "1.3.10", "contributors": [ { "name": "Joshua Thomas", diff --git a/test/lib/provision.js b/test/lib/provision.js index 0a1f7be..a543674 100644 --- a/test/lib/provision.js +++ b/test/lib/provision.js @@ -552,6 +552,64 @@ describe('provision', () => { }); }); + describe('#getProfile', () => { + it('should require a profileAlias (promise)', (done) => { + provision.getProfile() + .then(() => { + return done(new Error('should require profileAlias')); + }) + .catch((err) => { + should.exist(err); + should.exist(err.message); + err.message.should.contain('profileAlias is required'); + + return done(); + }); + }); + + it('should require profileAlias (callback)', (done) => { + provision.getProfile(function (err, result) { + should.exist(err); + should.exist(err.message); + err.message.should.contain('profileAlias is required'); + should.not.exist(result); + + return done(); + }); + }); + + it('should properly retrieve profile (promise)', (done) => { + // intercept outbound request + nock('https://provision-api.apps.playnetwork.com') + .get('/v2/profiles/test') + .reply(200, { total : 0 }); + + provision.getProfile('test') + .then((result) => { + should.exist(result); + should.exist(requestInfo); + + return done(); + }) + .catch((err) => (done(err))); + }); + + it('should properly retrieve profile (callback)', (done) => { + // intercept outbound request + nock('https://provision-api.apps.playnetwork.com') + .get('/v2/profiles/test') + .reply(200, { total : 0 }); + + provision.getProfile('test', function (err, result) { + should.not.exist(err); + should.exist(result); + should.exist(requestInfo); + + return done(); + }); + }); + }); + describe('#updateOrder', () => { let mockOrder = { orderId : 'test' @@ -625,6 +683,126 @@ describe('provision', () => { }); }); + describe('#updateProfile', () => { + let + mockProfileAlias = 'clientId:abc123', + mockProfile = { + profileId : 'test' + }; + + it('should require profile details (promise)', (done) => { + provision.updateProfile() + .then(() => { + return done(new Error('should require profile')); + }) + .catch((err) => { + should.exist(err); + should.exist(err.message); + err.message.should.contain('profile is required'); + + return done(); + }) + }); + + it('should require profile details with profileAlias (promise)', (done) => { + provision.updateProfile('test') + .then(() => { + return done(new Error('should require profile')); + }) + .catch((err) => { + should.exist(err); + should.exist(err.message); + err.message.should.contain('profile is required'); + + return done(); + }) + }); + + it('should require profile details (callback)', (done) => { + provision.updateProfile(function (err, result) { + should.exist(err); + should.exist(err.message); + err.message.should.contain('profile is required'); + should.not.exist(result); + + return done(); + }); + }); + + it('should require profile details with profileAlias (callback)', (done) => { + provision.updateProfile('test', function (err, result) { + should.exist(err); + should.exist(err.message); + err.message.should.contain('profile is required'); + should.not.exist(result); + + return done(); + }); + }); + + it('should properly update profile with profileAlias (promise)', (done) => { + // intercept outbound request + nock('https://provision-api.apps.playnetwork.com') + .put(`/v2/profiles/${mockProfileAlias}`) + .reply(200, mockProfile); + + provision.updateProfile(mockProfileAlias, mockProfile) + .then((result) => { + should.exist(result); + should.exist(requestInfo); + + return done(); + }) + .catch((err) => (done(err))); + }); + + it('should properly update profile with profileAlias (callback)', (done) => { + // intercept outbound request + nock('https://provision-api.apps.playnetwork.com') + .put(`/v2/profiles/${mockProfileAlias}`) + .reply(200, mockProfile); + + provision.updateProfile(mockProfileAlias, mockProfile, function (err, result) { + should.not.exist(err); + should.exist(result); + should.exist(requestInfo); + + return done(); + }); + }); + + it('should properly update profile without profileAlias (promise)', (done) => { + // intercept outbound request + nock('https://provision-api.apps.playnetwork.com') + .put('/v2/profiles/test') + .reply(200, mockProfile); + + provision.updateProfile(mockProfile) + .then((result) => { + should.exist(result); + should.exist(requestInfo); + + return done(); + }) + .catch((err) => (done(err))); + }); + + it('should properly update profile without profileAlias (callback)', (done) => { + // intercept outbound request + nock('https://provision-api.apps.playnetwork.com') + .put('/v2/profiles/test') + .reply(200, mockProfile); + + provision.updateProfile(mockProfile, function (err, result) { + should.not.exist(err); + should.exist(result); + should.exist(requestInfo); + + return done(); + }); + }); + }); + describe('#version', () => { it('should properly return version (promise)', (done) => { // intercept outbound request