ApiClient.cs 12 KB

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