ApiClient.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Model.DTO;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Users;
  9. namespace MediaBrowser.ApiInteraction
  10. {
  11. public class ApiClient : IDisposable
  12. {
  13. /// <summary>
  14. /// Gets or sets the server host name (myserver or 192.168.x.x)
  15. /// </summary>
  16. public string ServerHostName { get; set; }
  17. /// <summary>
  18. /// Gets or sets the port number used by the API
  19. /// </summary>
  20. public int ServerApiPort { get; set; }
  21. /// <summary>
  22. /// Gets the current api url based on hostname and port.
  23. /// </summary>
  24. protected string ApiUrl
  25. {
  26. get
  27. {
  28. return string.Format("http://{0}:{1}/mediabrowser/api", ServerHostName, ServerApiPort);
  29. }
  30. }
  31. public IHttpClient HttpClient { get; set; }
  32. public IJsonSerializer JsonSerializer { get; set; }
  33. /// <summary>
  34. /// Gets an image url that can be used to download an image from the api
  35. /// </summary>
  36. /// <param name="itemId">The Id of the item</param>
  37. /// <param name="imageType">The type of image requested</param>
  38. /// <param name="imageIndex">The image index, if there are multiple. Currently only applies to backdrops. Supply null or 0 for first backdrop.</param>
  39. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  40. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  41. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  42. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  43. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  44. 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)
  45. {
  46. string url = ApiUrl + "/image";
  47. url += "?id=" + itemId.ToString();
  48. url += "&type=" + imageType.ToString();
  49. if (imageIndex.HasValue)
  50. {
  51. url += "&index=" + imageIndex;
  52. }
  53. if (width.HasValue)
  54. {
  55. url += "&width=" + width;
  56. }
  57. if (height.HasValue)
  58. {
  59. url += "&height=" + height;
  60. }
  61. if (maxWidth.HasValue)
  62. {
  63. url += "&maxWidth=" + maxWidth;
  64. }
  65. if (maxHeight.HasValue)
  66. {
  67. url += "&maxHeight=" + maxHeight;
  68. }
  69. if (quality.HasValue)
  70. {
  71. url += "&quality=" + quality;
  72. }
  73. return url;
  74. }
  75. /// <summary>
  76. /// 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.
  77. /// </summary>
  78. /// <param name="itemWrapper">A given item.</param>
  79. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  80. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  81. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  82. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  83. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  84. public IEnumerable<string> GetBackdropImageUrls(ApiBaseItemContainer itemWrapper, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  85. {
  86. Guid? backdropItemId = null;
  87. int backdropCount = 0;
  88. if (itemWrapper.Item.BackdropImagePaths == null || !itemWrapper.Item.BackdropImagePaths.Any())
  89. {
  90. backdropItemId = itemWrapper.ParentBackdropItemId;
  91. backdropCount = itemWrapper.ParentBackdropCount ?? 0;
  92. }
  93. else
  94. {
  95. backdropItemId = itemWrapper.Item.Id;
  96. backdropCount = itemWrapper.Item.BackdropImagePaths.Count();
  97. }
  98. if (backdropItemId == null)
  99. {
  100. return new string[] { };
  101. }
  102. List<string> files = new List<string>();
  103. for (int i = 0; i < backdropCount; i++)
  104. {
  105. files.Add(GetImageUrl(backdropItemId.Value, ImageType.Backdrop, i, width, height, maxWidth, maxHeight, quality));
  106. }
  107. return files;
  108. }
  109. /// <summary>
  110. /// 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.
  111. /// </summary>
  112. /// <param name="itemWrapper">A given item.</param>
  113. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  114. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  115. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  116. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  117. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  118. public string GetLogoImageUrl(ApiBaseItemContainer itemWrapper, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  119. {
  120. Guid? logoItemId = !string.IsNullOrEmpty(itemWrapper.Item.LogoImagePath) ? itemWrapper.Item.Id : itemWrapper.ParentLogoItemId;
  121. if (logoItemId.HasValue)
  122. {
  123. return GetImageUrl(logoItemId.Value, ImageType.Logo, null, width, height, maxWidth, maxHeight, quality);
  124. }
  125. return null;
  126. }
  127. /// <summary>
  128. /// Gets an image stream based on a url
  129. /// </summary>
  130. public async Task<Stream> GetImageStreamAsync(string url)
  131. {
  132. return await HttpClient.GetStreamAsync(url);
  133. }
  134. /// <summary>
  135. /// Gets a BaseItem
  136. /// </summary>
  137. public async Task<ApiBaseItemContainer> GetItemAsync(Guid id, Guid userId)
  138. {
  139. string url = ApiUrl + "/item?userId=" + userId.ToString();
  140. if (id != Guid.Empty)
  141. {
  142. url += "&id=" + id.ToString();
  143. }
  144. using (Stream stream = await HttpClient.GetStreamAsync(url))
  145. {
  146. return JsonSerializer.DeserializeFromStream<ApiBaseItemContainer>(stream);
  147. }
  148. }
  149. /// <summary>
  150. /// Gets all Users
  151. /// </summary>
  152. public async Task<IEnumerable<User>> GetAllUsersAsync()
  153. {
  154. string url = ApiUrl + "/users";
  155. using (Stream stream = await HttpClient.GetStreamAsync(url))
  156. {
  157. return JsonSerializer.DeserializeFromStream<IEnumerable<User>>(stream);
  158. }
  159. }
  160. /// <summary>
  161. /// Gets all Genres
  162. /// </summary>
  163. public async Task<IEnumerable<IBNItem<Genre>>> GetAllGenresAsync(Guid userId)
  164. {
  165. string url = ApiUrl + "/genres?userId=" + userId.ToString();
  166. using (Stream stream = await HttpClient.GetStreamAsync(url))
  167. {
  168. return JsonSerializer.DeserializeFromStream<IEnumerable<IBNItem<Genre>>>(stream);
  169. }
  170. }
  171. /// <summary>
  172. /// Gets all Years
  173. /// </summary>
  174. public async Task<IEnumerable<IBNItem<Year>>> GetAllYearsAsync(Guid userId)
  175. {
  176. string url = ApiUrl + "/years?userId=" + userId.ToString();
  177. using (Stream stream = await HttpClient.GetStreamAsync(url))
  178. {
  179. return JsonSerializer.DeserializeFromStream<IEnumerable<IBNItem<Year>>>(stream);
  180. }
  181. }
  182. /// <summary>
  183. /// Gets all items that contain a given Year
  184. /// </summary>
  185. public async Task<IEnumerable<ApiBaseItemContainer>> GetItemsWithYearAsync(string name, Guid userId)
  186. {
  187. string url = ApiUrl + "/itemlist?listtype=itemswithyear&userId=" + userId.ToString() + "&name=" + name;
  188. using (Stream stream = await HttpClient.GetStreamAsync(url))
  189. {
  190. return JsonSerializer.DeserializeFromStream<IEnumerable<ApiBaseItemContainer>>(stream);
  191. }
  192. }
  193. /// <summary>
  194. /// Gets all items that contain a given Genre
  195. /// </summary>
  196. public async Task<IEnumerable<ApiBaseItemContainer>> GetItemsWithGenreAsync(string name, Guid userId)
  197. {
  198. string url = ApiUrl + "/itemlist?listtype=itemswithgenre&userId=" + userId.ToString() + "&name=" + name;
  199. using (Stream stream = await HttpClient.GetStreamAsync(url))
  200. {
  201. return JsonSerializer.DeserializeFromStream<IEnumerable<ApiBaseItemContainer>>(stream);
  202. }
  203. }
  204. /// <summary>
  205. /// Gets all items that contain a given Person
  206. /// </summary>
  207. public async Task<IEnumerable<ApiBaseItemContainer>> GetItemsWithPersonAsync(string name, PersonType? personType, Guid userId)
  208. {
  209. string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId.ToString() + "&name=" + name;
  210. if (personType.HasValue)
  211. {
  212. url += "&persontype=" + personType.Value.ToString();
  213. }
  214. using (Stream stream = await HttpClient.GetStreamAsync(url))
  215. {
  216. return JsonSerializer.DeserializeFromStream<IEnumerable<ApiBaseItemContainer>>(stream);
  217. }
  218. }
  219. /// <summary>
  220. /// Gets all studious
  221. /// </summary>
  222. public async Task<IEnumerable<IBNItem<Studio>>> GetAllStudiosAsync(Guid userId)
  223. {
  224. string url = ApiUrl + "/studios?userId=" + userId.ToString();
  225. using (Stream stream = await HttpClient.GetStreamAsync(url))
  226. {
  227. return JsonSerializer.DeserializeFromStream<IEnumerable<IBNItem<Studio>>>(stream);
  228. }
  229. }
  230. /// <summary>
  231. /// Gets all items that contain a given Studio
  232. /// </summary>
  233. public async Task<IEnumerable<ApiBaseItemContainer>> GetItemsWithStudioAsync(string name, Guid userId)
  234. {
  235. string url = ApiUrl + "/itemlist?listtype=itemswithstudio&userId=" + userId.ToString() + "&name=" + name;
  236. using (Stream stream = await HttpClient.GetStreamAsync(url))
  237. {
  238. return JsonSerializer.DeserializeFromStream<IEnumerable<ApiBaseItemContainer>>(stream);
  239. }
  240. }
  241. public void Dispose()
  242. {
  243. HttpClient.Dispose();
  244. }
  245. }
  246. }