ApiClient.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Model.DTO;
  8. using MediaBrowser.Model.Entities;
  9. namespace MediaBrowser.ApiInteraction
  10. {
  11. public class ApiClient : IDisposable
  12. {
  13. public ApiClient(HttpClientHandler handler)
  14. {
  15. handler.AutomaticDecompression = DecompressionMethods.Deflate;
  16. HttpClient = new HttpClient(handler);
  17. }
  18. /// <summary>
  19. /// Gets or sets the server host name (myserver or 192.168.x.x)
  20. /// </summary>
  21. public string ServerHostName { get; set; }
  22. /// <summary>
  23. /// Gets or sets the port number used by the API
  24. /// </summary>
  25. public int ServerApiPort { get; set; }
  26. /// <summary>
  27. /// Gets the current api url based on hostname and port.
  28. /// </summary>
  29. protected string ApiUrl
  30. {
  31. get
  32. {
  33. return string.Format("http://{0}:{1}/mediabrowser/api", ServerHostName, ServerApiPort);
  34. }
  35. }
  36. /// <summary>
  37. /// Gets the data format to request from the server
  38. /// </summary>
  39. private SerializationFormat SerializationFormat
  40. {
  41. get
  42. {
  43. // First try Protobuf since it has the best performance
  44. if (DataSerializer.CanDeserializeProtobuf)
  45. {
  46. return ApiInteraction.SerializationFormat.Protobuf;
  47. }
  48. // Next best is jsv
  49. if (DataSerializer.CanDeserializeJsv)
  50. {
  51. return ApiInteraction.SerializationFormat.Jsv;
  52. }
  53. return ApiInteraction.SerializationFormat.Json;
  54. }
  55. }
  56. public HttpClient HttpClient { get; private set; }
  57. public IDataSerializer DataSerializer { get; set; }
  58. /// <summary>
  59. /// Gets an image url that can be used to download an image from the api
  60. /// </summary>
  61. /// <param name="itemId">The Id of the item</param>
  62. /// <param name="imageType">The type of image requested</param>
  63. /// <param name="imageIndex">The image index, if there are multiple. Currently only applies to backdrops. Supply null or 0 for first backdrop.</param>
  64. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  65. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  66. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  67. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  68. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  69. public string GetImageUrl(Guid itemId, ImageType imageType, int? imageIndex = null, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  70. {
  71. string url = ApiUrl + "/image";
  72. url += "?id=" + itemId.ToString();
  73. url += "&type=" + imageType.ToString();
  74. if (imageIndex.HasValue)
  75. {
  76. url += "&index=" + imageIndex;
  77. }
  78. if (width.HasValue)
  79. {
  80. url += "&width=" + width;
  81. }
  82. if (height.HasValue)
  83. {
  84. url += "&height=" + height;
  85. }
  86. if (maxWidth.HasValue)
  87. {
  88. url += "&maxWidth=" + maxWidth;
  89. }
  90. if (maxHeight.HasValue)
  91. {
  92. url += "&maxHeight=" + maxHeight;
  93. }
  94. if (quality.HasValue)
  95. {
  96. url += "&quality=" + quality;
  97. }
  98. return url;
  99. }
  100. /// <summary>
  101. /// Gets an image url that can be used to download an image from the api
  102. /// </summary>
  103. /// <param name="userId">The Id of the user</param>
  104. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  105. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  106. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  107. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  108. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  109. public string GetUserImageUrl(Guid userId, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  110. {
  111. string url = ApiUrl + "/image";
  112. url += "?userId=" + userId.ToString();
  113. if (width.HasValue)
  114. {
  115. url += "&width=" + width;
  116. }
  117. if (height.HasValue)
  118. {
  119. url += "&height=" + height;
  120. }
  121. if (maxWidth.HasValue)
  122. {
  123. url += "&maxWidth=" + maxWidth;
  124. }
  125. if (maxHeight.HasValue)
  126. {
  127. url += "&maxHeight=" + maxHeight;
  128. }
  129. if (quality.HasValue)
  130. {
  131. url += "&quality=" + quality;
  132. }
  133. return url;
  134. }
  135. /// <summary>
  136. /// Gets an image url that can be used to download an image from the api
  137. /// </summary>
  138. /// <param name="name">The name of the person</param>
  139. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  140. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  141. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  142. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  143. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  144. public string GetPersonImageUrl(string name, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  145. {
  146. string url = ApiUrl + "/image";
  147. url += "?personname=" + name;
  148. if (width.HasValue)
  149. {
  150. url += "&width=" + width;
  151. }
  152. if (height.HasValue)
  153. {
  154. url += "&height=" + height;
  155. }
  156. if (maxWidth.HasValue)
  157. {
  158. url += "&maxWidth=" + maxWidth;
  159. }
  160. if (maxHeight.HasValue)
  161. {
  162. url += "&maxHeight=" + maxHeight;
  163. }
  164. if (quality.HasValue)
  165. {
  166. url += "&quality=" + quality;
  167. }
  168. return url;
  169. }
  170. /// <summary>
  171. /// Gets an image url that can be used to download an image from the api
  172. /// </summary>
  173. /// <param name="year">The year</param>
  174. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  175. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  176. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  177. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  178. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  179. public string GetYearImageUrl(int year, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  180. {
  181. string url = ApiUrl + "/image";
  182. url += "?year=" + year;
  183. if (width.HasValue)
  184. {
  185. url += "&width=" + width;
  186. }
  187. if (height.HasValue)
  188. {
  189. url += "&height=" + height;
  190. }
  191. if (maxWidth.HasValue)
  192. {
  193. url += "&maxWidth=" + maxWidth;
  194. }
  195. if (maxHeight.HasValue)
  196. {
  197. url += "&maxHeight=" + maxHeight;
  198. }
  199. if (quality.HasValue)
  200. {
  201. url += "&quality=" + quality;
  202. }
  203. return url;
  204. }
  205. /// <summary>
  206. /// Gets an image url that can be used to download an image from the api
  207. /// </summary>
  208. /// <param name="name">The name of the genre</param>
  209. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  210. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  211. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  212. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  213. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  214. public string GetGenreImageUrl(string name, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  215. {
  216. string url = ApiUrl + "/image";
  217. url += "?genre=" + name;
  218. if (width.HasValue)
  219. {
  220. url += "&width=" + width;
  221. }
  222. if (height.HasValue)
  223. {
  224. url += "&height=" + height;
  225. }
  226. if (maxWidth.HasValue)
  227. {
  228. url += "&maxWidth=" + maxWidth;
  229. }
  230. if (maxHeight.HasValue)
  231. {
  232. url += "&maxHeight=" + maxHeight;
  233. }
  234. if (quality.HasValue)
  235. {
  236. url += "&quality=" + quality;
  237. }
  238. return url;
  239. }
  240. /// <summary>
  241. /// Gets an image url that can be used to download an image from the api
  242. /// </summary>
  243. /// <param name="name">The name of the studio</param>
  244. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  245. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  246. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  247. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  248. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  249. public string GetStudioImageUrl(string name, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  250. {
  251. string url = ApiUrl + "/image";
  252. url += "?studio=" + name;
  253. if (width.HasValue)
  254. {
  255. url += "&width=" + width;
  256. }
  257. if (height.HasValue)
  258. {
  259. url += "&height=" + height;
  260. }
  261. if (maxWidth.HasValue)
  262. {
  263. url += "&maxWidth=" + maxWidth;
  264. }
  265. if (maxHeight.HasValue)
  266. {
  267. url += "&maxHeight=" + maxHeight;
  268. }
  269. if (quality.HasValue)
  270. {
  271. url += "&quality=" + quality;
  272. }
  273. return url;
  274. }
  275. /// <summary>
  276. /// This is a helper to get a list of backdrop url's from a given ApiBaseItemWrapper. If the actual item does not have any backdrops it will return backdrops from the first parent that does.
  277. /// </summary>
  278. /// <param name="item">A given item.</param>
  279. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  280. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  281. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  282. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  283. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  284. public IEnumerable<string> GetBackdropImageUrls(DTOBaseItem item, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  285. {
  286. Guid? backdropItemId = null;
  287. int backdropCount = 0;
  288. if (item.BackdropCount == 0)
  289. {
  290. backdropItemId = item.ParentBackdropItemId;
  291. backdropCount = item.ParentBackdropCount ?? 0;
  292. }
  293. else
  294. {
  295. backdropItemId = item.Id;
  296. backdropCount = item.BackdropCount;
  297. }
  298. if (backdropItemId == null)
  299. {
  300. return new string[] { };
  301. }
  302. List<string> files = new List<string>();
  303. for (int i = 0; i < backdropCount; i++)
  304. {
  305. files.Add(GetImageUrl(backdropItemId.Value, ImageType.Backdrop, i, width, height, maxWidth, maxHeight, quality));
  306. }
  307. return files;
  308. }
  309. /// <summary>
  310. /// This is a helper to get the logo image url from a given ApiBaseItemWrapper. If the actual item does not have a logo, it will return the logo from the first parent that does, or null.
  311. /// </summary>
  312. /// <param name="item">A given item.</param>
  313. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  314. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  315. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  316. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  317. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  318. public string GetLogoImageUrl(DTOBaseItem item, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  319. {
  320. Guid? logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId;
  321. if (logoItemId.HasValue)
  322. {
  323. return GetImageUrl(logoItemId.Value, ImageType.Logo, null, width, height, maxWidth, maxHeight, quality);
  324. }
  325. return null;
  326. }
  327. /// <summary>
  328. /// Gets an image stream based on a url
  329. /// </summary>
  330. public Task<Stream> GetImageStreamAsync(string url)
  331. {
  332. return GetStreamAsync(url);
  333. }
  334. /// <summary>
  335. /// Gets a BaseItem
  336. /// </summary>
  337. public async Task<DTOBaseItem> GetItemAsync(Guid id, Guid userId)
  338. {
  339. string url = ApiUrl + "/item?userId=" + userId.ToString();
  340. if (id != Guid.Empty)
  341. {
  342. url += "&id=" + id.ToString();
  343. }
  344. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  345. {
  346. return DeserializeFromStream<DTOBaseItem>(stream);
  347. }
  348. }
  349. /// <summary>
  350. /// Gets all Users
  351. /// </summary>
  352. public async Task<IEnumerable<DTOUser>> GetAllUsersAsync()
  353. {
  354. string url = ApiUrl + "/users";
  355. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  356. {
  357. return DeserializeFromStream<DTOUser[]>(stream);
  358. }
  359. }
  360. /// <summary>
  361. /// Gets all Genres
  362. /// </summary>
  363. public async Task<IEnumerable<IBNItem>> GetAllGenresAsync(Guid userId)
  364. {
  365. string url = ApiUrl + "/genres?userId=" + userId.ToString();
  366. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  367. {
  368. return DeserializeFromStream<IBNItem[]>(stream);
  369. }
  370. }
  371. /// <summary>
  372. /// Gets all Years
  373. /// </summary>
  374. public async Task<IEnumerable<IBNItem>> GetAllYearsAsync(Guid userId)
  375. {
  376. string url = ApiUrl + "/years?userId=" + userId.ToString();
  377. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  378. {
  379. return DeserializeFromStream<IBNItem[]>(stream);
  380. }
  381. }
  382. /// <summary>
  383. /// Gets all items that contain a given Year
  384. /// </summary>
  385. public async Task<IEnumerable<DTOBaseItem>> GetItemsWithYearAsync(string name, Guid userId)
  386. {
  387. string url = ApiUrl + "/itemlist?listtype=itemswithyear&userId=" + userId.ToString() + "&name=" + name;
  388. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  389. {
  390. return DeserializeFromStream<DTOBaseItem[]>(stream);
  391. }
  392. }
  393. /// <summary>
  394. /// Gets all items that contain a given Genre
  395. /// </summary>
  396. public async Task<IEnumerable<DTOBaseItem>> GetItemsWithGenreAsync(string name, Guid userId)
  397. {
  398. string url = ApiUrl + "/itemlist?listtype=itemswithgenre&userId=" + userId.ToString() + "&name=" + name;
  399. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  400. {
  401. return DeserializeFromStream<DTOBaseItem[]>(stream);
  402. }
  403. }
  404. /// <summary>
  405. /// Gets all items that contain a given Person
  406. /// </summary>
  407. public async Task<IEnumerable<DTOBaseItem>> GetItemsWithPersonAsync(string name, Guid userId)
  408. {
  409. string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId.ToString() + "&name=" + name;
  410. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  411. {
  412. return DeserializeFromStream<DTOBaseItem[]>(stream);
  413. }
  414. }
  415. /// <summary>
  416. /// Gets all items that contain a given Person
  417. /// </summary>
  418. public async Task<IEnumerable<DTOBaseItem>> GetItemsWithPersonAsync(string name, string personType, Guid userId)
  419. {
  420. string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId.ToString() + "&name=" + name;
  421. url += "&persontype=" + personType;
  422. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  423. {
  424. return DeserializeFromStream<DTOBaseItem[]>(stream);
  425. }
  426. }
  427. /// <summary>
  428. /// Gets all studious
  429. /// </summary>
  430. public async Task<IEnumerable<IBNItem>> GetAllStudiosAsync(Guid userId)
  431. {
  432. string url = ApiUrl + "/studios?userId=" + userId.ToString();
  433. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  434. {
  435. return DeserializeFromStream<IBNItem[]>(stream);
  436. }
  437. }
  438. /// <summary>
  439. /// Gets all items that contain a given Studio
  440. /// </summary>
  441. public async Task<IEnumerable<DTOBaseItem>> GetItemsWithStudioAsync(string name, Guid userId)
  442. {
  443. string url = ApiUrl + "/itemlist?listtype=itemswithstudio&userId=" + userId.ToString() + "&name=" + name;
  444. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  445. {
  446. return DeserializeFromStream<DTOBaseItem[]>(stream);
  447. }
  448. }
  449. /// <summary>
  450. /// Gets a studio
  451. /// </summary>
  452. public async Task<IBNItem> GetStudioAsync(Guid userId, string name)
  453. {
  454. string url = ApiUrl + "/studio?userId=" + userId.ToString() + "&name=" + name;
  455. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  456. {
  457. return DeserializeFromStream<IBNItem>(stream);
  458. }
  459. }
  460. /// <summary>
  461. /// Gets a genre
  462. /// </summary>
  463. public async Task<IBNItem> GetGenreAsync(Guid userId, string name)
  464. {
  465. string url = ApiUrl + "/genre?userId=" + userId.ToString() + "&name=" + name;
  466. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  467. {
  468. return DeserializeFromStream<IBNItem>(stream);
  469. }
  470. }
  471. /// <summary>
  472. /// Gets a person
  473. /// </summary>
  474. public async Task<IBNItem> GetPersonAsync(Guid userId, string name)
  475. {
  476. string url = ApiUrl + "/person?userId=" + userId.ToString() + "&name=" + name;
  477. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  478. {
  479. return DeserializeFromStream<IBNItem>(stream);
  480. }
  481. }
  482. /// <summary>
  483. /// Gets a year
  484. /// </summary>
  485. public async Task<IBNItem> GetYearAsync(Guid userId, int year)
  486. {
  487. string url = ApiUrl + "/year?userId=" + userId.ToString() + "&year=" + year;
  488. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  489. {
  490. return DeserializeFromStream<IBNItem>(stream);
  491. }
  492. }
  493. /// <summary>
  494. /// This is a helper around getting a stream from the server that contains serialized data
  495. /// </summary>
  496. private Task<Stream> GetSerializedStreamAsync(string url)
  497. {
  498. if (url.IndexOf('?') == -1)
  499. {
  500. url += "?dataformat=" + SerializationFormat.ToString().ToLower();
  501. }
  502. else
  503. {
  504. url += "&dataformat=" + SerializationFormat.ToString().ToLower();
  505. }
  506. return GetStreamAsync(url);
  507. }
  508. private T DeserializeFromStream<T>(Stream stream)
  509. {
  510. return DeserializeFromStream<T>(stream, SerializationFormat);
  511. }
  512. private T DeserializeFromStream<T>(Stream stream, SerializationFormat format)
  513. {
  514. if (format == ApiInteraction.SerializationFormat.Protobuf)
  515. {
  516. return DataSerializer.DeserializeProtobufFromStream<T>(stream);
  517. }
  518. if (format == ApiInteraction.SerializationFormat.Jsv)
  519. {
  520. return DataSerializer.DeserializeJsvFromStream<T>(stream);
  521. }
  522. return DataSerializer.DeserializeJsonFromStream<T>(stream);
  523. }
  524. /// <summary>
  525. /// This is just a helper around HttpClient
  526. /// </summary>
  527. private Task<Stream> GetStreamAsync(string url)
  528. {
  529. return HttpClient.GetStreamAsync(url);
  530. }
  531. public void Dispose()
  532. {
  533. HttpClient.Dispose();
  534. }
  535. }
  536. public enum SerializationFormat
  537. {
  538. Json,
  539. Jsv,
  540. Protobuf
  541. }
  542. }