BaseApiClient.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. using MediaBrowser.Model.DTO;
  2. using MediaBrowser.Model.Entities;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. namespace MediaBrowser.ApiInteraction
  8. {
  9. /// <summary>
  10. /// Provides api methods that are usable on all platforms
  11. /// </summary>
  12. public abstract class BaseApiClient : IDisposable
  13. {
  14. protected BaseApiClient()
  15. {
  16. DataSerializer.Configure();
  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 the default data format to request from the server
  38. /// </summary>
  39. protected SerializationFormats SerializationFormat
  40. {
  41. get
  42. {
  43. return SerializationFormats.Protobuf;
  44. }
  45. }
  46. /// <summary>
  47. /// Gets an image url that can be used to download an image from the api
  48. /// </summary>
  49. /// <param name="itemId">The Id of the item</param>
  50. /// <param name="imageType">The type of image requested</param>
  51. /// <param name="imageIndex">The image index, if there are multiple. Currently only applies to backdrops. Supply null or 0 for first backdrop.</param>
  52. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  53. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  54. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  55. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  56. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  57. 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)
  58. {
  59. string url = ApiUrl + "/image";
  60. url += "?id=" + itemId.ToString();
  61. url += "&type=" + imageType.ToString();
  62. if (imageIndex.HasValue)
  63. {
  64. url += "&index=" + imageIndex;
  65. }
  66. if (width.HasValue)
  67. {
  68. url += "&width=" + width;
  69. }
  70. if (height.HasValue)
  71. {
  72. url += "&height=" + height;
  73. }
  74. if (maxWidth.HasValue)
  75. {
  76. url += "&maxWidth=" + maxWidth;
  77. }
  78. if (maxHeight.HasValue)
  79. {
  80. url += "&maxHeight=" + maxHeight;
  81. }
  82. if (quality.HasValue)
  83. {
  84. url += "&quality=" + quality;
  85. }
  86. return url;
  87. }
  88. /// <summary>
  89. /// Gets an image url that can be used to download an image from the api
  90. /// </summary>
  91. /// <param name="userId">The Id of the user</param>
  92. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  93. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  94. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  95. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  96. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  97. public string GetUserImageUrl(Guid userId, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  98. {
  99. string url = ApiUrl + "/image";
  100. url += "?userId=" + userId.ToString();
  101. if (width.HasValue)
  102. {
  103. url += "&width=" + width;
  104. }
  105. if (height.HasValue)
  106. {
  107. url += "&height=" + height;
  108. }
  109. if (maxWidth.HasValue)
  110. {
  111. url += "&maxWidth=" + maxWidth;
  112. }
  113. if (maxHeight.HasValue)
  114. {
  115. url += "&maxHeight=" + maxHeight;
  116. }
  117. if (quality.HasValue)
  118. {
  119. url += "&quality=" + quality;
  120. }
  121. return url;
  122. }
  123. /// <summary>
  124. /// Gets an image url that can be used to download an image from the api
  125. /// </summary>
  126. /// <param name="name">The name of the person</param>
  127. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  128. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  129. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  130. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  131. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  132. public string GetPersonImageUrl(string name, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  133. {
  134. string url = ApiUrl + "/image";
  135. url += "?personname=" + name;
  136. if (width.HasValue)
  137. {
  138. url += "&width=" + width;
  139. }
  140. if (height.HasValue)
  141. {
  142. url += "&height=" + height;
  143. }
  144. if (maxWidth.HasValue)
  145. {
  146. url += "&maxWidth=" + maxWidth;
  147. }
  148. if (maxHeight.HasValue)
  149. {
  150. url += "&maxHeight=" + maxHeight;
  151. }
  152. if (quality.HasValue)
  153. {
  154. url += "&quality=" + quality;
  155. }
  156. return url;
  157. }
  158. /// <summary>
  159. /// Gets an image url that can be used to download an image from the api
  160. /// </summary>
  161. /// <param name="year">The year</param>
  162. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  163. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  164. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  165. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  166. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  167. public string GetYearImageUrl(int year, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  168. {
  169. string url = ApiUrl + "/image";
  170. url += "?year=" + year;
  171. if (width.HasValue)
  172. {
  173. url += "&width=" + width;
  174. }
  175. if (height.HasValue)
  176. {
  177. url += "&height=" + height;
  178. }
  179. if (maxWidth.HasValue)
  180. {
  181. url += "&maxWidth=" + maxWidth;
  182. }
  183. if (maxHeight.HasValue)
  184. {
  185. url += "&maxHeight=" + maxHeight;
  186. }
  187. if (quality.HasValue)
  188. {
  189. url += "&quality=" + quality;
  190. }
  191. return url;
  192. }
  193. /// <summary>
  194. /// Gets an image url that can be used to download an image from the api
  195. /// </summary>
  196. /// <param name="name">The name of the genre</param>
  197. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  198. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  199. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  200. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  201. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  202. public string GetGenreImageUrl(string name, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  203. {
  204. string url = ApiUrl + "/image";
  205. url += "?genre=" + name;
  206. if (width.HasValue)
  207. {
  208. url += "&width=" + width;
  209. }
  210. if (height.HasValue)
  211. {
  212. url += "&height=" + height;
  213. }
  214. if (maxWidth.HasValue)
  215. {
  216. url += "&maxWidth=" + maxWidth;
  217. }
  218. if (maxHeight.HasValue)
  219. {
  220. url += "&maxHeight=" + maxHeight;
  221. }
  222. if (quality.HasValue)
  223. {
  224. url += "&quality=" + quality;
  225. }
  226. return url;
  227. }
  228. /// <summary>
  229. /// Gets an image url that can be used to download an image from the api
  230. /// </summary>
  231. /// <param name="name">The name of the studio</param>
  232. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  233. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  234. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  235. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  236. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  237. public string GetStudioImageUrl(string name, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  238. {
  239. string url = ApiUrl + "/image";
  240. url += "?studio=" + name;
  241. if (width.HasValue)
  242. {
  243. url += "&width=" + width;
  244. }
  245. if (height.HasValue)
  246. {
  247. url += "&height=" + height;
  248. }
  249. if (maxWidth.HasValue)
  250. {
  251. url += "&maxWidth=" + maxWidth;
  252. }
  253. if (maxHeight.HasValue)
  254. {
  255. url += "&maxHeight=" + maxHeight;
  256. }
  257. if (quality.HasValue)
  258. {
  259. url += "&quality=" + quality;
  260. }
  261. return url;
  262. }
  263. /// <summary>
  264. /// 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.
  265. /// </summary>
  266. /// <param name="item">A given item.</param>
  267. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  268. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  269. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  270. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  271. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  272. public string[] GetBackdropImageUrls(DtoBaseItem item, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  273. {
  274. Guid? backdropItemId;
  275. int backdropCount;
  276. if (item.BackdropCount == 0)
  277. {
  278. backdropItemId = item.ParentBackdropItemId;
  279. backdropCount = item.ParentBackdropCount ?? 0;
  280. }
  281. else
  282. {
  283. backdropItemId = item.Id;
  284. backdropCount = item.BackdropCount;
  285. }
  286. if (backdropItemId == null)
  287. {
  288. return new string[] { };
  289. }
  290. var files = new string[backdropCount];
  291. for (int i = 0; i < backdropCount; i++)
  292. {
  293. files[i] = GetImageUrl(backdropItemId.Value, ImageType.Backdrop, i, width, height, maxWidth, maxHeight, quality);
  294. }
  295. return files;
  296. }
  297. /// <summary>
  298. /// 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.
  299. /// </summary>
  300. /// <param name="item">A given item.</param>
  301. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  302. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  303. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  304. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  305. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  306. public string GetLogoImageUrl(DtoBaseItem item, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  307. {
  308. Guid? logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId;
  309. if (logoItemId.HasValue)
  310. {
  311. return GetImageUrl(logoItemId.Value, ImageType.Logo, null, width, height, maxWidth, maxHeight, quality);
  312. }
  313. return null;
  314. }
  315. /// <summary>
  316. /// Gets the url needed to stream an audio file
  317. /// </summary>
  318. /// <param name="itemId">The id of the item</param>
  319. /// <param name="supportedOutputFormats">List all the output formats the decice is capable of playing. The more, the better, as it will decrease the likelyhood of having to encode, which will put a load on the server.</param>
  320. /// <param name="maxAudioChannels">The maximum number of channels that the device can play. Omit this if it doesn't matter. Phones and tablets should generally specify 2.</param>
  321. /// <param name="maxAudioSampleRate">The maximum sample rate that the device can play. This should generally be omitted. The server will default this to 44100, so only override if a different max is needed.</param>
  322. public string GetAudioStreamUrl(Guid itemId, IEnumerable<AudioOutputFormats> supportedOutputFormats, int? maxAudioChannels = null, int? maxAudioSampleRate = null)
  323. {
  324. string url = ApiUrl + "/audio?id=" + itemId;
  325. url += "&outputformats=" + string.Join(",", supportedOutputFormats.Select(s => s.ToString()).ToArray());
  326. if (maxAudioChannels.HasValue)
  327. {
  328. url += "&audiochannels=" + maxAudioChannels.Value;
  329. }
  330. if (maxAudioSampleRate.HasValue)
  331. {
  332. url += "&audiosamplerate=" + maxAudioSampleRate.Value;
  333. }
  334. return url;
  335. }
  336. /// <summary>
  337. /// Gets the url needed to stream a video file
  338. /// </summary>
  339. /// <param name="itemId">The id of the item</param>
  340. /// <param name="supportedOutputFormats">List all the output formats the decice is capable of playing. The more, the better, as it will decrease the likelyhood of having to encode, which will put a load on the server.</param>
  341. /// <param name="maxAudioChannels">The maximum number of channels that the device can play. Omit this if it doesn't matter. Phones and tablets should generally specify 2.</param>
  342. /// <param name="maxAudioSampleRate">The maximum sample rate that the device can play. This should generally be omitted. The server will default this to 44100, so only override if a different max is needed.</param>
  343. /// <param name="width">Specify this is a fixed video width is required</param>
  344. /// <param name="height">Specify this is a fixed video height is required</param>
  345. /// <param name="maxWidth">Specify this is a max video width is required</param>
  346. /// <param name="maxHeight">Specify this is a max video height is required</param>
  347. public string GetVideoStreamUrl(Guid itemId,
  348. IEnumerable<VideoOutputFormats> supportedOutputFormats,
  349. int? maxAudioChannels = null,
  350. int? maxAudioSampleRate = null,
  351. int? width = null,
  352. int? height = null,
  353. int? maxWidth = null,
  354. int? maxHeight = null)
  355. {
  356. string url = ApiUrl + "/video?id=" + itemId;
  357. url += "&outputformats=" + string.Join(",", supportedOutputFormats.Select(s => s.ToString()).ToArray());
  358. if (maxAudioChannels.HasValue)
  359. {
  360. url += "&audiochannels=" + maxAudioChannels.Value;
  361. }
  362. if (maxAudioSampleRate.HasValue)
  363. {
  364. url += "&audiosamplerate=" + maxAudioSampleRate.Value;
  365. }
  366. if (width.HasValue)
  367. {
  368. url += "&width=" + width.Value;
  369. }
  370. if (height.HasValue)
  371. {
  372. url += "&height=" + height.Value;
  373. }
  374. if (maxWidth.HasValue)
  375. {
  376. url += "&maxWidth=" + maxWidth.Value;
  377. }
  378. if (maxHeight.HasValue)
  379. {
  380. url += "&maxHeight=" + maxHeight.Value;
  381. }
  382. return url;
  383. }
  384. protected T DeserializeFromStream<T>(Stream stream)
  385. where T : class
  386. {
  387. return DataSerializer.DeserializeFromStream<T>(stream, SerializationFormat);
  388. }
  389. public virtual void Dispose()
  390. {
  391. }
  392. }
  393. }