ApiClient.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. /// <summary>
  37. /// Gets or sets the format to request from the server
  38. /// The Data Serializer will have to be able to support it.
  39. /// </summary>
  40. public SerializationFormat SerializationFormat { get; set; }
  41. public HttpClient HttpClient { get; private set; }
  42. public IDataSerializer DataSerializer { get; set; }
  43. /// <summary>
  44. /// Gets an image url that can be used to download an image from the api
  45. /// </summary>
  46. /// <param name="itemId">The Id of the item</param>
  47. /// <param name="imageType">The type of image requested</param>
  48. /// <param name="imageIndex">The image index, if there are multiple. Currently only applies to backdrops. Supply null or 0 for first backdrop.</param>
  49. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  50. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  51. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  52. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  53. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  54. 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)
  55. {
  56. string url = ApiUrl + "/image";
  57. url += "?id=" + itemId.ToString();
  58. url += "&type=" + imageType.ToString();
  59. if (imageIndex.HasValue)
  60. {
  61. url += "&index=" + imageIndex;
  62. }
  63. if (width.HasValue)
  64. {
  65. url += "&width=" + width;
  66. }
  67. if (height.HasValue)
  68. {
  69. url += "&height=" + height;
  70. }
  71. if (maxWidth.HasValue)
  72. {
  73. url += "&maxWidth=" + maxWidth;
  74. }
  75. if (maxHeight.HasValue)
  76. {
  77. url += "&maxHeight=" + maxHeight;
  78. }
  79. if (quality.HasValue)
  80. {
  81. url += "&quality=" + quality;
  82. }
  83. return url;
  84. }
  85. /// <summary>
  86. /// 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.
  87. /// </summary>
  88. /// <param name="item">A given item.</param>
  89. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  90. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  91. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  92. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  93. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  94. public IEnumerable<string> GetBackdropImageUrls(DTOBaseItem item, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  95. {
  96. Guid? backdropItemId = null;
  97. int backdropCount = 0;
  98. if (item.BackdropCount == 0)
  99. {
  100. backdropItemId = item.ParentBackdropItemId;
  101. backdropCount = item.ParentBackdropCount ?? 0;
  102. }
  103. else
  104. {
  105. backdropItemId = item.Id;
  106. backdropCount = item.BackdropCount;
  107. }
  108. if (backdropItemId == null)
  109. {
  110. return new string[] { };
  111. }
  112. List<string> files = new List<string>();
  113. for (int i = 0; i < backdropCount; i++)
  114. {
  115. files.Add(GetImageUrl(backdropItemId.Value, ImageType.Backdrop, i, width, height, maxWidth, maxHeight, quality));
  116. }
  117. return files;
  118. }
  119. /// <summary>
  120. /// 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.
  121. /// </summary>
  122. /// <param name="item">A given item.</param>
  123. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  124. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  125. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  126. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  127. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  128. public string GetLogoImageUrl(DTOBaseItem item, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  129. {
  130. Guid? logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId;
  131. if (logoItemId.HasValue)
  132. {
  133. return GetImageUrl(logoItemId.Value, ImageType.Logo, null, width, height, maxWidth, maxHeight, quality);
  134. }
  135. return null;
  136. }
  137. /// <summary>
  138. /// Gets an image stream based on a url
  139. /// </summary>
  140. public Task<Stream> GetImageStreamAsync(string url)
  141. {
  142. return GetStreamAsync(url);
  143. }
  144. /// <summary>
  145. /// Gets a BaseItem
  146. /// </summary>
  147. public async Task<DTOBaseItem> GetItemAsync(Guid id, Guid userId)
  148. {
  149. string url = ApiUrl + "/item?userId=" + userId.ToString();
  150. if (id != Guid.Empty)
  151. {
  152. url += "&id=" + id.ToString();
  153. }
  154. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  155. {
  156. return DataSerializer.DeserializeFromStream<DTOBaseItem>(stream);
  157. }
  158. }
  159. /// <summary>
  160. /// Gets all Users
  161. /// </summary>
  162. public async Task<IEnumerable<User>> GetAllUsersAsync()
  163. {
  164. string url = ApiUrl + "/users";
  165. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  166. {
  167. return DataSerializer.DeserializeFromStream<IEnumerable<User>>(stream);
  168. }
  169. }
  170. /// <summary>
  171. /// Gets all Genres
  172. /// </summary>
  173. public async Task<IEnumerable<IBNItem>> GetAllGenresAsync(Guid userId)
  174. {
  175. string url = ApiUrl + "/genres?userId=" + userId.ToString();
  176. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  177. {
  178. return DataSerializer.DeserializeFromStream<IEnumerable<IBNItem>>(stream);
  179. }
  180. }
  181. /// <summary>
  182. /// Gets all Years
  183. /// </summary>
  184. public async Task<IEnumerable<IBNItem>> GetAllYearsAsync(Guid userId)
  185. {
  186. string url = ApiUrl + "/years?userId=" + userId.ToString();
  187. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  188. {
  189. return DataSerializer.DeserializeFromStream<IEnumerable<IBNItem>>(stream);
  190. }
  191. }
  192. /// <summary>
  193. /// Gets all items that contain a given Year
  194. /// </summary>
  195. public async Task<IEnumerable<DTOBaseItem>> GetItemsWithYearAsync(string name, Guid userId)
  196. {
  197. string url = ApiUrl + "/itemlist?listtype=itemswithyear&userId=" + userId.ToString() + "&name=" + name;
  198. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  199. {
  200. return DataSerializer.DeserializeFromStream<IEnumerable<DTOBaseItem>>(stream);
  201. }
  202. }
  203. /// <summary>
  204. /// Gets all items that contain a given Genre
  205. /// </summary>
  206. public async Task<IEnumerable<DTOBaseItem>> GetItemsWithGenreAsync(string name, Guid userId)
  207. {
  208. string url = ApiUrl + "/itemlist?listtype=itemswithgenre&userId=" + userId.ToString() + "&name=" + name;
  209. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  210. {
  211. return DataSerializer.DeserializeFromStream<IEnumerable<DTOBaseItem>>(stream);
  212. }
  213. }
  214. /// <summary>
  215. /// Gets all items that contain a given Person
  216. /// </summary>
  217. public async Task<IEnumerable<DTOBaseItem>> GetItemsWithPersonAsync(string name, Guid userId)
  218. {
  219. string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId.ToString() + "&name=" + name;
  220. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  221. {
  222. return DataSerializer.DeserializeFromStream<IEnumerable<DTOBaseItem>>(stream);
  223. }
  224. }
  225. /// <summary>
  226. /// Gets all items that contain a given Person
  227. /// </summary>
  228. public async Task<IEnumerable<DTOBaseItem>> GetItemsWithPersonAsync(string name, string personType, Guid userId)
  229. {
  230. string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId.ToString() + "&name=" + name;
  231. url += "&persontype=" + personType;
  232. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  233. {
  234. return DataSerializer.DeserializeFromStream<IEnumerable<DTOBaseItem>>(stream);
  235. }
  236. }
  237. /// <summary>
  238. /// Gets all studious
  239. /// </summary>
  240. public async Task<IEnumerable<IBNItem>> GetAllStudiosAsync(Guid userId)
  241. {
  242. string url = ApiUrl + "/studios?userId=" + userId.ToString();
  243. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  244. {
  245. return DataSerializer.DeserializeFromStream<IEnumerable<IBNItem>>(stream);
  246. }
  247. }
  248. /// <summary>
  249. /// Gets all items that contain a given Studio
  250. /// </summary>
  251. public async Task<IEnumerable<DTOBaseItem>> GetItemsWithStudioAsync(string name, Guid userId)
  252. {
  253. string url = ApiUrl + "/itemlist?listtype=itemswithstudio&userId=" + userId.ToString() + "&name=" + name;
  254. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  255. {
  256. return DataSerializer.DeserializeFromStream<IEnumerable<DTOBaseItem>>(stream);
  257. }
  258. }
  259. /// <summary>
  260. /// Gets a studio
  261. /// </summary>
  262. public async Task<IBNItem> GetStudioAsync(Guid userId, string name)
  263. {
  264. string url = ApiUrl + "/studio?userId=" + userId.ToString() + "&name=" + name;
  265. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  266. {
  267. return DataSerializer.DeserializeFromStream<IBNItem>(stream);
  268. }
  269. }
  270. /// <summary>
  271. /// Gets a genre
  272. /// </summary>
  273. public async Task<IBNItem> GetGenreAsync(Guid userId, string name)
  274. {
  275. string url = ApiUrl + "/genre?userId=" + userId.ToString() + "&name=" + name;
  276. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  277. {
  278. return DataSerializer.DeserializeFromStream<IBNItem>(stream);
  279. }
  280. }
  281. /// <summary>
  282. /// Gets a person
  283. /// </summary>
  284. public async Task<IBNItem> GetPersonAsync(Guid userId, string name)
  285. {
  286. string url = ApiUrl + "/person?userId=" + userId.ToString() + "&name=" + name;
  287. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  288. {
  289. return DataSerializer.DeserializeFromStream<IBNItem>(stream);
  290. }
  291. }
  292. /// <summary>
  293. /// Gets a year
  294. /// </summary>
  295. public async Task<IBNItem> GetYearAsync(Guid userId, int year)
  296. {
  297. string url = ApiUrl + "/year?userId=" + userId.ToString() + "&year=" + year;
  298. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  299. {
  300. return DataSerializer.DeserializeFromStream<IBNItem>(stream);
  301. }
  302. }
  303. /// <summary>
  304. /// This is a helper around getting a stream from the server that contains serialized data
  305. /// </summary>
  306. private Task<Stream> GetSerializedStreamAsync(string url)
  307. {
  308. if (url.IndexOf('?') == -1)
  309. {
  310. url += "?dataformat=" + SerializationFormat.ToString().ToLower();
  311. }
  312. else
  313. {
  314. url += "&dataformat=" + SerializationFormat.ToString().ToLower();
  315. }
  316. return GetStreamAsync(url);
  317. }
  318. /// <summary>
  319. /// This is just a helper around HttpClient
  320. /// </summary>
  321. private Task<Stream> GetStreamAsync(string url)
  322. {
  323. return HttpClient.GetStreamAsync(url);
  324. }
  325. public void Dispose()
  326. {
  327. HttpClient.Dispose();
  328. }
  329. }
  330. public enum SerializationFormat
  331. {
  332. Json,
  333. Jsv
  334. }
  335. }