ApiClient.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Compression;
  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. /// <summary>
  20. /// Gets an image url that can be used to download an image from the api
  21. /// </summary>
  22. /// <param name="itemId">The Id of the item</param>
  23. /// <param name="imageType">The type of image requested</param>
  24. /// <param name="imageIndex">The image index, if there are multiple. Currently only applies to backdrops. Supply null or 0 for first backdrop.</param>
  25. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  26. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  27. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  28. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  29. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  30. public string GetImageUrl(Guid itemId, ImageType imageType, int? imageIndex, int? width, int? height, int? maxWidth, int? maxHeight, int? quality)
  31. {
  32. string url = ApiUrl + "/image";
  33. url += "?id=" + itemId.ToString();
  34. url += "&type=" + imageType.ToString();
  35. if (imageIndex.HasValue)
  36. {
  37. url += "&index=" + imageIndex;
  38. }
  39. if (width.HasValue)
  40. {
  41. url += "&width=" + width;
  42. }
  43. if (height.HasValue)
  44. {
  45. url += "&height=" + height;
  46. }
  47. if (maxWidth.HasValue)
  48. {
  49. url += "&maxWidth=" + maxWidth;
  50. }
  51. if (maxHeight.HasValue)
  52. {
  53. url += "&maxHeight=" + maxHeight;
  54. }
  55. if (quality.HasValue)
  56. {
  57. url += "&quality=" + quality;
  58. }
  59. return url;
  60. }
  61. /// <summary>
  62. /// Gets an image stream based on a url
  63. /// </summary>
  64. public async Task<Stream> GetImageStreamAsync(string url)
  65. {
  66. Stream stream = await HttpClient.GetStreamAsync(url);
  67. // For now this assumes the response stream is compressed. We can always improve this later.
  68. return new GZipStream(stream, CompressionMode.Decompress, false);
  69. }
  70. /// <summary>
  71. /// Gets a BaseItem
  72. /// </summary>
  73. public async Task<ApiBaseItemWrapper<ApiBaseItem>> GetItemAsync(Guid id, Guid userId)
  74. {
  75. string url = ApiUrl + "/item?userId=" + userId.ToString();
  76. if (id != Guid.Empty)
  77. {
  78. url += "&id=" + id.ToString();
  79. }
  80. using (Stream stream = await HttpClient.GetStreamAsync(url))
  81. {
  82. using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
  83. {
  84. return JsonSerializer.DeserializeFromStream<ApiBaseItemWrapper<ApiBaseItem>>(gzipStream);
  85. }
  86. }
  87. }
  88. /// <summary>
  89. /// Gets all Users
  90. /// </summary>
  91. public async Task<IEnumerable<User>> GetAllUsersAsync()
  92. {
  93. string url = ApiUrl + "/users";
  94. using (Stream stream = await HttpClient.GetStreamAsync(url))
  95. {
  96. using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
  97. {
  98. return JsonSerializer.DeserializeFromStream<IEnumerable<User>>(gzipStream);
  99. }
  100. }
  101. }
  102. /// <summary>
  103. /// Gets all Genres
  104. /// </summary>
  105. public async Task<IEnumerable<CategoryInfo<Genre>>> GetAllGenresAsync(Guid userId)
  106. {
  107. string url = ApiUrl + "/genres?userId=" + userId.ToString();
  108. using (Stream stream = await HttpClient.GetStreamAsync(url))
  109. {
  110. using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
  111. {
  112. return JsonSerializer.DeserializeFromStream<IEnumerable<CategoryInfo<Genre>>>(gzipStream);
  113. }
  114. }
  115. }
  116. /// <summary>
  117. /// Gets a Genre
  118. /// </summary>
  119. public async Task<CategoryInfo<Genre>> GetGenreAsync(string name, Guid userId)
  120. {
  121. string url = ApiUrl + "/genre?userId=" + userId.ToString() + "&name=" + name;
  122. using (Stream stream = await HttpClient.GetStreamAsync(url))
  123. {
  124. using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
  125. {
  126. return JsonSerializer.DeserializeFromStream<CategoryInfo<Genre>>(gzipStream);
  127. }
  128. }
  129. }
  130. /// <summary>
  131. /// Gets all studious
  132. /// </summary>
  133. public async Task<IEnumerable<CategoryInfo<Studio>>> GetAllStudiosAsync(Guid userId)
  134. {
  135. string url = ApiUrl + "/studios?userId=" + userId.ToString();
  136. using (Stream stream = await HttpClient.GetStreamAsync(url))
  137. {
  138. using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
  139. {
  140. return JsonSerializer.DeserializeFromStream<IEnumerable<CategoryInfo<Studio>>>(gzipStream);
  141. }
  142. }
  143. }
  144. /// <summary>
  145. /// Gets the current personalized configuration
  146. /// </summary>
  147. public async Task<UserConfiguration> GetUserConfigurationAsync(Guid userId)
  148. {
  149. string url = ApiUrl + "/userconfiguration?userId=" + userId.ToString();
  150. using (Stream stream = await HttpClient.GetStreamAsync(url))
  151. {
  152. using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
  153. {
  154. return JsonSerializer.DeserializeFromStream<UserConfiguration>(gzipStream);
  155. }
  156. }
  157. }
  158. /// <summary>
  159. /// Gets a Studio
  160. /// </summary>
  161. public async Task<CategoryInfo<Studio>> GetStudioAsync(string name, Guid userId)
  162. {
  163. string url = ApiUrl + "/studio?userId=" + userId.ToString() + "&name=" + name;
  164. using (Stream stream = await HttpClient.GetStreamAsync(url))
  165. {
  166. using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
  167. {
  168. return JsonSerializer.DeserializeFromStream<CategoryInfo<Studio>>(gzipStream);
  169. }
  170. }
  171. }
  172. }
  173. }