Source: rest/flexApi/v1/webChannel.js

  1. 'use strict';
  2. /* jshint ignore:start */
  3. /**
  4. * This code was generated by
  5. * \ / _ _ _| _ _
  6. * | (_)\/(_)(_|\/| |(/_ v1.0.0
  7. * / /
  8. */
  9. /* jshint ignore:end */
  10. var Q = require('q'); /* jshint ignore:line */
  11. var _ = require('lodash'); /* jshint ignore:line */
  12. var util = require('util'); /* jshint ignore:line */
  13. var Page = require('../../../base/Page'); /* jshint ignore:line */
  14. var deserialize = require(
  15. '../../../base/deserialize'); /* jshint ignore:line */
  16. var values = require('../../../base/values'); /* jshint ignore:line */
  17. var WebChannelList;
  18. var WebChannelPage;
  19. var WebChannelInstance;
  20. var WebChannelContext;
  21. /* jshint ignore:start */
  22. /**
  23. * Initialize the WebChannelList
  24. *
  25. * @constructor Twilio.FlexApi.V1.WebChannelList
  26. *
  27. * @param {Twilio.FlexApi.V1} version - Version of the resource
  28. */
  29. /* jshint ignore:end */
  30. WebChannelList = function WebChannelList(version) {
  31. /* jshint ignore:start */
  32. /**
  33. * @function webChannel
  34. * @memberof Twilio.FlexApi.V1#
  35. *
  36. * @param {string} sid - sid of instance
  37. *
  38. * @returns {Twilio.FlexApi.V1.WebChannelContext}
  39. */
  40. /* jshint ignore:end */
  41. function WebChannelListInstance(sid) {
  42. return WebChannelListInstance.get(sid);
  43. }
  44. WebChannelListInstance._version = version;
  45. // Path Solution
  46. WebChannelListInstance._solution = {};
  47. WebChannelListInstance._uri = `/WebChannels`;
  48. /* jshint ignore:start */
  49. /**
  50. * Streams WebChannelInstance records from the API.
  51. *
  52. * This operation lazily loads records as efficiently as possible until the limit
  53. * is reached.
  54. *
  55. * The results are passed into the callback function, so this operation is memory
  56. * efficient.
  57. *
  58. * If a function is passed as the first argument, it will be used as the callback
  59. * function.
  60. *
  61. * @function each
  62. * @memberof Twilio.FlexApi.V1.WebChannelList#
  63. *
  64. * @param {object} [opts] - Options for request
  65. * @param {number} [opts.limit] -
  66. * Upper limit for the number of records to return.
  67. * each() guarantees never to return more than limit.
  68. * Default is no limit
  69. * @param {number} [opts.pageSize] -
  70. * Number of records to fetch per request,
  71. * when not set will use the default value of 50 records.
  72. * If no pageSize is defined but a limit is defined,
  73. * each() will attempt to read the limit with the most efficient
  74. * page size, i.e. min(limit, 1000)
  75. * @param {Function} [opts.callback] -
  76. * Function to process each record. If this and a positional
  77. * callback are passed, this one will be used
  78. * @param {Function} [opts.done] -
  79. * Function to be called upon completion of streaming
  80. * @param {Function} [callback] - Function to process each record
  81. */
  82. /* jshint ignore:end */
  83. WebChannelListInstance.each = function each(opts, callback) {
  84. if (_.isFunction(opts)) {
  85. callback = opts;
  86. opts = {};
  87. }
  88. opts = opts || {};
  89. if (opts.callback) {
  90. callback = opts.callback;
  91. }
  92. if (_.isUndefined(callback)) {
  93. throw new Error('Callback function must be provided');
  94. }
  95. var done = false;
  96. var currentPage = 1;
  97. var currentResource = 0;
  98. var limits = this._version.readLimits({
  99. limit: opts.limit,
  100. pageSize: opts.pageSize
  101. });
  102. function onComplete(error) {
  103. done = true;
  104. if (_.isFunction(opts.done)) {
  105. opts.done(error);
  106. }
  107. }
  108. function fetchNextPage(fn) {
  109. var promise = fn();
  110. if (_.isUndefined(promise)) {
  111. onComplete();
  112. return;
  113. }
  114. promise.then(function(page) {
  115. _.each(page.instances, function(instance) {
  116. if (done || (!_.isUndefined(opts.limit) && currentResource >= opts.limit)) {
  117. done = true;
  118. return false;
  119. }
  120. currentResource++;
  121. callback(instance, onComplete);
  122. });
  123. if (!done) {
  124. currentPage++;
  125. fetchNextPage(_.bind(page.nextPage, page));
  126. }
  127. });
  128. promise.catch(onComplete);
  129. }
  130. fetchNextPage(_.bind(this.page, this, _.merge(opts, limits)));
  131. };
  132. /* jshint ignore:start */
  133. /**
  134. * Lists WebChannelInstance records from the API as a list.
  135. *
  136. * If a function is passed as the first argument, it will be used as the callback
  137. * function.
  138. *
  139. * @function list
  140. * @memberof Twilio.FlexApi.V1.WebChannelList#
  141. *
  142. * @param {object} [opts] - Options for request
  143. * @param {number} [opts.limit] -
  144. * Upper limit for the number of records to return.
  145. * list() guarantees never to return more than limit.
  146. * Default is no limit
  147. * @param {number} [opts.pageSize] -
  148. * Number of records to fetch per request,
  149. * when not set will use the default value of 50 records.
  150. * If no page_size is defined but a limit is defined,
  151. * list() will attempt to read the limit with the most
  152. * efficient page size, i.e. min(limit, 1000)
  153. * @param {function} [callback] - Callback to handle list of records
  154. *
  155. * @returns {Promise} Resolves to a list of records
  156. */
  157. /* jshint ignore:end */
  158. WebChannelListInstance.list = function list(opts, callback) {
  159. if (_.isFunction(opts)) {
  160. callback = opts;
  161. opts = {};
  162. }
  163. opts = opts || {};
  164. var deferred = Q.defer();
  165. var allResources = [];
  166. opts.callback = function(resource, done) {
  167. allResources.push(resource);
  168. if (!_.isUndefined(opts.limit) && allResources.length === opts.limit) {
  169. done();
  170. }
  171. };
  172. opts.done = function(error) {
  173. if (_.isUndefined(error)) {
  174. deferred.resolve(allResources);
  175. } else {
  176. deferred.reject(error);
  177. }
  178. };
  179. if (_.isFunction(callback)) {
  180. deferred.promise.nodeify(callback);
  181. }
  182. this.each(opts);
  183. return deferred.promise;
  184. };
  185. /* jshint ignore:start */
  186. /**
  187. * Retrieve a single page of WebChannelInstance records from the API.
  188. *
  189. * The request is executed immediately.
  190. *
  191. * If a function is passed as the first argument, it will be used as the callback
  192. * function.
  193. *
  194. * @function page
  195. * @memberof Twilio.FlexApi.V1.WebChannelList#
  196. *
  197. * @param {object} [opts] - Options for request
  198. * @param {string} [opts.pageToken] - PageToken provided by the API
  199. * @param {number} [opts.pageNumber] -
  200. * Page Number, this value is simply for client state
  201. * @param {number} [opts.pageSize] - Number of records to return, defaults to 50
  202. * @param {function} [callback] - Callback to handle list of records
  203. *
  204. * @returns {Promise} Resolves to a list of records
  205. */
  206. /* jshint ignore:end */
  207. WebChannelListInstance.page = function page(opts, callback) {
  208. if (_.isFunction(opts)) {
  209. callback = opts;
  210. opts = {};
  211. }
  212. opts = opts || {};
  213. var deferred = Q.defer();
  214. var data = values.of({
  215. 'PageToken': opts.pageToken,
  216. 'Page': opts.pageNumber,
  217. 'PageSize': opts.pageSize
  218. });
  219. var promise = this._version.page({uri: this._uri, method: 'GET', params: data});
  220. promise = promise.then(function(payload) {
  221. deferred.resolve(new WebChannelPage(this._version, payload, this._solution));
  222. }.bind(this));
  223. promise.catch(function(error) {
  224. deferred.reject(error);
  225. });
  226. if (_.isFunction(callback)) {
  227. deferred.promise.nodeify(callback);
  228. }
  229. return deferred.promise;
  230. };
  231. /* jshint ignore:start */
  232. /**
  233. * Retrieve a single target page of WebChannelInstance records from the API.
  234. *
  235. * The request is executed immediately.
  236. *
  237. * If a function is passed as the first argument, it will be used as the callback
  238. * function.
  239. *
  240. * @function getPage
  241. * @memberof Twilio.FlexApi.V1.WebChannelList#
  242. *
  243. * @param {string} [targetUrl] - API-generated URL for the requested results page
  244. * @param {function} [callback] - Callback to handle list of records
  245. *
  246. * @returns {Promise} Resolves to a list of records
  247. */
  248. /* jshint ignore:end */
  249. WebChannelListInstance.getPage = function getPage(targetUrl, callback) {
  250. var deferred = Q.defer();
  251. var promise = this._version._domain.twilio.request({method: 'GET', uri: targetUrl});
  252. promise = promise.then(function(payload) {
  253. deferred.resolve(new WebChannelPage(this._version, payload, this._solution));
  254. }.bind(this));
  255. promise.catch(function(error) {
  256. deferred.reject(error);
  257. });
  258. if (_.isFunction(callback)) {
  259. deferred.promise.nodeify(callback);
  260. }
  261. return deferred.promise;
  262. };
  263. /* jshint ignore:start */
  264. /**
  265. * create a WebChannelInstance
  266. *
  267. * @function create
  268. * @memberof Twilio.FlexApi.V1.WebChannelList#
  269. *
  270. * @param {object} opts - Options for request
  271. * @param {string} opts.flexFlowSid - The SID of the Flex Flow
  272. * @param {string} opts.identity - The chat identity
  273. * @param {string} opts.customerFriendlyName - The chat participant's friendly name
  274. * @param {string} opts.chatFriendlyName - The chat channel's friendly name
  275. * @param {string} [opts.chatUniqueName] - The chat channel's unique name
  276. * @param {string} [opts.preEngagementData] - The pre-engagement data
  277. * @param {function} [callback] - Callback to handle processed record
  278. *
  279. * @returns {Promise} Resolves to processed WebChannelInstance
  280. */
  281. /* jshint ignore:end */
  282. WebChannelListInstance.create = function create(opts, callback) {
  283. if (_.isUndefined(opts)) {
  284. throw new Error('Required parameter "opts" missing.');
  285. }
  286. if (_.isUndefined(opts.flexFlowSid)) {
  287. throw new Error('Required parameter "opts.flexFlowSid" missing.');
  288. }
  289. if (_.isUndefined(opts.identity)) {
  290. throw new Error('Required parameter "opts.identity" missing.');
  291. }
  292. if (_.isUndefined(opts.customerFriendlyName)) {
  293. throw new Error('Required parameter "opts.customerFriendlyName" missing.');
  294. }
  295. if (_.isUndefined(opts.chatFriendlyName)) {
  296. throw new Error('Required parameter "opts.chatFriendlyName" missing.');
  297. }
  298. var deferred = Q.defer();
  299. var data = values.of({
  300. 'FlexFlowSid': _.get(opts, 'flexFlowSid'),
  301. 'Identity': _.get(opts, 'identity'),
  302. 'CustomerFriendlyName': _.get(opts, 'customerFriendlyName'),
  303. 'ChatFriendlyName': _.get(opts, 'chatFriendlyName'),
  304. 'ChatUniqueName': _.get(opts, 'chatUniqueName'),
  305. 'PreEngagementData': _.get(opts, 'preEngagementData')
  306. });
  307. var promise = this._version.create({uri: this._uri, method: 'POST', data: data});
  308. promise = promise.then(function(payload) {
  309. deferred.resolve(new WebChannelInstance(this._version, payload, this._solution.sid));
  310. }.bind(this));
  311. promise.catch(function(error) {
  312. deferred.reject(error);
  313. });
  314. if (_.isFunction(callback)) {
  315. deferred.promise.nodeify(callback);
  316. }
  317. return deferred.promise;
  318. };
  319. /* jshint ignore:start */
  320. /**
  321. * Constructs a web_channel
  322. *
  323. * @function get
  324. * @memberof Twilio.FlexApi.V1.WebChannelList#
  325. *
  326. * @param {string} sid - The SID of the WebChannel resource to fetch
  327. *
  328. * @returns {Twilio.FlexApi.V1.WebChannelContext}
  329. */
  330. /* jshint ignore:end */
  331. WebChannelListInstance.get = function get(sid) {
  332. return new WebChannelContext(this._version, sid);
  333. };
  334. /* jshint ignore:start */
  335. /**
  336. * Provide a user-friendly representation
  337. *
  338. * @function toJSON
  339. * @memberof Twilio.FlexApi.V1.WebChannelList#
  340. *
  341. * @returns Object
  342. */
  343. /* jshint ignore:end */
  344. WebChannelListInstance.toJSON = function toJSON() {
  345. return this._solution;
  346. };
  347. WebChannelListInstance[util.inspect.custom] = function inspect(depth, options) {
  348. return util.inspect(this.toJSON(), options);
  349. };
  350. return WebChannelListInstance;
  351. };
  352. /* jshint ignore:start */
  353. /**
  354. * Initialize the WebChannelPage
  355. *
  356. * @constructor Twilio.FlexApi.V1.WebChannelPage
  357. *
  358. * @param {V1} version - Version of the resource
  359. * @param {Response<string>} response - Response from the API
  360. * @param {WebChannelSolution} solution - Path solution
  361. *
  362. * @returns WebChannelPage
  363. */
  364. /* jshint ignore:end */
  365. WebChannelPage = function WebChannelPage(version, response, solution) {
  366. // Path Solution
  367. this._solution = solution;
  368. Page.prototype.constructor.call(this, version, response, this._solution);
  369. };
  370. _.extend(WebChannelPage.prototype, Page.prototype);
  371. WebChannelPage.prototype.constructor = WebChannelPage;
  372. /* jshint ignore:start */
  373. /**
  374. * Build an instance of WebChannelInstance
  375. *
  376. * @function getInstance
  377. * @memberof Twilio.FlexApi.V1.WebChannelPage#
  378. *
  379. * @param {WebChannelPayload} payload - Payload response from the API
  380. *
  381. * @returns WebChannelInstance
  382. */
  383. /* jshint ignore:end */
  384. WebChannelPage.prototype.getInstance = function getInstance(payload) {
  385. return new WebChannelInstance(this._version, payload);
  386. };
  387. /* jshint ignore:start */
  388. /**
  389. * Provide a user-friendly representation
  390. *
  391. * @function toJSON
  392. * @memberof Twilio.FlexApi.V1.WebChannelPage#
  393. *
  394. * @returns Object
  395. */
  396. /* jshint ignore:end */
  397. WebChannelPage.prototype.toJSON = function toJSON() {
  398. let clone = {};
  399. _.forOwn(this, function(value, key) {
  400. if (!_.startsWith(key, '_') && ! _.isFunction(value)) {
  401. clone[key] = value;
  402. }
  403. });
  404. return clone;
  405. };
  406. WebChannelPage.prototype[util.inspect.custom] = function inspect(depth, options)
  407. {
  408. return util.inspect(this.toJSON(), options);
  409. };
  410. /* jshint ignore:start */
  411. /**
  412. * Initialize the WebChannelContext
  413. *
  414. * @constructor Twilio.FlexApi.V1.WebChannelInstance
  415. *
  416. * @property {string} accountSid -
  417. * The SID of the Account that created the resource and owns this Workflow
  418. * @property {string} flexFlowSid - The SID of the Flex Flow
  419. * @property {string} sid -
  420. * The unique string that identifies the WebChannel resource
  421. * @property {string} url - The absolute URL of the WebChannel resource
  422. * @property {Date} dateCreated -
  423. * The ISO 8601 date and time in GMT when the resource was created
  424. * @property {Date} dateUpdated -
  425. * The ISO 8601 date and time in GMT when the resource was last updated
  426. *
  427. * @param {V1} version - Version of the resource
  428. * @param {WebChannelPayload} payload - The instance payload
  429. * @param {sid} sid - The SID of the WebChannel resource to fetch
  430. */
  431. /* jshint ignore:end */
  432. WebChannelInstance = function WebChannelInstance(version, payload, sid) {
  433. this._version = version;
  434. // Marshaled Properties
  435. this.accountSid = payload.account_sid; // jshint ignore:line
  436. this.flexFlowSid = payload.flex_flow_sid; // jshint ignore:line
  437. this.sid = payload.sid; // jshint ignore:line
  438. this.url = payload.url; // jshint ignore:line
  439. this.dateCreated = deserialize.iso8601DateTime(payload.date_created); // jshint ignore:line
  440. this.dateUpdated = deserialize.iso8601DateTime(payload.date_updated); // jshint ignore:line
  441. // Context
  442. this._context = undefined;
  443. this._solution = {sid: sid || this.sid, };
  444. };
  445. Object.defineProperty(WebChannelInstance.prototype,
  446. '_proxy', {
  447. get: function() {
  448. if (!this._context) {
  449. this._context = new WebChannelContext(this._version, this._solution.sid);
  450. }
  451. return this._context;
  452. }
  453. });
  454. /* jshint ignore:start */
  455. /**
  456. * fetch a WebChannelInstance
  457. *
  458. * @function fetch
  459. * @memberof Twilio.FlexApi.V1.WebChannelInstance#
  460. *
  461. * @param {function} [callback] - Callback to handle processed record
  462. *
  463. * @returns {Promise} Resolves to processed WebChannelInstance
  464. */
  465. /* jshint ignore:end */
  466. WebChannelInstance.prototype.fetch = function fetch(callback) {
  467. return this._proxy.fetch(callback);
  468. };
  469. /* jshint ignore:start */
  470. /**
  471. * update a WebChannelInstance
  472. *
  473. * @function update
  474. * @memberof Twilio.FlexApi.V1.WebChannelInstance#
  475. *
  476. * @param {object} [opts] - Options for request
  477. * @param {web_channel.chat_status} [opts.chatStatus] - The chat status
  478. * @param {string} [opts.postEngagementData] - The post-engagement data
  479. * @param {function} [callback] - Callback to handle processed record
  480. *
  481. * @returns {Promise} Resolves to processed WebChannelInstance
  482. */
  483. /* jshint ignore:end */
  484. WebChannelInstance.prototype.update = function update(opts, callback) {
  485. return this._proxy.update(opts, callback);
  486. };
  487. /* jshint ignore:start */
  488. /**
  489. * remove a WebChannelInstance
  490. *
  491. * @function remove
  492. * @memberof Twilio.FlexApi.V1.WebChannelInstance#
  493. *
  494. * @param {function} [callback] - Callback to handle processed record
  495. *
  496. * @returns {Promise} Resolves to processed WebChannelInstance
  497. */
  498. /* jshint ignore:end */
  499. WebChannelInstance.prototype.remove = function remove(callback) {
  500. return this._proxy.remove(callback);
  501. };
  502. /* jshint ignore:start */
  503. /**
  504. * Provide a user-friendly representation
  505. *
  506. * @function toJSON
  507. * @memberof Twilio.FlexApi.V1.WebChannelInstance#
  508. *
  509. * @returns Object
  510. */
  511. /* jshint ignore:end */
  512. WebChannelInstance.prototype.toJSON = function toJSON() {
  513. let clone = {};
  514. _.forOwn(this, function(value, key) {
  515. if (!_.startsWith(key, '_') && ! _.isFunction(value)) {
  516. clone[key] = value;
  517. }
  518. });
  519. return clone;
  520. };
  521. WebChannelInstance.prototype[util.inspect.custom] = function inspect(depth,
  522. options) {
  523. return util.inspect(this.toJSON(), options);
  524. };
  525. /* jshint ignore:start */
  526. /**
  527. * Initialize the WebChannelContext
  528. *
  529. * @constructor Twilio.FlexApi.V1.WebChannelContext
  530. *
  531. * @param {V1} version - Version of the resource
  532. * @param {sid} sid - The SID of the WebChannel resource to fetch
  533. */
  534. /* jshint ignore:end */
  535. WebChannelContext = function WebChannelContext(version, sid) {
  536. this._version = version;
  537. // Path Solution
  538. this._solution = {sid: sid, };
  539. this._uri = `/WebChannels/${sid}`;
  540. };
  541. /* jshint ignore:start */
  542. /**
  543. * fetch a WebChannelInstance
  544. *
  545. * @function fetch
  546. * @memberof Twilio.FlexApi.V1.WebChannelContext#
  547. *
  548. * @param {function} [callback] - Callback to handle processed record
  549. *
  550. * @returns {Promise} Resolves to processed WebChannelInstance
  551. */
  552. /* jshint ignore:end */
  553. WebChannelContext.prototype.fetch = function fetch(callback) {
  554. var deferred = Q.defer();
  555. var promise = this._version.fetch({uri: this._uri, method: 'GET'});
  556. promise = promise.then(function(payload) {
  557. deferred.resolve(new WebChannelInstance(this._version, payload, this._solution.sid));
  558. }.bind(this));
  559. promise.catch(function(error) {
  560. deferred.reject(error);
  561. });
  562. if (_.isFunction(callback)) {
  563. deferred.promise.nodeify(callback);
  564. }
  565. return deferred.promise;
  566. };
  567. /* jshint ignore:start */
  568. /**
  569. * update a WebChannelInstance
  570. *
  571. * @function update
  572. * @memberof Twilio.FlexApi.V1.WebChannelContext#
  573. *
  574. * @param {object} [opts] - Options for request
  575. * @param {web_channel.chat_status} [opts.chatStatus] - The chat status
  576. * @param {string} [opts.postEngagementData] - The post-engagement data
  577. * @param {function} [callback] - Callback to handle processed record
  578. *
  579. * @returns {Promise} Resolves to processed WebChannelInstance
  580. */
  581. /* jshint ignore:end */
  582. WebChannelContext.prototype.update = function update(opts, callback) {
  583. if (_.isFunction(opts)) {
  584. callback = opts;
  585. opts = {};
  586. }
  587. opts = opts || {};
  588. var deferred = Q.defer();
  589. var data = values.of({
  590. 'ChatStatus': _.get(opts, 'chatStatus'),
  591. 'PostEngagementData': _.get(opts, 'postEngagementData')
  592. });
  593. var promise = this._version.update({uri: this._uri, method: 'POST', data: data});
  594. promise = promise.then(function(payload) {
  595. deferred.resolve(new WebChannelInstance(this._version, payload, this._solution.sid));
  596. }.bind(this));
  597. promise.catch(function(error) {
  598. deferred.reject(error);
  599. });
  600. if (_.isFunction(callback)) {
  601. deferred.promise.nodeify(callback);
  602. }
  603. return deferred.promise;
  604. };
  605. /* jshint ignore:start */
  606. /**
  607. * remove a WebChannelInstance
  608. *
  609. * @function remove
  610. * @memberof Twilio.FlexApi.V1.WebChannelContext#
  611. *
  612. * @param {function} [callback] - Callback to handle processed record
  613. *
  614. * @returns {Promise} Resolves to processed WebChannelInstance
  615. */
  616. /* jshint ignore:end */
  617. WebChannelContext.prototype.remove = function remove(callback) {
  618. var deferred = Q.defer();
  619. var promise = this._version.remove({uri: this._uri, method: 'DELETE'});
  620. promise = promise.then(function(payload) {
  621. deferred.resolve(payload);
  622. }.bind(this));
  623. promise.catch(function(error) {
  624. deferred.reject(error);
  625. });
  626. if (_.isFunction(callback)) {
  627. deferred.promise.nodeify(callback);
  628. }
  629. return deferred.promise;
  630. };
  631. /* jshint ignore:start */
  632. /**
  633. * Provide a user-friendly representation
  634. *
  635. * @function toJSON
  636. * @memberof Twilio.FlexApi.V1.WebChannelContext#
  637. *
  638. * @returns Object
  639. */
  640. /* jshint ignore:end */
  641. WebChannelContext.prototype.toJSON = function toJSON() {
  642. return this._solution;
  643. };
  644. WebChannelContext.prototype[util.inspect.custom] = function inspect(depth,
  645. options) {
  646. return util.inspect(this.toJSON(), options);
  647. };
  648. module.exports = {
  649. WebChannelList: WebChannelList,
  650. WebChannelPage: WebChannelPage,
  651. WebChannelInstance: WebChannelInstance,
  652. WebChannelContext: WebChannelContext
  653. };