ApiClient.js 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. /**
  2. * Represents a javascript version of ApiClient.
  3. * This should be kept up to date with all possible api methods and parameters
  4. */
  5. var ApiClient = {
  6. serverProtocol: "http",
  7. /**
  8. * Gets or sets the host name of the server
  9. */
  10. serverHostName: "localhost",
  11. serverPortNumber: 8096,
  12. /**
  13. * Detects the hostname and port of MB server based on the current url
  14. */
  15. inferServerFromUrl: function () {
  16. var loc = window.location;
  17. ApiClient.serverProtocol = loc.protocol;
  18. ApiClient.serverHostName = loc.hostname;
  19. ApiClient.serverPortNumber = loc.port;
  20. },
  21. /**
  22. * Creates an api url based on a handler name and query string parameters
  23. * @param {String} name
  24. * @param {Object} params
  25. */
  26. getUrl: function (name, params) {
  27. if (!name) {
  28. throw new Error("Url name cannot be empty");
  29. }
  30. params = params || {};
  31. var url = ApiClient.serverProtocol + "//" + ApiClient.serverHostName + ":" + ApiClient.serverPortNumber + "/mediabrowser/" + name;
  32. if (params) {
  33. url += "?" + $.param(params);
  34. }
  35. return url;
  36. },
  37. /**
  38. * Returns the name of the current browser
  39. */
  40. getDeviceName: function () {
  41. /*if ($.browser.chrome) {
  42. return "Chrome";
  43. }
  44. if ($.browser.safari) {
  45. return "Safari";
  46. }
  47. if ($.browser.webkit) {
  48. return "WebKit";
  49. }
  50. if ($.browser.msie) {
  51. return "Internet Explorer";
  52. }
  53. if ($.browser.firefox) {
  54. return "Firefox";
  55. }
  56. if ($.browser.mozilla) {
  57. return "Firefox";
  58. }
  59. if ($.browser.opera) {
  60. return "Opera";
  61. }*/
  62. return "Web Browser";
  63. },
  64. /**
  65. * Creates a custom api url based on a handler name and query string parameters
  66. * @param {String} name
  67. * @param {Object} params
  68. */
  69. getCustomUrl: function (name, params) {
  70. if (!name) {
  71. throw new Error("Url name cannot be empty");
  72. }
  73. params = params || {};
  74. params.client = "Dashboard";
  75. params.device = ApiClient.getDeviceName();
  76. params.format = "json";
  77. var url = ApiClient.serverProtocol + "//" + ApiClient.serverHostName + ":" + ApiClient.serverPortNumber + "/mediabrowser/" + name;
  78. if (params) {
  79. url += "?" + $.param(params);
  80. }
  81. return url;
  82. },
  83. /**
  84. * Gets an item from the server
  85. * Omit itemId to get the root folder.
  86. */
  87. getItem: function (userId, itemId) {
  88. if (!userId) {
  89. throw new Error("null userId");
  90. }
  91. var url = ApiClient.getUrl("Users/" + userId + "/Items/" + itemId);
  92. return $.getJSON(url);
  93. },
  94. /**
  95. * Gets the root folder from the server
  96. */
  97. getRootFolder: function (userId) {
  98. return ApiClient.getItem(userId);
  99. },
  100. /**
  101. * Gets the current server status
  102. */
  103. getSystemInfo: function () {
  104. var url = ApiClient.getUrl("System/Info");
  105. return $.getJSON(url);
  106. },
  107. /**
  108. * Gets all cultures known to the server
  109. */
  110. getCultures: function () {
  111. var url = ApiClient.getUrl("Localization/cultures");
  112. return $.getJSON(url);
  113. },
  114. /**
  115. * Gets all countries known to the server
  116. */
  117. getCountries: function () {
  118. var url = ApiClient.getUrl("Localization/countries");
  119. return $.getJSON(url);
  120. },
  121. /**
  122. * Gets plugin security info
  123. */
  124. getPluginSecurityInfo: function () {
  125. var url = ApiClient.getUrl("Plugins/SecurityInfo");
  126. return $.getJSON(url);
  127. },
  128. /**
  129. * Gets the directory contents of a path on the server
  130. */
  131. getDirectoryContents: function (path, options) {
  132. if (!path) {
  133. throw new Error("null path");
  134. }
  135. options = options || {};
  136. options.path = path;
  137. var url = ApiClient.getUrl("Environment/DirectoryContents", options);
  138. return $.getJSON(url);
  139. },
  140. /**
  141. * Gets a list of physical drives from the server
  142. */
  143. getDrives: function () {
  144. var url = ApiClient.getUrl("Environment/Drives");
  145. return $.getJSON(url);
  146. },
  147. /**
  148. * Gets a list of network computers from the server
  149. */
  150. getNetworkComputers: function () {
  151. var url = ApiClient.getUrl("Environment/NetworkComputers");
  152. return $.getJSON(url);
  153. },
  154. /**
  155. * Cancels a package installation
  156. */
  157. cancelPackageInstallation: function (installationId) {
  158. if (!installationId) {
  159. throw new Error("null installationId");
  160. }
  161. var url = ApiClient.getUrl("Packages/Installing/" + id);
  162. return $.ajax({
  163. type: "DELETE",
  164. url: url,
  165. dataType: "json"
  166. });
  167. },
  168. /**
  169. * Installs or updates a new plugin
  170. */
  171. installPlugin: function (name, updateClass, version) {
  172. if (!name) {
  173. throw new Error("null name");
  174. }
  175. if (!updateClass) {
  176. throw new Error("null updateClass");
  177. }
  178. var options = {
  179. updateClass: updateClass
  180. };
  181. if (version) {
  182. options.version = version;
  183. }
  184. var url = ApiClient.getUrl("Packages/Installed/" + name, options);
  185. return $.post(url);
  186. },
  187. /**
  188. * Instructs the server to perform a pending kernel reload or app restart.
  189. * If a restart is not currently required, nothing will happen.
  190. */
  191. performPendingRestart: function () {
  192. var url = ApiClient.getUrl("System/Restart");
  193. return $.post(url);
  194. },
  195. /**
  196. * Gets information about an installable package
  197. */
  198. getPackageInfo: function (name) {
  199. if (!name) {
  200. throw new Error("null name");
  201. }
  202. var url = ApiClient.getUrl("Packages/" + name);
  203. return $.getJSON(url);
  204. },
  205. /**
  206. * Gets the latest available application update (if any)
  207. */
  208. getAvailableApplicationUpdate: function () {
  209. var url = ApiClient.getUrl("Packages/Updates", { PackageType: "System" });
  210. return $.getJSON(url);
  211. },
  212. /**
  213. * Gets the latest available plugin updates (if any)
  214. */
  215. getAvailablePluginUpdates: function () {
  216. var url = ApiClient.getUrl("Packages/Updates", { PackageType: "UserInstalled" });
  217. return $.getJSON(url);
  218. },
  219. /**
  220. * Gets the virtual folder for a view. Specify a userId to get a user view, or omit for the default view.
  221. */
  222. getVirtualFolders: function (userId) {
  223. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  224. url = ApiClient.getUrl(url);
  225. return $.getJSON(url);
  226. },
  227. /**
  228. * Gets all the paths of the locations in the physical root.
  229. */
  230. getPhysicalPaths: function () {
  231. var url = ApiClient.getUrl("Library/PhysicalPaths");
  232. return $.getJSON(url);
  233. },
  234. /**
  235. * Gets the current server configuration
  236. */
  237. getServerConfiguration: function () {
  238. var url = ApiClient.getUrl("System/Configuration");
  239. return $.getJSON(url);
  240. },
  241. /**
  242. * Gets the server's scheduled tasks
  243. */
  244. getScheduledTasks: function () {
  245. var url = ApiClient.getUrl("ScheduledTasks");
  246. return $.getJSON(url);
  247. },
  248. /**
  249. * Starts a scheduled task
  250. */
  251. startScheduledTask: function (id) {
  252. if (!id) {
  253. throw new Error("null id");
  254. }
  255. var url = ApiClient.getUrl("ScheduledTasks/Running/" + id);
  256. return $.post(url);
  257. },
  258. /**
  259. * Gets a scheduled task
  260. */
  261. getScheduledTask: function (id) {
  262. if (!id) {
  263. throw new Error("null id");
  264. }
  265. var url = ApiClient.getUrl("ScheduledTasks/" + id);
  266. return $.getJSON(url);
  267. },
  268. /**
  269. * Stops a scheduled task
  270. */
  271. stopScheduledTask: function (id) {
  272. if (!id) {
  273. throw new Error("null id");
  274. }
  275. var url = ApiClient.getUrl("ScheduledTasks/Running/" + id);
  276. return $.ajax({
  277. type: "DELETE",
  278. url: url,
  279. dataType: "json"
  280. });
  281. },
  282. /**
  283. * Gets the configuration of a plugin
  284. * @param {String} Id
  285. */
  286. getPluginConfiguration: function (id) {
  287. if (!id) {
  288. throw new Error("null Id");
  289. }
  290. var url = ApiClient.getUrl("Plugins/" + id + "/Configuration");
  291. return $.getJSON(url);
  292. },
  293. /**
  294. * Gets a list of plugins that are available to be installed
  295. */
  296. getAvailablePlugins: function () {
  297. var url = ApiClient.getUrl("Packages", { PackageType: "UserInstalled" });
  298. return $.getJSON(url);
  299. },
  300. /**
  301. * Uninstalls a plugin
  302. * @param {String} Id
  303. */
  304. uninstallPlugin: function (id) {
  305. if (!id) {
  306. throw new Error("null Id");
  307. }
  308. var url = ApiClient.getUrl("Plugins/" + id);
  309. return $.ajax({
  310. type: "DELETE",
  311. url: url,
  312. dataType: "json"
  313. });
  314. },
  315. /**
  316. * Removes a virtual folder from either the default view or a user view
  317. * @param {String} name
  318. */
  319. removeVirtualFolder: function (name, userId) {
  320. if (!name) {
  321. throw new Error("null name");
  322. }
  323. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  324. url += "/" + name;
  325. url = ApiClient.getUrl(url);
  326. return $.ajax({
  327. type: "DELETE",
  328. url: url,
  329. dataType: "json"
  330. });
  331. },
  332. /**
  333. * Adds a virtual folder to either the default view or a user view
  334. * @param {String} name
  335. */
  336. addVirtualFolder: function (name, userId) {
  337. if (!name) {
  338. throw new Error("null name");
  339. }
  340. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  341. url += "/" + name;
  342. url = ApiClient.getUrl(url);
  343. return $.post(url);
  344. },
  345. /**
  346. * Renames a virtual folder, within either the default view or a user view
  347. * @param {String} name
  348. */
  349. renameVirtualFolder: function (name, newName, userId) {
  350. if (!name) {
  351. throw new Error("null name");
  352. }
  353. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  354. url += "/" + name + "/Name";
  355. url = ApiClient.getUrl(url, { newName: newName });
  356. return $.post(url);
  357. },
  358. /**
  359. * Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view
  360. * @param {String} name
  361. */
  362. addMediaPath: function (virtualFolderName, mediaPath, userId) {
  363. if (!virtualFolderName) {
  364. throw new Error("null virtualFolderName");
  365. }
  366. if (!mediaPath) {
  367. throw new Error("null mediaPath");
  368. }
  369. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  370. url += "/" + virtualFolderName + "/Paths";
  371. url = ApiClient.getUrl(url, { path: mediaPath });
  372. return $.post(url);
  373. },
  374. /**
  375. * Removes a media path from a virtual folder, within either the default view or a user view
  376. * @param {String} name
  377. */
  378. removeMediaPath: function (virtualFolderName, mediaPath, userId) {
  379. if (!virtualFolderName) {
  380. throw new Error("null virtualFolderName");
  381. }
  382. if (!mediaPath) {
  383. throw new Error("null mediaPath");
  384. }
  385. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  386. url += "/" + virtualFolderName + "/Paths";
  387. url = ApiClient.getUrl(url, { path: mediaPath });
  388. return $.ajax({
  389. type: "DELETE",
  390. url: url,
  391. dataType: "json"
  392. });
  393. },
  394. /**
  395. * Deletes a user
  396. * @param {String} id
  397. */
  398. deleteUser: function (id) {
  399. if (!id) {
  400. throw new Error("null id");
  401. }
  402. var url = ApiClient.getUrl("Users/" + id);
  403. return $.ajax({
  404. type: "DELETE",
  405. url: url,
  406. dataType: "json"
  407. });
  408. },
  409. /**
  410. * Deletes a user image
  411. * @param {String} userId
  412. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  413. */
  414. deleteUserImage: function (userId, imageType) {
  415. if (!userId) {
  416. throw new Error("null userId");
  417. }
  418. if (!imageType) {
  419. throw new Error("null imageType");
  420. }
  421. var url = ApiClient.getUrl("Users/" + userId + "/Images/" + imageType);
  422. return $.ajax({
  423. type: "DELETE",
  424. url: url,
  425. dataType: "json"
  426. });
  427. },
  428. /**
  429. * Uploads a user image
  430. * @param {String} userId
  431. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  432. * @param {Object} file The file from the input element
  433. */
  434. uploadUserImage: function (userId, imageType, file) {
  435. if (!userId) {
  436. throw new Error("null userId");
  437. }
  438. if (!imageType) {
  439. throw new Error("null imageType");
  440. }
  441. if (!file || !file.type.match('image.*')) {
  442. throw new Error("File must be an image.");
  443. }
  444. var deferred = $.Deferred();
  445. var reader = new FileReader();
  446. reader.onerror = function () {
  447. deferred.reject();
  448. };
  449. reader.onabort = function () {
  450. deferred.reject();
  451. };
  452. // Closure to capture the file information.
  453. reader.onload = function (e) {
  454. var data = window.btoa(e.target.result);
  455. var url = ApiClient.getUrl("Users/" + userId + "/Images/" + imageType);
  456. $.ajax({
  457. type: "POST",
  458. url: url,
  459. data: data,
  460. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  461. }).done(function (result) {
  462. deferred.resolveWith(null, [result]);
  463. }).fail(function () {
  464. deferred.reject();
  465. });
  466. };
  467. // Read in the image file as a data URL.
  468. reader.readAsBinaryString(file);
  469. return deferred.promise();
  470. },
  471. /**
  472. * Gets the list of installed plugins on the server
  473. */
  474. getInstalledPlugins: function () {
  475. var url = ApiClient.getUrl("Plugins");
  476. return $.getJSON(url);
  477. },
  478. /**
  479. * Gets a user by id
  480. * @param {String} id
  481. */
  482. getUser: function (id) {
  483. if (!id) {
  484. throw new Error("Must supply a userId");
  485. }
  486. var url = ApiClient.getUrl("Users/" + id);
  487. return $.getJSON(url);
  488. },
  489. /**
  490. * Gets a studio
  491. */
  492. getStudio: function (name) {
  493. if (!name) {
  494. throw new Error("null name");
  495. }
  496. var url = ApiClient.getUrl("Library/Studios/" + name);
  497. return $.getJSON(url);
  498. },
  499. /**
  500. * Gets a genre
  501. */
  502. getGenre: function (name) {
  503. if (!name) {
  504. throw new Error("null name");
  505. }
  506. var url = ApiClient.getUrl("Library/Genres/" + name);
  507. return $.getJSON(url);
  508. },
  509. /**
  510. * Gets a year
  511. */
  512. getYear: function (year) {
  513. if (!year) {
  514. throw new Error("null year");
  515. }
  516. var url = ApiClient.getUrl("Library/Years/" + year);
  517. return $.getJSON(url);
  518. },
  519. /**
  520. * Gets a Person
  521. */
  522. getPerson: function (name) {
  523. if (!name) {
  524. throw new Error("null name");
  525. }
  526. var url = ApiClient.getUrl("Library/Persons/" + name);
  527. return $.getJSON(url);
  528. },
  529. /**
  530. * Gets weather info
  531. * @param {String} location - us zip code / city, state, country / city, country
  532. * Omit location to get weather info using stored server configuration value
  533. */
  534. getWeatherInfo: function (location) {
  535. var url = ApiClient.getUrl("weather", {
  536. location: location
  537. });
  538. return $.getJSON(url);
  539. },
  540. /**
  541. * Gets all users from the server
  542. */
  543. getAllUsers: function () {
  544. var url = ApiClient.getUrl("users");
  545. return $.getJSON(url);
  546. },
  547. /**
  548. * Gets all available parental ratings from the server
  549. */
  550. getParentalRatings: function () {
  551. var url = ApiClient.getUrl("Localization/ParentalRatings");
  552. return $.getJSON(url);
  553. },
  554. /**
  555. * Gets a list of all available conrete BaseItem types from the server
  556. */
  557. getItemTypes: function (options) {
  558. var url = ApiClient.getUrl("Library/ItemTypes", options);
  559. return $.getJSON(url);
  560. },
  561. /**
  562. * Constructs a url for a user image
  563. * @param {String} userId
  564. * @param {Object} options
  565. * Options supports the following properties:
  566. * width - download the image at a fixed width
  567. * height - download the image at a fixed height
  568. * maxWidth - download the image at a maxWidth
  569. * maxHeight - download the image at a maxHeight
  570. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  571. * For best results do not specify both width and height together, as aspect ratio might be altered.
  572. */
  573. getUserImageUrl: function (userId, options) {
  574. if (!userId) {
  575. throw new Error("null userId");
  576. }
  577. options = options || {
  578. };
  579. var url = "Users/" + userId + "/Images/" + options.type;
  580. if (options.index != null) {
  581. url += "/" + options.index;
  582. }
  583. // Don't put these on the query string
  584. delete options.type;
  585. delete options.index;
  586. return ApiClient.getUrl(url, options);
  587. },
  588. /**
  589. * Constructs a url for a person image
  590. * @param {String} name
  591. * @param {Object} options
  592. * Options supports the following properties:
  593. * width - download the image at a fixed width
  594. * height - download the image at a fixed height
  595. * maxWidth - download the image at a maxWidth
  596. * maxHeight - download the image at a maxHeight
  597. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  598. * For best results do not specify both width and height together, as aspect ratio might be altered.
  599. */
  600. getPersonImageUrl: function (name, options) {
  601. if (!name) {
  602. throw new Error("null name");
  603. }
  604. options = options || {
  605. };
  606. var url = "Persons/" + name + "/Images/" + options.type;
  607. if (options.index != null) {
  608. url += "/" + options.index;
  609. }
  610. // Don't put these on the query string
  611. delete options.type;
  612. delete options.index;
  613. return ApiClient.getUrl(url, options);
  614. },
  615. /**
  616. * Constructs a url for a year image
  617. * @param {String} year
  618. * @param {Object} options
  619. * Options supports the following properties:
  620. * width - download the image at a fixed width
  621. * height - download the image at a fixed height
  622. * maxWidth - download the image at a maxWidth
  623. * maxHeight - download the image at a maxHeight
  624. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  625. * For best results do not specify both width and height together, as aspect ratio might be altered.
  626. */
  627. getYearImageUrl: function (year, options) {
  628. if (!year) {
  629. throw new Error("null year");
  630. }
  631. options = options || {
  632. };
  633. var url = "Years/" + year + "/Images/" + options.type;
  634. if (options.index != null) {
  635. url += "/" + options.index;
  636. }
  637. // Don't put these on the query string
  638. delete options.type;
  639. delete options.index;
  640. return ApiClient.getUrl(url, options);
  641. },
  642. /**
  643. * Constructs a url for a genre image
  644. * @param {String} name
  645. * @param {Object} options
  646. * Options supports the following properties:
  647. * width - download the image at a fixed width
  648. * height - download the image at a fixed height
  649. * maxWidth - download the image at a maxWidth
  650. * maxHeight - download the image at a maxHeight
  651. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  652. * For best results do not specify both width and height together, as aspect ratio might be altered.
  653. */
  654. getGenreImageUrl: function (name, options) {
  655. if (!name) {
  656. throw new Error("null name");
  657. }
  658. options = options || {
  659. };
  660. var url = "Genres/" + name + "/Images/" + options.type;
  661. if (options.index != null) {
  662. url += "/" + options.index;
  663. }
  664. // Don't put these on the query string
  665. delete options.type;
  666. delete options.index;
  667. return ApiClient.getUrl(url, options);
  668. },
  669. /**
  670. * Constructs a url for a genre image
  671. * @param {String} name
  672. * @param {Object} options
  673. * Options supports the following properties:
  674. * width - download the image at a fixed width
  675. * height - download the image at a fixed height
  676. * maxWidth - download the image at a maxWidth
  677. * maxHeight - download the image at a maxHeight
  678. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  679. * For best results do not specify both width and height together, as aspect ratio might be altered.
  680. */
  681. getStudioImageUrl: function (name, options) {
  682. if (!name) {
  683. throw new Error("null name");
  684. }
  685. options = options || {
  686. };
  687. var url = "Studios/" + name + "/Images/" + options.type;
  688. if (options.index != null) {
  689. url += "/" + options.index;
  690. }
  691. // Don't put these on the query string
  692. delete options.type;
  693. delete options.index;
  694. return ApiClient.getUrl(url, options);
  695. },
  696. /**
  697. * Constructs a url for an item image
  698. * @param {String} itemId
  699. * @param {Object} options
  700. * Options supports the following properties:
  701. * type - Primary, logo, backdrop, etc. See the server-side enum ImageType
  702. * index - When downloading a backdrop, use this to specify which one (omitting is equivalent to zero)
  703. * width - download the image at a fixed width
  704. * height - download the image at a fixed height
  705. * maxWidth - download the image at a maxWidth
  706. * maxHeight - download the image at a maxHeight
  707. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  708. * For best results do not specify both width and height together, as aspect ratio might be altered.
  709. */
  710. getImageUrl: function (itemId, options) {
  711. if (!itemId) {
  712. throw new Error("itemId cannot be empty");
  713. }
  714. options = options || {
  715. };
  716. var url = "Items/" + itemId + "/Images/" + options.type;
  717. if (options.index != null) {
  718. url += "/" + options.index;
  719. }
  720. // Don't put these on the query string
  721. delete options.type;
  722. delete options.index;
  723. return ApiClient.getUrl(url, options);
  724. },
  725. /**
  726. * Constructs a url for an item logo image
  727. * If the item doesn't have a logo, it will inherit a logo from a parent
  728. * @param {Object} item A BaseItem
  729. * @param {Object} options
  730. * Options supports the following properties:
  731. * width - download the image at a fixed width
  732. * height - download the image at a fixed height
  733. * maxWidth - download the image at a maxWidth
  734. * maxHeight - download the image at a maxHeight
  735. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  736. * For best results do not specify both width and height together, as aspect ratio might be altered.
  737. */
  738. getLogoImageUrl: function (item, options) {
  739. if (!item) {
  740. throw new Error("null item");
  741. }
  742. options = options || {
  743. };
  744. options.imageType = "logo";
  745. var logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId;
  746. return logoItemId ? ApiClient.getImageUrl(logoItemId, options) : null;
  747. },
  748. /**
  749. * Constructs an array of backdrop image url's for an item
  750. * If the item doesn't have any backdrops, it will inherit them from a parent
  751. * @param {Object} item A BaseItem
  752. * @param {Object} options
  753. * Options supports the following properties:
  754. * width - download the image at a fixed width
  755. * height - download the image at a fixed height
  756. * maxWidth - download the image at a maxWidth
  757. * maxHeight - download the image at a maxHeight
  758. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  759. * For best results do not specify both width and height together, as aspect ratio might be altered.
  760. */
  761. getBackdropImageUrl: function (item, options) {
  762. if (!item) {
  763. throw new Error("null item");
  764. }
  765. options = options || {
  766. };
  767. options.imageType = "backdrop";
  768. var backdropItemId;
  769. var backdropCount;
  770. if (!item.BackdropCount) {
  771. backdropItemId = item.ParentBackdropItemId;
  772. backdropCount = item.ParentBackdropCount || 0;
  773. } else {
  774. backdropItemId = item.Id;
  775. backdropCount = item.BackdropCount;
  776. }
  777. if (!backdropItemId) {
  778. return [];
  779. }
  780. var files = [];
  781. for (var i = 0; i < backdropCount; i++) {
  782. options.imageIndex = i;
  783. files[i] = ApiClient.getImageUrl(backdropItemId, options);
  784. }
  785. return files;
  786. },
  787. /**
  788. * Authenticates a user
  789. * @param {String} userId
  790. * @param {String} password
  791. */
  792. authenticateUser: function (userId, password) {
  793. if (!userId) {
  794. throw new Error("null userId");
  795. }
  796. var url = ApiClient.getUrl("Users/" + userId + "/authenticate");
  797. var postData = {
  798. };
  799. if (password) {
  800. postData.password = password;
  801. }
  802. return $.post(url, postData);
  803. },
  804. /**
  805. * Updates a user's password
  806. * @param {String} userId
  807. * @param {String} currentPassword
  808. * @param {String} newPassword
  809. */
  810. updateUserPassword: function (userId, currentPassword, newPassword) {
  811. if (!userId) {
  812. throw new Error("null userId");
  813. }
  814. var url = ApiClient.getUrl("Users/" + userId + "/Password");
  815. var postData = {
  816. };
  817. if (currentPassword) {
  818. postData.currentPassword = currentPassword;
  819. }
  820. if (newPassword) {
  821. postData.newPassword = newPassword;
  822. }
  823. return $.post(url, postData);
  824. },
  825. /**
  826. * Resets a user's password
  827. * @param {String} userId
  828. */
  829. resetUserPassword: function (userId) {
  830. if (!userId) {
  831. throw new Error("null userId");
  832. }
  833. var url = ApiClient.getUrl("Users/" + userId + "/Password");
  834. var postData = {
  835. };
  836. postData.resetPassword = 1;
  837. return $.post(url, postData);
  838. },
  839. /**
  840. * Updates the server's configuration
  841. * @param {Object} configuration
  842. */
  843. updateServerConfiguration: function (configuration) {
  844. if (!configuration) {
  845. throw new Error("null configuration");
  846. }
  847. var url = ApiClient.getUrl("System/Configuration");
  848. return $.post(url, JSON.stringify(configuration));
  849. },
  850. /**
  851. * Creates a user
  852. * @param {Object} user
  853. */
  854. createUser: function (user) {
  855. if (!user) {
  856. throw new Error("null user");
  857. }
  858. var url = ApiClient.getUrl("Users");
  859. return $.post(url, JSON.stringify(user));
  860. },
  861. /**
  862. * Updates a user
  863. * @param {Object} user
  864. */
  865. updateUser: function (user) {
  866. if (!user) {
  867. throw new Error("null user");
  868. }
  869. var url = ApiClient.getUrl("Users/" + user.Id);
  870. return $.post(url, JSON.stringify(user));
  871. },
  872. /**
  873. * Updates the Triggers for a ScheduledTask
  874. * @param {String} id
  875. * @param {Object} triggers
  876. */
  877. updateScheduledTaskTriggers: function (id, triggers) {
  878. if (!id) {
  879. throw new Error("null id");
  880. }
  881. if (!triggers) {
  882. throw new Error("null triggers");
  883. }
  884. var url = ApiClient.getUrl("ScheduledTasks/" + id + "/Triggers");
  885. return $.post(url, JSON.stringify(triggers));
  886. },
  887. /**
  888. * Updates a plugin's configuration
  889. * @param {String} Id
  890. * @param {Object} configuration
  891. */
  892. updatePluginConfiguration: function (id, configuration) {
  893. if (!id) {
  894. throw new Error("null Id");
  895. }
  896. if (!configuration) {
  897. throw new Error("null configuration");
  898. }
  899. var url = ApiClient.getUrl("Plugins/" + id + "/Configuration");
  900. return $.post(url, JSON.stringify(configuration));
  901. },
  902. /**
  903. * Gets items based on a query, typicall for children of a folder
  904. * @param {String} userId
  905. * @param {Object} options
  906. * Options accepts the following properties:
  907. * itemId - Localize the search to a specific folder (root if omitted)
  908. * startIndex - Use for paging
  909. * limit - Use to limit results to a certain number of items
  910. * filter - Specify one or more ItemFilters, comma delimeted (see server-side enum)
  911. * sortBy - Specify an ItemSortBy (comma-delimeted list see server-side enum)
  912. * sortOrder - ascending/descending
  913. * fields - additional fields to include aside from basic info. This is a comma delimited list. See server-side enum ItemFields.
  914. * index - the name of the dynamic, localized index function
  915. * dynamicSortBy - the name of the dynamic localized sort function
  916. * recursive - Whether or not the query should be recursive
  917. * searchTerm - search term to use as a filter
  918. */
  919. getItems: function (userId, options) {
  920. if (!userId) {
  921. throw new Error("null userId");
  922. }
  923. return $.getJSON(ApiClient.getUrl("Users/" + userId + "/Items", options));
  924. },
  925. /**
  926. * Marks an item as played or unplayed
  927. * This should not be used to update playstate following playback.
  928. * There are separate playstate check-in methods for that. This should be used for a
  929. * separate option to reset playstate.
  930. * @param {String} userId
  931. * @param {String} itemId
  932. * @param {Boolean} wasPlayed
  933. */
  934. updatePlayedStatus: function (userId, itemId, wasPlayed) {
  935. if (!userId) {
  936. throw new Error("null userId");
  937. }
  938. if (!itemId) {
  939. throw new Error("null itemId");
  940. }
  941. var url = "Users/" + userId + "/PlayedItems/" + itemId;
  942. var method = wasPlayed ? "POST" : "DELETE";
  943. return $.ajax({
  944. type: method,
  945. url: url,
  946. dataType: "json"
  947. });
  948. },
  949. /**
  950. * Updates a user's favorite status for an item and returns the updated UserItemData object.
  951. * @param {String} userId
  952. * @param {String} itemId
  953. * @param {Boolean} isFavorite
  954. */
  955. updateFavoriteStatus: function (userId, itemId, isFavorite) {
  956. if (!userId) {
  957. throw new Error("null userId");
  958. }
  959. if (!itemId) {
  960. throw new Error("null itemId");
  961. }
  962. var url = "Users/" + userId + "/FavoriteItems/" + itemId;
  963. var method = isFavorite ? "POST" : "DELETE";
  964. return $.ajax({
  965. type: method,
  966. url: url,
  967. dataType: "json"
  968. });
  969. },
  970. /**
  971. * Updates a user's personal rating for an item
  972. * @param {String} userId
  973. * @param {String} itemId
  974. * @param {Boolean} likes
  975. */
  976. updateUserItemRating: function (userId, itemId, likes) {
  977. if (!userId) {
  978. throw new Error("null userId");
  979. }
  980. if (!itemId) {
  981. throw new Error("null itemId");
  982. }
  983. var url = ApiClient.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating", {
  984. likes: likes
  985. });
  986. return $.post(url);
  987. },
  988. /**
  989. * Clears a user's personal rating for an item
  990. * @param {String} userId
  991. * @param {String} itemId
  992. */
  993. clearUserItemRating: function (userId, itemId) {
  994. if (!userId) {
  995. throw new Error("null userId");
  996. }
  997. if (!itemId) {
  998. throw new Error("null itemId");
  999. }
  1000. var url = ApiClient.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  1001. return $.ajax({
  1002. type: "DELETE",
  1003. url: url,
  1004. dataType: "json"
  1005. });
  1006. }
  1007. };
  1008. // Do this initially. The consumer can always override later
  1009. ApiClient.inferServerFromUrl();