ApiClient.js 41 KB

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