BaseApiClient.cs 15 KB

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