2
0

ApiClient.js 34 KB

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