ApiClient.js 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  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/DefaultVirtualFolders";
  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 params = {
  324. name: name,
  325. action: "RemoveVirtualFolder"
  326. };
  327. if (userId) {
  328. params.userId = userId;
  329. }
  330. var url = ApiClient.getUrl("UpdateMediaLibrary", params);
  331. return $.post(url);
  332. },
  333. /**
  334. * Adds a virtual folder to either the default view or a user view
  335. * @param {String} name
  336. */
  337. addVirtualFolder: function (name, userId) {
  338. if (!name) {
  339. throw new Error("null name");
  340. }
  341. var params = {
  342. name: name,
  343. action: "addVirtualFolder"
  344. };
  345. if (userId) {
  346. params.userId = userId;
  347. }
  348. var url = ApiClient.getUrl("UpdateMediaLibrary", params);
  349. return $.post(url);
  350. },
  351. /**
  352. * Renames a virtual folder, within either the default view or a user view
  353. * @param {String} name
  354. */
  355. renameVirtualFolder: function (name, newName, userId) {
  356. if (!name) {
  357. throw new Error("null name");
  358. }
  359. if (!newName) {
  360. throw new Error("null newName");
  361. }
  362. var params = {
  363. name: name,
  364. newName: newName,
  365. action: "RenameVirtualFolder"
  366. };
  367. if (userId) {
  368. params.userId = userId;
  369. }
  370. var url = ApiClient.getUrl("UpdateMediaLibrary", params);
  371. return $.post(url);
  372. },
  373. /**
  374. * Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view
  375. * @param {String} name
  376. */
  377. addMediaPath: function (virtualFolderName, mediaPath, userId) {
  378. if (!virtualFolderName) {
  379. throw new Error("null virtualFolderName");
  380. }
  381. if (!mediaPath) {
  382. throw new Error("null mediaPath");
  383. }
  384. var params = {
  385. virtualFolderName: virtualFolderName,
  386. mediaPath: mediaPath,
  387. action: "addMediaPath"
  388. };
  389. if (userId) {
  390. params.userId = userId;
  391. }
  392. var url = ApiClient.getUrl("UpdateMediaLibrary", params);
  393. return $.post(url);
  394. },
  395. /**
  396. * Removes a media path from a virtual folder, within either the default view or a user view
  397. * @param {String} name
  398. */
  399. removeMediaPath: function (virtualFolderName, mediaPath, userId) {
  400. if (!virtualFolderName) {
  401. throw new Error("null virtualFolderName");
  402. }
  403. if (!mediaPath) {
  404. throw new Error("null mediaPath");
  405. }
  406. var params = {
  407. virtualFolderName: virtualFolderName,
  408. mediaPath: mediaPath,
  409. action: "RemoveMediaPath"
  410. };
  411. if (userId) {
  412. params.userId = userId;
  413. }
  414. var url = ApiClient.getUrl("UpdateMediaLibrary", params);
  415. return $.post(url);
  416. },
  417. /**
  418. * Deletes a user
  419. * @param {String} id
  420. */
  421. deleteUser: function (id) {
  422. if (!id) {
  423. throw new Error("null id");
  424. }
  425. var url = ApiClient.getUrl("Users/" + id);
  426. return $.ajax({
  427. type: "DELETE",
  428. url: url,
  429. dataType: "json"
  430. });
  431. },
  432. /**
  433. * Deletes a user image
  434. * @param {String} userId
  435. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  436. */
  437. deleteUserImage: function (userId, imageType) {
  438. if (!userId) {
  439. throw new Error("null userId");
  440. }
  441. if (!imageType) {
  442. throw new Error("null imageType");
  443. }
  444. var url = ApiClient.getUrl("Users/" + userId + "/Images/" + imageType);
  445. return $.ajax({
  446. type: "DELETE",
  447. url: url,
  448. dataType: "json"
  449. });
  450. },
  451. /**
  452. * Uploads a user image
  453. * @param {String} userId
  454. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  455. * @param {Object} file The file from the input element
  456. */
  457. uploadUserImage: function (userId, imageType, file) {
  458. if (!userId) {
  459. throw new Error("null userId");
  460. }
  461. if (!imageType) {
  462. throw new Error("null imageType");
  463. }
  464. if (!file || !file.type.match('image.*')) {
  465. throw new Error("File must be an image.");
  466. }
  467. var deferred = $.Deferred();
  468. var reader = new FileReader();
  469. reader.onerror = function () {
  470. deferred.reject();
  471. };
  472. reader.onabort = function () {
  473. deferred.reject();
  474. };
  475. // Closure to capture the file information.
  476. reader.onload = function (e) {
  477. var data = window.btoa(e.target.result);
  478. var params = {
  479. userId: userId,
  480. type: imageType,
  481. filename: file.name
  482. };
  483. var url = ApiClient.getUrl("UploadImage", params);
  484. $.post(url, data).done(function (result) {
  485. deferred.resolveWith(null, [result]);
  486. }).fail(function () {
  487. deferred.reject();
  488. });
  489. };
  490. // Read in the image file as a data URL.
  491. reader.readAsBinaryString(file);
  492. return deferred.promise();
  493. },
  494. /**
  495. * Gets the list of installed plugins on the server
  496. */
  497. getInstalledPlugins: function () {
  498. var url = ApiClient.getUrl("Plugins");
  499. return $.getJSON(url);
  500. },
  501. /**
  502. * Gets a user by id
  503. * @param {String} id
  504. */
  505. getUser: function (id) {
  506. if (!id) {
  507. throw new Error("Must supply a userId");
  508. }
  509. var url = ApiClient.getUrl("Users/" + id);
  510. return $.getJSON(url);
  511. },
  512. /**
  513. * Gets a studio
  514. */
  515. getStudio: function (name) {
  516. if (!name) {
  517. throw new Error("null name");
  518. }
  519. var url = ApiClient.getUrl("Library/Studios/" + name);
  520. return $.getJSON(url);
  521. },
  522. /**
  523. * Gets a genre
  524. */
  525. getGenre: function (name) {
  526. if (!name) {
  527. throw new Error("null name");
  528. }
  529. var url = ApiClient.getUrl("Library/Genres/" + name);
  530. return $.getJSON(url);
  531. },
  532. /**
  533. * Gets a year
  534. */
  535. getYear: function (year) {
  536. if (!year) {
  537. throw new Error("null year");
  538. }
  539. var url = ApiClient.getUrl("Library/Years/" + year);
  540. return $.getJSON(url);
  541. },
  542. /**
  543. * Gets a Person
  544. */
  545. getPerson: function (name) {
  546. if (!name) {
  547. throw new Error("null name");
  548. }
  549. var url = ApiClient.getUrl("Library/Persons/" + name);
  550. return $.getJSON(url);
  551. },
  552. /**
  553. * Gets weather info
  554. * @param {String} location - us zip code / city, state, country / city, country
  555. * Omit location to get weather info using stored server configuration value
  556. */
  557. getWeatherInfo: function (location) {
  558. var url = ApiClient.getUrl("weather", {
  559. location: location
  560. });
  561. return $.getJSON(url);
  562. },
  563. /**
  564. * Gets all users from the server
  565. */
  566. getAllUsers: function () {
  567. var url = ApiClient.getUrl("users");
  568. return $.getJSON(url);
  569. },
  570. /**
  571. * Gets all available parental ratings from the server
  572. */
  573. getParentalRatings: function () {
  574. var url = ApiClient.getUrl("Localization/ParentalRatings");
  575. return $.getJSON(url);
  576. },
  577. /**
  578. * Gets a list of all available conrete BaseItem types from the server
  579. */
  580. getItemTypes: function (options) {
  581. var url = ApiClient.getUrl("Library/ItemTypes", options);
  582. return $.getJSON(url);
  583. },
  584. /**
  585. * Constructs a url for a user image
  586. * @param {String} userId
  587. * @param {Object} options
  588. * Options supports the following properties:
  589. * width - download the image at a fixed width
  590. * height - download the image at a fixed height
  591. * maxWidth - download the image at a maxWidth
  592. * maxHeight - download the image at a maxHeight
  593. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  594. * For best results do not specify both width and height together, as aspect ratio might be altered.
  595. */
  596. getUserImageUrl: function (userId, options) {
  597. if (!userId) {
  598. throw new Error("null userId");
  599. }
  600. options = options || {
  601. };
  602. var url = "Users/" + userId + "/Images/" + options.type;
  603. if (options.index != null) {
  604. url += "/" + options.index;
  605. }
  606. // Don't put these on the query string
  607. delete options.type;
  608. delete options.index;
  609. return ApiClient.getUrl(url, options);
  610. },
  611. /**
  612. * Constructs a url for a person image
  613. * @param {String} name
  614. * @param {Object} options
  615. * Options supports the following properties:
  616. * width - download the image at a fixed width
  617. * height - download the image at a fixed height
  618. * maxWidth - download the image at a maxWidth
  619. * maxHeight - download the image at a maxHeight
  620. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  621. * For best results do not specify both width and height together, as aspect ratio might be altered.
  622. */
  623. getPersonImageUrl: function (name, options) {
  624. if (!name) {
  625. throw new Error("null name");
  626. }
  627. options = options || {
  628. };
  629. var url = "Persons/" + name + "/Images/" + options.type;
  630. if (options.index != null) {
  631. url += "/" + options.index;
  632. }
  633. // Don't put these on the query string
  634. delete options.type;
  635. delete options.index;
  636. return ApiClient.getUrl(url, options);
  637. },
  638. /**
  639. * Constructs a url for a year image
  640. * @param {String} year
  641. * @param {Object} options
  642. * Options supports the following properties:
  643. * width - download the image at a fixed width
  644. * height - download the image at a fixed height
  645. * maxWidth - download the image at a maxWidth
  646. * maxHeight - download the image at a maxHeight
  647. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  648. * For best results do not specify both width and height together, as aspect ratio might be altered.
  649. */
  650. getYearImageUrl: function (year, options) {
  651. if (!year) {
  652. throw new Error("null year");
  653. }
  654. options = options || {
  655. };
  656. var url = "Years/" + year + "/Images/" + options.type;
  657. if (options.index != null) {
  658. url += "/" + options.index;
  659. }
  660. // Don't put these on the query string
  661. delete options.type;
  662. delete options.index;
  663. return ApiClient.getUrl(url, options);
  664. },
  665. /**
  666. * Constructs a url for a genre image
  667. * @param {String} name
  668. * @param {Object} options
  669. * Options supports the following properties:
  670. * width - download the image at a fixed width
  671. * height - download the image at a fixed height
  672. * maxWidth - download the image at a maxWidth
  673. * maxHeight - download the image at a maxHeight
  674. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  675. * For best results do not specify both width and height together, as aspect ratio might be altered.
  676. */
  677. getGenreImageUrl: function (name, options) {
  678. if (!name) {
  679. throw new Error("null name");
  680. }
  681. options = options || {
  682. };
  683. var url = "Genres/" + name + "/Images/" + options.type;
  684. if (options.index != null) {
  685. url += "/" + options.index;
  686. }
  687. // Don't put these on the query string
  688. delete options.type;
  689. delete options.index;
  690. return ApiClient.getUrl(url, options);
  691. },
  692. /**
  693. * Constructs a url for a genre image
  694. * @param {String} name
  695. * @param {Object} options
  696. * Options supports the following properties:
  697. * width - download the image at a fixed width
  698. * height - download the image at a fixed height
  699. * maxWidth - download the image at a maxWidth
  700. * maxHeight - download the image at a maxHeight
  701. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  702. * For best results do not specify both width and height together, as aspect ratio might be altered.
  703. */
  704. getStudioImageUrl: function (name, options) {
  705. if (!name) {
  706. throw new Error("null name");
  707. }
  708. options = options || {
  709. };
  710. var url = "Studios/" + name + "/Images/" + options.type;
  711. if (options.index != null) {
  712. url += "/" + options.index;
  713. }
  714. // Don't put these on the query string
  715. delete options.type;
  716. delete options.index;
  717. return ApiClient.getUrl(url, options);
  718. },
  719. /**
  720. * Constructs a url for an item image
  721. * @param {String} itemId
  722. * @param {Object} options
  723. * Options supports the following properties:
  724. * type - Primary, logo, backdrop, etc. See the server-side enum ImageType
  725. * index - When downloading a backdrop, use this to specify which one (omitting is equivalent to zero)
  726. * width - download the image at a fixed width
  727. * height - download the image at a fixed height
  728. * maxWidth - download the image at a maxWidth
  729. * maxHeight - download the image at a maxHeight
  730. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  731. * For best results do not specify both width and height together, as aspect ratio might be altered.
  732. */
  733. getImageUrl: function (itemId, options) {
  734. if (!itemId) {
  735. throw new Error("itemId cannot be empty");
  736. }
  737. options = options || {
  738. };
  739. var url = "Items/" + itemId + "/Images/" + options.type;
  740. if (options.index != null) {
  741. url += "/" + options.index;
  742. }
  743. // Don't put these on the query string
  744. delete options.type;
  745. delete options.index;
  746. return ApiClient.getUrl(url, options);
  747. },
  748. /**
  749. * Constructs a url for an item logo image
  750. * If the item doesn't have a logo, it will inherit a logo 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. getLogoImageUrl: function (item, options) {
  762. if (!item) {
  763. throw new Error("null item");
  764. }
  765. options = options || {
  766. };
  767. options.imageType = "logo";
  768. var logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId;
  769. return logoItemId ? ApiClient.getImageUrl(logoItemId, options) : null;
  770. },
  771. /**
  772. * Constructs an array of backdrop image url's for an item
  773. * If the item doesn't have any backdrops, it will inherit them from a parent
  774. * @param {Object} item A BaseItem
  775. * @param {Object} options
  776. * Options supports the following properties:
  777. * width - download the image at a fixed width
  778. * height - download the image at a fixed height
  779. * maxWidth - download the image at a maxWidth
  780. * maxHeight - download the image at a maxHeight
  781. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  782. * For best results do not specify both width and height together, as aspect ratio might be altered.
  783. */
  784. getBackdropImageUrl: function (item, options) {
  785. if (!item) {
  786. throw new Error("null item");
  787. }
  788. options = options || {
  789. };
  790. options.imageType = "backdrop";
  791. var backdropItemId;
  792. var backdropCount;
  793. if (!item.BackdropCount) {
  794. backdropItemId = item.ParentBackdropItemId;
  795. backdropCount = item.ParentBackdropCount || 0;
  796. } else {
  797. backdropItemId = item.Id;
  798. backdropCount = item.BackdropCount;
  799. }
  800. if (!backdropItemId) {
  801. return [];
  802. }
  803. var files = [];
  804. for (var i = 0; i < backdropCount; i++) {
  805. options.imageIndex = i;
  806. files[i] = ApiClient.getImageUrl(backdropItemId, options);
  807. }
  808. return files;
  809. },
  810. /**
  811. * Authenticates a user
  812. * @param {String} userId
  813. * @param {String} password
  814. */
  815. authenticateUser: function (userId, password) {
  816. if (!userId) {
  817. throw new Error("null userId");
  818. }
  819. var url = ApiClient.getUrl("Users/" + userId + "/authenticate");
  820. var postData = {
  821. };
  822. if (password) {
  823. postData.password = password;
  824. }
  825. return $.post(url, postData);
  826. },
  827. /**
  828. * Updates a user's password
  829. * @param {String} userId
  830. * @param {String} currentPassword
  831. * @param {String} newPassword
  832. */
  833. updateUserPassword: function (userId, currentPassword, newPassword) {
  834. if (!userId) {
  835. throw new Error("null userId");
  836. }
  837. var url = ApiClient.getUrl("Users/" + userId + "/Password");
  838. var postData = {
  839. };
  840. if (currentPassword) {
  841. postData.currentPassword = currentPassword;
  842. }
  843. if (newPassword) {
  844. postData.newPassword = newPassword;
  845. }
  846. return $.post(url, postData);
  847. },
  848. /**
  849. * Resets a user's password
  850. * @param {String} userId
  851. */
  852. resetUserPassword: function (userId) {
  853. if (!userId) {
  854. throw new Error("null userId");
  855. }
  856. var url = ApiClient.getUrl("Users/" + userId + "/Password");
  857. var postData = {
  858. };
  859. postData.resetPassword = 1;
  860. return $.post(url, postData);
  861. },
  862. /**
  863. * Updates the server's configuration
  864. * @param {Object} configuration
  865. */
  866. updateServerConfiguration: function (configuration) {
  867. if (!configuration) {
  868. throw new Error("null configuration");
  869. }
  870. var url = ApiClient.getUrl("System/Configuration");
  871. return $.post(url, JSON.stringify(configuration));
  872. },
  873. /**
  874. * Creates a user
  875. * @param {Object} user
  876. */
  877. createUser: function (user) {
  878. if (!user) {
  879. throw new Error("null user");
  880. }
  881. var url = ApiClient.getUrl("Users");
  882. return $.post(url, JSON.stringify(user));
  883. },
  884. /**
  885. * Updates a user
  886. * @param {Object} user
  887. */
  888. updateUser: function (user) {
  889. if (!user) {
  890. throw new Error("null user");
  891. }
  892. var url = ApiClient.getUrl("Users/" + user.Id);
  893. return $.post(url, JSON.stringify(user));
  894. },
  895. /**
  896. * Updates the Triggers for a ScheduledTask
  897. * @param {String} id
  898. * @param {Object} triggers
  899. */
  900. updateScheduledTaskTriggers: function (id, triggers) {
  901. if (!id) {
  902. throw new Error("null id");
  903. }
  904. if (!triggers) {
  905. throw new Error("null triggers");
  906. }
  907. var url = ApiClient.getUrl("ScheduledTasks/" + id + "/Triggers");
  908. return $.post(url, JSON.stringify(triggers));
  909. },
  910. /**
  911. * Updates a plugin's configuration
  912. * @param {String} Id
  913. * @param {Object} configuration
  914. */
  915. updatePluginConfiguration: function (id, configuration) {
  916. if (!id) {
  917. throw new Error("null Id");
  918. }
  919. if (!configuration) {
  920. throw new Error("null configuration");
  921. }
  922. var url = ApiClient.getUrl("Plugins/" + id + "/Configuration");
  923. return $.post(url, JSON.stringify(configuration));
  924. },
  925. /**
  926. * Gets items based on a query, typicall for children of a folder
  927. * @param {String} userId
  928. * @param {Object} options
  929. * Options accepts the following properties:
  930. * itemId - Localize the search to a specific folder (root if omitted)
  931. * startIndex - Use for paging
  932. * limit - Use to limit results to a certain number of items
  933. * filter - Specify one or more ItemFilters, comma delimeted (see server-side enum)
  934. * sortBy - Specify an ItemSortBy (comma-delimeted list see server-side enum)
  935. * sortOrder - ascending/descending
  936. * fields - additional fields to include aside from basic info. This is a comma delimited list. See server-side enum ItemFields.
  937. * index - the name of the dynamic, localized index function
  938. * dynamicSortBy - the name of the dynamic localized sort function
  939. * recursive - Whether or not the query should be recursive
  940. * searchTerm - search term to use as a filter
  941. */
  942. getItems: function (userId, options) {
  943. if (!userId) {
  944. throw new Error("null userId");
  945. }
  946. return $.getJSON(ApiClient.getUrl("Users/" + userId + "/Items", options));
  947. },
  948. /**
  949. * Marks an item as played or unplayed
  950. * This should not be used to update playstate following playback.
  951. * There are separate playstate check-in methods for that. This should be used for a
  952. * separate option to reset playstate.
  953. * @param {String} userId
  954. * @param {String} itemId
  955. * @param {Boolean} wasPlayed
  956. */
  957. updatePlayedStatus: function (userId, itemId, wasPlayed) {
  958. if (!userId) {
  959. throw new Error("null userId");
  960. }
  961. if (!itemId) {
  962. throw new Error("null itemId");
  963. }
  964. var url = "Users/" + userId + "/PlayedItems/" + itemId;
  965. var method = wasPlayed ? "POST" : "DELETE";
  966. return $.ajax({
  967. type: method,
  968. url: url,
  969. dataType: "json"
  970. });
  971. },
  972. /**
  973. * Updates a user's favorite status for an item and returns the updated UserItemData object.
  974. * @param {String} userId
  975. * @param {String} itemId
  976. * @param {Boolean} isFavorite
  977. */
  978. updateFavoriteStatus: function (userId, itemId, isFavorite) {
  979. if (!userId) {
  980. throw new Error("null userId");
  981. }
  982. if (!itemId) {
  983. throw new Error("null itemId");
  984. }
  985. var url = "Users/" + userId + "/FavoriteItems/" + itemId;
  986. var method = isFavorite ? "POST" : "DELETE";
  987. return $.ajax({
  988. type: method,
  989. url: url,
  990. dataType: "json"
  991. });
  992. },
  993. /**
  994. * Updates a user's personal rating for an item
  995. * @param {String} userId
  996. * @param {String} itemId
  997. * @param {Boolean} likes
  998. */
  999. updateUserItemRating: function (userId, itemId, likes) {
  1000. if (!userId) {
  1001. throw new Error("null userId");
  1002. }
  1003. if (!itemId) {
  1004. throw new Error("null itemId");
  1005. }
  1006. var url = ApiClient.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating", {
  1007. likes: likes
  1008. });
  1009. return $.post(url);
  1010. },
  1011. /**
  1012. * Clears a user's personal rating for an item
  1013. * @param {String} userId
  1014. * @param {String} itemId
  1015. */
  1016. clearUserItemRating: function (userId, itemId) {
  1017. if (!userId) {
  1018. throw new Error("null userId");
  1019. }
  1020. if (!itemId) {
  1021. throw new Error("null itemId");
  1022. }
  1023. var url = ApiClient.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  1024. return $.ajax({
  1025. type: "DELETE",
  1026. url: url,
  1027. dataType: "json"
  1028. });
  1029. }
  1030. };
  1031. // Do this initially. The consumer can always override later
  1032. ApiClient.inferServerFromUrl();