Source: rest/video/v1/room/roomParticipant/roomParticipantPublishedTrack.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 PublishedTrackList;
  18. var PublishedTrackPage;
  19. var PublishedTrackInstance;
  20. var PublishedTrackContext;
  21. /* jshint ignore:start */
  22. /**
  23. * Initialize the PublishedTrackList
  24. *
  25. * @constructor Twilio.Video.V1.RoomContext.ParticipantContext.PublishedTrackList
  26. *
  27. * @param {Twilio.Video.V1} version - Version of the resource
  28. * @param {string} roomSid -
  29. * The SID of the Room resource where the track is published
  30. * @param {string} participantSid -
  31. * The SID of the Participant resource with the published track
  32. */
  33. /* jshint ignore:end */
  34. PublishedTrackList = function PublishedTrackList(version, roomSid,
  35. participantSid) {
  36. /* jshint ignore:start */
  37. /**
  38. * @function publishedTracks
  39. * @memberof Twilio.Video.V1.RoomContext.ParticipantContext#
  40. *
  41. * @param {string} sid - sid of instance
  42. *
  43. * @returns {Twilio.Video.V1.RoomContext.ParticipantContext.PublishedTrackContext}
  44. */
  45. /* jshint ignore:end */
  46. function PublishedTrackListInstance(sid) {
  47. return PublishedTrackListInstance.get(sid);
  48. }
  49. PublishedTrackListInstance._version = version;
  50. // Path Solution
  51. PublishedTrackListInstance._solution = {roomSid: roomSid, participantSid: participantSid};
  52. PublishedTrackListInstance._uri = `/Rooms/${roomSid}/Participants/${participantSid}/PublishedTracks`;
  53. /* jshint ignore:start */
  54. /**
  55. * Streams PublishedTrackInstance records from the API.
  56. *
  57. * This operation lazily loads records as efficiently as possible until the limit
  58. * is reached.
  59. *
  60. * The results are passed into the callback function, so this operation is memory
  61. * efficient.
  62. *
  63. * If a function is passed as the first argument, it will be used as the callback
  64. * function.
  65. *
  66. * @function each
  67. * @memberof Twilio.Video.V1.RoomContext.ParticipantContext.PublishedTrackList#
  68. *
  69. * @param {object} [opts] - Options for request
  70. * @param {number} [opts.limit] -
  71. * Upper limit for the number of records to return.
  72. * each() guarantees never to return more than limit.
  73. * Default is no limit
  74. * @param {number} [opts.pageSize] -
  75. * Number of records to fetch per request,
  76. * when not set will use the default value of 50 records.
  77. * If no pageSize is defined but a limit is defined,
  78. * each() will attempt to read the limit with the most efficient
  79. * page size, i.e. min(limit, 1000)
  80. * @param {Function} [opts.callback] -
  81. * Function to process each record. If this and a positional
  82. * callback are passed, this one will be used
  83. * @param {Function} [opts.done] -
  84. * Function to be called upon completion of streaming
  85. * @param {Function} [callback] - Function to process each record
  86. */
  87. /* jshint ignore:end */
  88. PublishedTrackListInstance.each = function each(opts, callback) {
  89. if (_.isFunction(opts)) {
  90. callback = opts;
  91. opts = {};
  92. }
  93. opts = opts || {};
  94. if (opts.callback) {
  95. callback = opts.callback;
  96. }
  97. if (_.isUndefined(callback)) {
  98. throw new Error('Callback function must be provided');
  99. }
  100. var done = false;
  101. var currentPage = 1;
  102. var currentResource = 0;
  103. var limits = this._version.readLimits({
  104. limit: opts.limit,
  105. pageSize: opts.pageSize
  106. });
  107. function onComplete(error) {
  108. done = true;
  109. if (_.isFunction(opts.done)) {
  110. opts.done(error);
  111. }
  112. }
  113. function fetchNextPage(fn) {
  114. var promise = fn();
  115. if (_.isUndefined(promise)) {
  116. onComplete();
  117. return;
  118. }
  119. promise.then(function(page) {
  120. _.each(page.instances, function(instance) {
  121. if (done || (!_.isUndefined(opts.limit) && currentResource >= opts.limit)) {
  122. done = true;
  123. return false;
  124. }
  125. currentResource++;
  126. callback(instance, onComplete);
  127. });
  128. if (!done) {
  129. currentPage++;
  130. fetchNextPage(_.bind(page.nextPage, page));
  131. }
  132. });
  133. promise.catch(onComplete);
  134. }
  135. fetchNextPage(_.bind(this.page, this, _.merge(opts, limits)));
  136. };
  137. /* jshint ignore:start */
  138. /**
  139. * Lists PublishedTrackInstance records from the API as a list.
  140. *
  141. * If a function is passed as the first argument, it will be used as the callback
  142. * function.
  143. *
  144. * @function list
  145. * @memberof Twilio.Video.V1.RoomContext.ParticipantContext.PublishedTrackList#
  146. *
  147. * @param {object} [opts] - Options for request
  148. * @param {number} [opts.limit] -
  149. * Upper limit for the number of records to return.
  150. * list() guarantees never to return more than limit.
  151. * Default is no limit
  152. * @param {number} [opts.pageSize] -
  153. * Number of records to fetch per request,
  154. * when not set will use the default value of 50 records.
  155. * If no page_size is defined but a limit is defined,
  156. * list() will attempt to read the limit with the most
  157. * efficient page size, i.e. min(limit, 1000)
  158. * @param {function} [callback] - Callback to handle list of records
  159. *
  160. * @returns {Promise} Resolves to a list of records
  161. */
  162. /* jshint ignore:end */
  163. PublishedTrackListInstance.list = function list(opts, callback) {
  164. if (_.isFunction(opts)) {
  165. callback = opts;
  166. opts = {};
  167. }
  168. opts = opts || {};
  169. var deferred = Q.defer();
  170. var allResources = [];
  171. opts.callback = function(resource, done) {
  172. allResources.push(resource);
  173. if (!_.isUndefined(opts.limit) && allResources.length === opts.limit) {
  174. done();
  175. }
  176. };
  177. opts.done = function(error) {
  178. if (_.isUndefined(error)) {
  179. deferred.resolve(allResources);
  180. } else {
  181. deferred.reject(error);
  182. }
  183. };
  184. if (_.isFunction(callback)) {
  185. deferred.promise.nodeify(callback);
  186. }
  187. this.each(opts);
  188. return deferred.promise;
  189. };
  190. /* jshint ignore:start */
  191. /**
  192. * Retrieve a single page of PublishedTrackInstance records from the API.
  193. *
  194. * The request is executed immediately.
  195. *
  196. * If a function is passed as the first argument, it will be used as the callback
  197. * function.
  198. *
  199. * @function page
  200. * @memberof Twilio.Video.V1.RoomContext.ParticipantContext.PublishedTrackList#
  201. *
  202. * @param {object} [opts] - Options for request
  203. * @param {string} [opts.pageToken] - PageToken provided by the API
  204. * @param {number} [opts.pageNumber] -
  205. * Page Number, this value is simply for client state
  206. * @param {number} [opts.pageSize] - Number of records to return, defaults to 50
  207. * @param {function} [callback] - Callback to handle list of records
  208. *
  209. * @returns {Promise} Resolves to a list of records
  210. */
  211. /* jshint ignore:end */
  212. PublishedTrackListInstance.page = function page(opts, callback) {
  213. if (_.isFunction(opts)) {
  214. callback = opts;
  215. opts = {};
  216. }
  217. opts = opts || {};
  218. var deferred = Q.defer();
  219. var data = values.of({
  220. 'PageToken': opts.pageToken,
  221. 'Page': opts.pageNumber,
  222. 'PageSize': opts.pageSize
  223. });
  224. var promise = this._version.page({uri: this._uri, method: 'GET', params: data});
  225. promise = promise.then(function(payload) {
  226. deferred.resolve(new PublishedTrackPage(this._version, payload, this._solution));
  227. }.bind(this));
  228. promise.catch(function(error) {
  229. deferred.reject(error);
  230. });
  231. if (_.isFunction(callback)) {
  232. deferred.promise.nodeify(callback);
  233. }
  234. return deferred.promise;
  235. };
  236. /* jshint ignore:start */
  237. /**
  238. * Retrieve a single target page of PublishedTrackInstance records from the API.
  239. *
  240. * The request is executed immediately.
  241. *
  242. * If a function is passed as the first argument, it will be used as the callback
  243. * function.
  244. *
  245. * @function getPage
  246. * @memberof Twilio.Video.V1.RoomContext.ParticipantContext.PublishedTrackList#
  247. *
  248. * @param {string} [targetUrl] - API-generated URL for the requested results page
  249. * @param {function} [callback] - Callback to handle list of records
  250. *
  251. * @returns {Promise} Resolves to a list of records
  252. */
  253. /* jshint ignore:end */
  254. PublishedTrackListInstance.getPage = function getPage(targetUrl, callback) {
  255. var deferred = Q.defer();
  256. var promise = this._version._domain.twilio.request({method: 'GET', uri: targetUrl});
  257. promise = promise.then(function(payload) {
  258. deferred.resolve(new PublishedTrackPage(this._version, payload, this._solution));
  259. }.bind(this));
  260. promise.catch(function(error) {
  261. deferred.reject(error);
  262. });
  263. if (_.isFunction(callback)) {
  264. deferred.promise.nodeify(callback);
  265. }
  266. return deferred.promise;
  267. };
  268. /* jshint ignore:start */
  269. /**
  270. * Constructs a published_track
  271. *
  272. * @function get
  273. * @memberof Twilio.Video.V1.RoomContext.ParticipantContext.PublishedTrackList#
  274. *
  275. * @param {string} sid - The SID that identifies the resource to fetch
  276. *
  277. * @returns {Twilio.Video.V1.RoomContext.ParticipantContext.PublishedTrackContext}
  278. */
  279. /* jshint ignore:end */
  280. PublishedTrackListInstance.get = function get(sid) {
  281. return new PublishedTrackContext(
  282. this._version,
  283. this._solution.roomSid,
  284. this._solution.participantSid,
  285. sid
  286. );
  287. };
  288. /* jshint ignore:start */
  289. /**
  290. * Provide a user-friendly representation
  291. *
  292. * @function toJSON
  293. * @memberof Twilio.Video.V1.RoomContext.ParticipantContext.PublishedTrackList#
  294. *
  295. * @returns Object
  296. */
  297. /* jshint ignore:end */
  298. PublishedTrackListInstance.toJSON = function toJSON() {
  299. return this._solution;
  300. };
  301. PublishedTrackListInstance[util.inspect.custom] = function inspect(depth,
  302. options) {
  303. return util.inspect(this.toJSON(), options);
  304. };
  305. return PublishedTrackListInstance;
  306. };
  307. /* jshint ignore:start */
  308. /**
  309. * Initialize the PublishedTrackPage
  310. *
  311. * @constructor Twilio.Video.V1.RoomContext.ParticipantContext.PublishedTrackPage
  312. *
  313. * @param {V1} version - Version of the resource
  314. * @param {Response<string>} response - Response from the API
  315. * @param {PublishedTrackSolution} solution - Path solution
  316. *
  317. * @returns PublishedTrackPage
  318. */
  319. /* jshint ignore:end */
  320. PublishedTrackPage = function PublishedTrackPage(version, response, solution) {
  321. // Path Solution
  322. this._solution = solution;
  323. Page.prototype.constructor.call(this, version, response, this._solution);
  324. };
  325. _.extend(PublishedTrackPage.prototype, Page.prototype);
  326. PublishedTrackPage.prototype.constructor = PublishedTrackPage;
  327. /* jshint ignore:start */
  328. /**
  329. * Build an instance of PublishedTrackInstance
  330. *
  331. * @function getInstance
  332. * @memberof Twilio.Video.V1.RoomContext.ParticipantContext.PublishedTrackPage#
  333. *
  334. * @param {PublishedTrackPayload} payload - Payload response from the API
  335. *
  336. * @returns PublishedTrackInstance
  337. */
  338. /* jshint ignore:end */
  339. PublishedTrackPage.prototype.getInstance = function getInstance(payload) {
  340. return new PublishedTrackInstance(
  341. this._version,
  342. payload,
  343. this._solution.roomSid,
  344. this._solution.participantSid
  345. );
  346. };
  347. /* jshint ignore:start */
  348. /**
  349. * Provide a user-friendly representation
  350. *
  351. * @function toJSON
  352. * @memberof Twilio.Video.V1.RoomContext.ParticipantContext.PublishedTrackPage#
  353. *
  354. * @returns Object
  355. */
  356. /* jshint ignore:end */
  357. PublishedTrackPage.prototype.toJSON = function toJSON() {
  358. let clone = {};
  359. _.forOwn(this, function(value, key) {
  360. if (!_.startsWith(key, '_') && ! _.isFunction(value)) {
  361. clone[key] = value;
  362. }
  363. });
  364. return clone;
  365. };
  366. PublishedTrackPage.prototype[util.inspect.custom] = function inspect(depth,
  367. options) {
  368. return util.inspect(this.toJSON(), options);
  369. };
  370. /* jshint ignore:start */
  371. /**
  372. * Initialize the PublishedTrackContext
  373. *
  374. * @constructor Twilio.Video.V1.RoomContext.ParticipantContext.PublishedTrackInstance
  375. *
  376. * @property {string} sid - The unique string that identifies the resource
  377. * @property {string} participantSid -
  378. * The SID of the Participant resource with the published track
  379. * @property {string} roomSid -
  380. * The SID of the Room resource where the track is published
  381. * @property {string} name - The track name
  382. * @property {Date} dateCreated -
  383. * The ISO 8601 date and time in GMT when the resource was created
  384. * @property {Date} dateUpdated -
  385. * The ISO 8601 date and time in GMT when the resource was last updated
  386. * @property {boolean} enabled - Whether the track is enabled
  387. * @property {published_track.kind} kind - The track type
  388. * @property {string} url - The absolute URL of the resource
  389. *
  390. * @param {V1} version - Version of the resource
  391. * @param {PublishedTrackPayload} payload - The instance payload
  392. * @param {sid} roomSid - The SID of the Room resource where the track is published
  393. * @param {sid} participantSid -
  394. * The SID of the Participant resource with the published track
  395. * @param {sid_like} sid - The SID that identifies the resource to fetch
  396. */
  397. /* jshint ignore:end */
  398. PublishedTrackInstance = function PublishedTrackInstance(version, payload,
  399. roomSid,
  400. participantSid, sid) {
  401. this._version = version;
  402. // Marshaled Properties
  403. this.sid = payload.sid; // jshint ignore:line
  404. this.participantSid = payload.participant_sid; // jshint ignore:line
  405. this.roomSid = payload.room_sid; // jshint ignore:line
  406. this.name = payload.name; // jshint ignore:line
  407. this.dateCreated = deserialize.iso8601DateTime(payload.date_created); // jshint ignore:line
  408. this.dateUpdated = deserialize.iso8601DateTime(payload.date_updated); // jshint ignore:line
  409. this.enabled = payload.enabled; // jshint ignore:line
  410. this.kind = payload.kind; // jshint ignore:line
  411. this.url = payload.url; // jshint ignore:line
  412. // Context
  413. this._context = undefined;
  414. this._solution = {roomSid: roomSid, participantSid: participantSid, sid: sid || this.sid, };
  415. };
  416. Object.defineProperty(PublishedTrackInstance.prototype,
  417. '_proxy', {
  418. get: function() {
  419. if (!this._context) {
  420. this._context = new PublishedTrackContext(
  421. this._version,
  422. this._solution.roomSid,
  423. this._solution.participantSid,
  424. this._solution.sid
  425. );
  426. }
  427. return this._context;
  428. }
  429. });
  430. /* jshint ignore:start */
  431. /**
  432. * fetch a PublishedTrackInstance
  433. *
  434. * @function fetch
  435. * @memberof Twilio.Video.V1.RoomContext.ParticipantContext.PublishedTrackInstance#
  436. *
  437. * @param {function} [callback] - Callback to handle processed record
  438. *
  439. * @returns {Promise} Resolves to processed PublishedTrackInstance
  440. */
  441. /* jshint ignore:end */
  442. PublishedTrackInstance.prototype.fetch = function fetch(callback) {
  443. return this._proxy.fetch(callback);
  444. };
  445. /* jshint ignore:start */
  446. /**
  447. * Provide a user-friendly representation
  448. *
  449. * @function toJSON
  450. * @memberof Twilio.Video.V1.RoomContext.ParticipantContext.PublishedTrackInstance#
  451. *
  452. * @returns Object
  453. */
  454. /* jshint ignore:end */
  455. PublishedTrackInstance.prototype.toJSON = function toJSON() {
  456. let clone = {};
  457. _.forOwn(this, function(value, key) {
  458. if (!_.startsWith(key, '_') && ! _.isFunction(value)) {
  459. clone[key] = value;
  460. }
  461. });
  462. return clone;
  463. };
  464. PublishedTrackInstance.prototype[util.inspect.custom] = function inspect(depth,
  465. options) {
  466. return util.inspect(this.toJSON(), options);
  467. };
  468. /* jshint ignore:start */
  469. /**
  470. * Initialize the PublishedTrackContext
  471. *
  472. * @constructor Twilio.Video.V1.RoomContext.ParticipantContext.PublishedTrackContext
  473. *
  474. * @param {V1} version - Version of the resource
  475. * @param {sid_like} roomSid -
  476. * The SID of the Room resource where the Track resource to fetch is published
  477. * @param {sid_like} participantSid -
  478. * The SID of the Participant resource with the published track to fetch
  479. * @param {sid_like} sid - The SID that identifies the resource to fetch
  480. */
  481. /* jshint ignore:end */
  482. PublishedTrackContext = function PublishedTrackContext(version, roomSid,
  483. participantSid, sid) {
  484. this._version = version;
  485. // Path Solution
  486. this._solution = {roomSid: roomSid, participantSid: participantSid, sid: sid, };
  487. this._uri = `/Rooms/${roomSid}/Participants/${participantSid}/PublishedTracks/${sid}`;
  488. };
  489. /* jshint ignore:start */
  490. /**
  491. * fetch a PublishedTrackInstance
  492. *
  493. * @function fetch
  494. * @memberof Twilio.Video.V1.RoomContext.ParticipantContext.PublishedTrackContext#
  495. *
  496. * @param {function} [callback] - Callback to handle processed record
  497. *
  498. * @returns {Promise} Resolves to processed PublishedTrackInstance
  499. */
  500. /* jshint ignore:end */
  501. PublishedTrackContext.prototype.fetch = function fetch(callback) {
  502. var deferred = Q.defer();
  503. var promise = this._version.fetch({uri: this._uri, method: 'GET'});
  504. promise = promise.then(function(payload) {
  505. deferred.resolve(new PublishedTrackInstance(
  506. this._version,
  507. payload,
  508. this._solution.roomSid,
  509. this._solution.participantSid,
  510. this._solution.sid
  511. ));
  512. }.bind(this));
  513. promise.catch(function(error) {
  514. deferred.reject(error);
  515. });
  516. if (_.isFunction(callback)) {
  517. deferred.promise.nodeify(callback);
  518. }
  519. return deferred.promise;
  520. };
  521. /* jshint ignore:start */
  522. /**
  523. * Provide a user-friendly representation
  524. *
  525. * @function toJSON
  526. * @memberof Twilio.Video.V1.RoomContext.ParticipantContext.PublishedTrackContext#
  527. *
  528. * @returns Object
  529. */
  530. /* jshint ignore:end */
  531. PublishedTrackContext.prototype.toJSON = function toJSON() {
  532. return this._solution;
  533. };
  534. PublishedTrackContext.prototype[util.inspect.custom] = function inspect(depth,
  535. options) {
  536. return util.inspect(this.toJSON(), options);
  537. };
  538. module.exports = {
  539. PublishedTrackList: PublishedTrackList,
  540. PublishedTrackPage: PublishedTrackPage,
  541. PublishedTrackInstance: PublishedTrackInstance,
  542. PublishedTrackContext: PublishedTrackContext
  543. };