BaseApiClient.cs 15 KB

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