ApiClient.js 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  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 devices from the server
  149. */
  150. getNetworkDevices: function () {
  151. var url = ApiClient.getUrl("Environment/NetworkDevices");
  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 $.ajax({
  803. type: "POST",
  804. url: url,
  805. data: JSON.stringify(postData),
  806. dataType: "json",
  807. contentType: "application/json"
  808. });
  809. },
  810. /**
  811. * Updates a user's password
  812. * @param {String} userId
  813. * @param {String} currentPassword
  814. * @param {String} newPassword
  815. */
  816. updateUserPassword: function (userId, currentPassword, newPassword) {
  817. if (!userId) {
  818. throw new Error("null userId");
  819. }
  820. var url = ApiClient.getUrl("Users/" + userId + "/Password");
  821. var postData = {
  822. };
  823. if (currentPassword) {
  824. postData.currentPassword = currentPassword;
  825. }
  826. if (newPassword) {
  827. postData.newPassword = newPassword;
  828. }
  829. return $.post(url, postData);
  830. },
  831. /**
  832. * Resets a user's password
  833. * @param {String} userId
  834. */
  835. resetUserPassword: function (userId) {
  836. if (!userId) {
  837. throw new Error("null userId");
  838. }
  839. var url = ApiClient.getUrl("Users/" + userId + "/Password");
  840. var postData = {
  841. };
  842. postData.resetPassword = 1;
  843. return $.post(url, postData);
  844. },
  845. /**
  846. * Updates the server's configuration
  847. * @param {Object} configuration
  848. */
  849. updateServerConfiguration: function (configuration) {
  850. if (!configuration) {
  851. throw new Error("null configuration");
  852. }
  853. var url = ApiClient.getUrl("System/Configuration");
  854. return $.ajax({
  855. type: "POST",
  856. url: url,
  857. data: JSON.stringify(configuration),
  858. dataType: "json",
  859. contentType: "application/json"
  860. });
  861. },
  862. /**
  863. * Updates plugin security info
  864. */
  865. updatePluginSecurityInfo: function (info) {
  866. var url = ApiClient.getUrl("Plugins/SecurityInfo");
  867. return $.ajax({
  868. type: "POST",
  869. url: url,
  870. data: JSON.stringify(info),
  871. dataType: "json",
  872. contentType: "application/json"
  873. });
  874. },
  875. /**
  876. * Creates a user
  877. * @param {Object} user
  878. */
  879. createUser: function (user) {
  880. if (!user) {
  881. throw new Error("null user");
  882. }
  883. var url = ApiClient.getUrl("Users");
  884. return $.ajax({
  885. type: "POST",
  886. url: url,
  887. data: JSON.stringify(user),
  888. dataType: "json",
  889. contentType: "application/json"
  890. });
  891. },
  892. /**
  893. * Updates a user
  894. * @param {Object} user
  895. */
  896. updateUser: function (user) {
  897. if (!user) {
  898. throw new Error("null user");
  899. }
  900. var url = ApiClient.getUrl("Users/" + user.Id);
  901. return $.ajax({
  902. type: "POST",
  903. url: url,
  904. data: JSON.stringify(user),
  905. dataType: "json",
  906. contentType: "application/json"
  907. });
  908. },
  909. /**
  910. * Updates the Triggers for a ScheduledTask
  911. * @param {String} id
  912. * @param {Object} triggers
  913. */
  914. updateScheduledTaskTriggers: function (id, triggers) {
  915. if (!id) {
  916. throw new Error("null id");
  917. }
  918. if (!triggers) {
  919. throw new Error("null triggers");
  920. }
  921. var url = ApiClient.getUrl("ScheduledTasks/" + id + "/Triggers");
  922. return $.ajax({
  923. type: "POST",
  924. url: url,
  925. data: JSON.stringify(triggers),
  926. dataType: "json",
  927. contentType: "application/json"
  928. });
  929. },
  930. /**
  931. * Updates a plugin's configuration
  932. * @param {String} Id
  933. * @param {Object} configuration
  934. */
  935. updatePluginConfiguration: function (id, configuration) {
  936. if (!id) {
  937. throw new Error("null Id");
  938. }
  939. if (!configuration) {
  940. throw new Error("null configuration");
  941. }
  942. var url = ApiClient.getUrl("Plugins/" + id + "/Configuration");
  943. return $.ajax({
  944. type: "POST",
  945. url: url,
  946. data: JSON.stringify(configuration),
  947. dataType: "json",
  948. contentType: "application/json"
  949. });
  950. },
  951. /**
  952. * Gets items based on a query, typicall for children of a folder
  953. * @param {String} userId
  954. * @param {Object} options
  955. * Options accepts the following properties:
  956. * itemId - Localize the search to a specific folder (root if omitted)
  957. * startIndex - Use for paging
  958. * limit - Use to limit results to a certain number of items
  959. * filter - Specify one or more ItemFilters, comma delimeted (see server-side enum)
  960. * sortBy - Specify an ItemSortBy (comma-delimeted list see server-side enum)
  961. * sortOrder - ascending/descending
  962. * fields - additional fields to include aside from basic info. This is a comma delimited list. See server-side enum ItemFields.
  963. * index - the name of the dynamic, localized index function
  964. * dynamicSortBy - the name of the dynamic localized sort function
  965. * recursive - Whether or not the query should be recursive
  966. * searchTerm - search term to use as a filter
  967. */
  968. getItems: function (userId, options) {
  969. if (!userId) {
  970. throw new Error("null userId");
  971. }
  972. return $.getJSON(ApiClient.getUrl("Users/" + userId + "/Items", options));
  973. },
  974. /**
  975. * Marks an item as played or unplayed
  976. * This should not be used to update playstate following playback.
  977. * There are separate playstate check-in methods for that. This should be used for a
  978. * separate option to reset playstate.
  979. * @param {String} userId
  980. * @param {String} itemId
  981. * @param {Boolean} wasPlayed
  982. */
  983. updatePlayedStatus: function (userId, itemId, wasPlayed) {
  984. if (!userId) {
  985. throw new Error("null userId");
  986. }
  987. if (!itemId) {
  988. throw new Error("null itemId");
  989. }
  990. var url = "Users/" + userId + "/PlayedItems/" + itemId;
  991. var method = wasPlayed ? "POST" : "DELETE";
  992. return $.ajax({
  993. type: method,
  994. url: url,
  995. dataType: "json"
  996. });
  997. },
  998. /**
  999. * Updates a user's favorite status for an item and returns the updated UserItemData object.
  1000. * @param {String} userId
  1001. * @param {String} itemId
  1002. * @param {Boolean} isFavorite
  1003. */
  1004. updateFavoriteStatus: function (userId, itemId, isFavorite) {
  1005. if (!userId) {
  1006. throw new Error("null userId");
  1007. }
  1008. if (!itemId) {
  1009. throw new Error("null itemId");
  1010. }
  1011. var url = "Users/" + userId + "/FavoriteItems/" + itemId;
  1012. var method = isFavorite ? "POST" : "DELETE";
  1013. return $.ajax({
  1014. type: method,
  1015. url: url,
  1016. dataType: "json"
  1017. });
  1018. },
  1019. /**
  1020. * Updates a user's personal rating for an item
  1021. * @param {String} userId
  1022. * @param {String} itemId
  1023. * @param {Boolean} likes
  1024. */
  1025. updateUserItemRating: function (userId, itemId, likes) {
  1026. if (!userId) {
  1027. throw new Error("null userId");
  1028. }
  1029. if (!itemId) {
  1030. throw new Error("null itemId");
  1031. }
  1032. var url = ApiClient.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating", {
  1033. likes: likes
  1034. });
  1035. return $.post(url);
  1036. },
  1037. /**
  1038. * Clears a user's personal rating for an item
  1039. * @param {String} userId
  1040. * @param {String} itemId
  1041. */
  1042. clearUserItemRating: function (userId, itemId) {
  1043. if (!userId) {
  1044. throw new Error("null userId");
  1045. }
  1046. if (!itemId) {
  1047. throw new Error("null itemId");
  1048. }
  1049. var url = ApiClient.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  1050. return $.ajax({
  1051. type: "DELETE",
  1052. url: url,
  1053. dataType: "json"
  1054. });
  1055. }
  1056. };
  1057. // Do this initially. The consumer can always override later
  1058. ApiClient.inferServerFromUrl();