ApiClient.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net.Http;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Model.Configuration;
  8. using MediaBrowser.Model.Entities;
  9. using MediaBrowser.Model.Users;
  10. namespace MediaBrowser.ApiInteraction
  11. {
  12. public class ApiClient : BaseClient
  13. {
  14. public IJsonSerializer JsonSerializer { get; set; }
  15. public ApiClient()
  16. : base()
  17. {
  18. }
  19. public ApiClient(HttpClientHandler handler)
  20. : base(handler)
  21. {
  22. }
  23. /// <summary>
  24. /// Gets an image url that can be used to download an image from the api
  25. /// </summary>
  26. /// <param name="itemId">The Id of the item</param>
  27. /// <param name="imageType">The type of image requested</param>
  28. /// <param name="imageIndex">The image index, if there are multiple. Currently only applies to backdrops. Supply null or 0 for first backdrop.</param>
  29. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  30. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  31. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  32. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  33. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  34. 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)
  35. {
  36. string url = ApiUrl + "/image";
  37. url += "?id=" + itemId.ToString();
  38. url += "&type=" + imageType.ToString();
  39. if (imageIndex.HasValue)
  40. {
  41. url += "&index=" + imageIndex;
  42. }
  43. if (width.HasValue)
  44. {
  45. url += "&width=" + width;
  46. }
  47. if (height.HasValue)
  48. {
  49. url += "&height=" + height;
  50. }
  51. if (maxWidth.HasValue)
  52. {
  53. url += "&maxWidth=" + maxWidth;
  54. }
  55. if (maxHeight.HasValue)
  56. {
  57. url += "&maxHeight=" + maxHeight;
  58. }
  59. if (quality.HasValue)
  60. {
  61. url += "&quality=" + quality;
  62. }
  63. return url;
  64. }
  65. /// <summary>
  66. /// 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.
  67. /// </summary>
  68. /// <param name="itemWrapper">A given item.</param>
  69. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  70. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  71. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  72. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  73. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  74. public IEnumerable<string> GetBackdropImageUrls(ApiBaseItemWrapper<ApiBaseItem> itemWrapper, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  75. {
  76. Guid? backdropItemId = null;
  77. int backdropCount = 0;
  78. if (itemWrapper.Item.BackdropImagePaths == null || !itemWrapper.Item.BackdropImagePaths.Any())
  79. {
  80. backdropItemId = itemWrapper.ParentBackdropItemId;
  81. backdropCount = itemWrapper.ParentBackdropCount ?? 0;
  82. }
  83. else
  84. {
  85. backdropItemId = itemWrapper.Item.Id;
  86. backdropCount = itemWrapper.Item.BackdropImagePaths.Count();
  87. }
  88. if (backdropItemId == null)
  89. {
  90. return new string[] { };
  91. }
  92. List<string> files = new List<string>();
  93. for (int i = 0; i < backdropCount; i++)
  94. {
  95. files.Add(GetImageUrl(backdropItemId.Value, ImageType.Backdrop, i, width, height, maxWidth, maxHeight, quality));
  96. }
  97. return files;
  98. }
  99. /// <summary>
  100. /// 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.
  101. /// </summary>
  102. /// <param name="itemWrapper">A given item.</param>
  103. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  104. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  105. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  106. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  107. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  108. public string GetLogoImageUrl(ApiBaseItemWrapper<ApiBaseItem> itemWrapper, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  109. {
  110. Guid? logoItemId = !string.IsNullOrEmpty(itemWrapper.Item.LogoImagePath) ? itemWrapper.Item.Id : itemWrapper.ParentLogoItemId;
  111. if (logoItemId.HasValue)
  112. {
  113. return GetImageUrl(logoItemId.Value, ImageType.Logo, null, width, height, maxWidth, maxHeight, quality);
  114. }
  115. return null;
  116. }
  117. /// <summary>
  118. /// Gets an image stream based on a url
  119. /// </summary>
  120. public async Task<Stream> GetImageStreamAsync(string url)
  121. {
  122. return await HttpClient.GetStreamAsync(url);
  123. }
  124. /// <summary>
  125. /// Gets a BaseItem
  126. /// </summary>
  127. public async Task<ApiBaseItemWrapper<ApiBaseItem>> GetItemAsync(Guid id, Guid userId)
  128. {
  129. string url = ApiUrl + "/item?userId=" + userId.ToString();
  130. if (id != Guid.Empty)
  131. {
  132. url += "&id=" + id.ToString();
  133. }
  134. using (Stream stream = await HttpClient.GetStreamAsync(url))
  135. {
  136. return JsonSerializer.DeserializeFromStream<ApiBaseItemWrapper<ApiBaseItem>>(stream);
  137. }
  138. }
  139. /// <summary>
  140. /// Gets all Users
  141. /// </summary>
  142. public async Task<IEnumerable<User>> GetAllUsersAsync()
  143. {
  144. string url = ApiUrl + "/users";
  145. using (Stream stream = await HttpClient.GetStreamAsync(url))
  146. {
  147. return JsonSerializer.DeserializeFromStream<IEnumerable<User>>(stream);
  148. }
  149. }
  150. /// <summary>
  151. /// Gets all Genres
  152. /// </summary>
  153. public async Task<IEnumerable<CategoryInfo<Genre>>> GetAllGenresAsync(Guid userId)
  154. {
  155. string url = ApiUrl + "/genres?userId=" + userId.ToString();
  156. using (Stream stream = await HttpClient.GetStreamAsync(url))
  157. {
  158. return JsonSerializer.DeserializeFromStream<IEnumerable<CategoryInfo<Genre>>>(stream);
  159. }
  160. }
  161. /// <summary>
  162. /// Gets a Genre
  163. /// </summary>
  164. public async Task<CategoryInfo<Genre>> GetGenreAsync(string name, Guid userId)
  165. {
  166. string url = ApiUrl + "/genre?userId=" + userId.ToString() + "&name=" + name;
  167. using (Stream stream = await HttpClient.GetStreamAsync(url))
  168. {
  169. return JsonSerializer.DeserializeFromStream<CategoryInfo<Genre>>(stream);
  170. }
  171. }
  172. /// <summary>
  173. /// Gets all studious
  174. /// </summary>
  175. public async Task<IEnumerable<CategoryInfo<Studio>>> GetAllStudiosAsync(Guid userId)
  176. {
  177. string url = ApiUrl + "/studios?userId=" + userId.ToString();
  178. using (Stream stream = await HttpClient.GetStreamAsync(url))
  179. {
  180. return JsonSerializer.DeserializeFromStream<IEnumerable<CategoryInfo<Studio>>>(stream);
  181. }
  182. }
  183. /// <summary>
  184. /// Gets the current personalized configuration
  185. /// </summary>
  186. public async Task<UserConfiguration> GetUserConfigurationAsync(Guid userId)
  187. {
  188. string url = ApiUrl + "/userconfiguration?userId=" + userId.ToString();
  189. using (Stream stream = await HttpClient.GetStreamAsync(url))
  190. {
  191. return JsonSerializer.DeserializeFromStream<UserConfiguration>(stream);
  192. }
  193. }
  194. /// <summary>
  195. /// Gets a Studio
  196. /// </summary>
  197. public async Task<CategoryInfo<Studio>> GetStudioAsync(string name, Guid userId)
  198. {
  199. string url = ApiUrl + "/studio?userId=" + userId.ToString() + "&name=" + name;
  200. using (Stream stream = await HttpClient.GetStreamAsync(url))
  201. {
  202. return JsonSerializer.DeserializeFromStream<CategoryInfo<Studio>>(stream);
  203. }
  204. }
  205. }
  206. }