ApiClient.cs 23 KB

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