ApiClient.cs 14 KB

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