2
0

ApiClient.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net.Http;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Model.Configuration;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Users;
  9. namespace MediaBrowser.ApiInteraction
  10. {
  11. public class ApiClient : BaseClient
  12. {
  13. public IJsonSerializer JsonSerializer { get; set; }
  14. public ApiClient()
  15. : base()
  16. {
  17. }
  18. public ApiClient(HttpClientHandler handler)
  19. : base(handler)
  20. {
  21. }
  22. /// <summary>
  23. /// Gets an image url that can be used to download an image from the api
  24. /// </summary>
  25. /// <param name="itemId">The Id of the item</param>
  26. /// <param name="imageType">The type of image requested</param>
  27. /// <param name="imageIndex">The image index, if there are multiple. Currently only applies to backdrops. Supply null or 0 for first backdrop.</param>
  28. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  29. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  30. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  31. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  32. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  33. public string GetImageUrl(Guid itemId, ImageType imageType, int? imageIndex, int? width, int? height, int? maxWidth, int? maxHeight, int? quality)
  34. {
  35. string url = ApiUrl + "/image";
  36. url += "?id=" + itemId.ToString();
  37. url += "&type=" + imageType.ToString();
  38. if (imageIndex.HasValue)
  39. {
  40. url += "&index=" + imageIndex;
  41. }
  42. if (width.HasValue)
  43. {
  44. url += "&width=" + width;
  45. }
  46. if (height.HasValue)
  47. {
  48. url += "&height=" + height;
  49. }
  50. if (maxWidth.HasValue)
  51. {
  52. url += "&maxWidth=" + maxWidth;
  53. }
  54. if (maxHeight.HasValue)
  55. {
  56. url += "&maxHeight=" + maxHeight;
  57. }
  58. if (quality.HasValue)
  59. {
  60. url += "&quality=" + quality;
  61. }
  62. return url;
  63. }
  64. /// <summary>
  65. /// Gets an image stream based on a url
  66. /// </summary>
  67. public async Task<Stream> GetImageStreamAsync(string url)
  68. {
  69. return await HttpClient.GetStreamAsync(url);
  70. }
  71. /// <summary>
  72. /// Gets a BaseItem
  73. /// </summary>
  74. public async Task<ApiBaseItemWrapper<ApiBaseItem>> GetItemAsync(Guid id, Guid userId)
  75. {
  76. string url = ApiUrl + "/item?userId=" + userId.ToString();
  77. if (id != Guid.Empty)
  78. {
  79. url += "&id=" + id.ToString();
  80. }
  81. using (Stream stream = await HttpClient.GetStreamAsync(url))
  82. {
  83. return JsonSerializer.DeserializeFromStream<ApiBaseItemWrapper<ApiBaseItem>>(stream);
  84. }
  85. }
  86. /// <summary>
  87. /// Gets all Users
  88. /// </summary>
  89. public async Task<IEnumerable<User>> GetAllUsersAsync()
  90. {
  91. string url = ApiUrl + "/users";
  92. using (Stream stream = await HttpClient.GetStreamAsync(url))
  93. {
  94. return JsonSerializer.DeserializeFromStream<IEnumerable<User>>(stream);
  95. }
  96. }
  97. /// <summary>
  98. /// Gets all Genres
  99. /// </summary>
  100. public async Task<IEnumerable<CategoryInfo<Genre>>> GetAllGenresAsync(Guid userId)
  101. {
  102. string url = ApiUrl + "/genres?userId=" + userId.ToString();
  103. using (Stream stream = await HttpClient.GetStreamAsync(url))
  104. {
  105. return JsonSerializer.DeserializeFromStream<IEnumerable<CategoryInfo<Genre>>>(stream);
  106. }
  107. }
  108. /// <summary>
  109. /// Gets a Genre
  110. /// </summary>
  111. public async Task<CategoryInfo<Genre>> GetGenreAsync(string name, Guid userId)
  112. {
  113. string url = ApiUrl + "/genre?userId=" + userId.ToString() + "&name=" + name;
  114. using (Stream stream = await HttpClient.GetStreamAsync(url))
  115. {
  116. return JsonSerializer.DeserializeFromStream<CategoryInfo<Genre>>(stream);
  117. }
  118. }
  119. /// <summary>
  120. /// Gets all studious
  121. /// </summary>
  122. public async Task<IEnumerable<CategoryInfo<Studio>>> GetAllStudiosAsync(Guid userId)
  123. {
  124. string url = ApiUrl + "/studios?userId=" + userId.ToString();
  125. using (Stream stream = await HttpClient.GetStreamAsync(url))
  126. {
  127. return JsonSerializer.DeserializeFromStream<IEnumerable<CategoryInfo<Studio>>>(stream);
  128. }
  129. }
  130. /// <summary>
  131. /// Gets the current personalized configuration
  132. /// </summary>
  133. public async Task<UserConfiguration> GetUserConfigurationAsync(Guid userId)
  134. {
  135. string url = ApiUrl + "/userconfiguration?userId=" + userId.ToString();
  136. using (Stream stream = await HttpClient.GetStreamAsync(url))
  137. {
  138. return JsonSerializer.DeserializeFromStream<UserConfiguration>(stream);
  139. }
  140. }
  141. /// <summary>
  142. /// Gets a Studio
  143. /// </summary>
  144. public async Task<CategoryInfo<Studio>> GetStudioAsync(string name, Guid userId)
  145. {
  146. string url = ApiUrl + "/studio?userId=" + userId.ToString() + "&name=" + name;
  147. using (Stream stream = await HttpClient.GetStreamAsync(url))
  148. {
  149. return JsonSerializer.DeserializeFromStream<CategoryInfo<Studio>>(stream);
  150. }
  151. }
  152. }
  153. }