ApiClient.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. using MediaBrowser.Model.Configuration;
  2. using MediaBrowser.Model.DTO;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.Model.Weather;
  5. using System;
  6. using System.IO;
  7. using System.Net;
  8. using System.Net.Http;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace MediaBrowser.ApiInteraction
  12. {
  13. public class ApiClient : BaseApiClient
  14. {
  15. public ApiClient(HttpClientHandler handler)
  16. : base()
  17. {
  18. handler.AutomaticDecompression = DecompressionMethods.Deflate;
  19. HttpClient = new HttpClient(handler);
  20. }
  21. private HttpClient HttpClient { get; set; }
  22. /// <summary>
  23. /// Gets an image stream based on a url
  24. /// </summary>
  25. public Task<Stream> GetImageStreamAsync(string url)
  26. {
  27. return GetStreamAsync(url);
  28. }
  29. /// <summary>
  30. /// Gets a BaseItem
  31. /// </summary>
  32. public async Task<DTOBaseItem> GetItemAsync(Guid id, Guid userId)
  33. {
  34. string url = ApiUrl + "/item?userId=" + userId.ToString();
  35. if (id != Guid.Empty)
  36. {
  37. url += "&id=" + id.ToString();
  38. }
  39. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  40. {
  41. return DeserializeFromStream<DTOBaseItem>(stream);
  42. }
  43. }
  44. /// <summary>
  45. /// Gets all Users
  46. /// </summary>
  47. public async Task<DTOUser[]> GetAllUsersAsync()
  48. {
  49. string url = ApiUrl + "/users";
  50. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  51. {
  52. return DeserializeFromStream<DTOUser[]>(stream);
  53. }
  54. }
  55. /// <summary>
  56. /// Gets all Genres
  57. /// </summary>
  58. public async Task<IBNItem[]> GetAllGenresAsync(Guid userId)
  59. {
  60. string url = ApiUrl + "/genres?userId=" + userId.ToString();
  61. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  62. {
  63. return DeserializeFromStream<IBNItem[]>(stream);
  64. }
  65. }
  66. /// <summary>
  67. /// Gets in-progress items
  68. /// </summary>
  69. /// <param name="userId">The user id.</param>
  70. /// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
  71. public async Task<DTOBaseItem[]> GetInProgressItemsItemsAsync(Guid userId, Guid? folderId = null)
  72. {
  73. string url = ApiUrl + "/itemlist?listtype=inprogressitems&userId=" + userId.ToString();
  74. if (folderId.HasValue)
  75. {
  76. url += "&id=" + folderId.ToString();
  77. }
  78. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  79. {
  80. return DeserializeFromStream<DTOBaseItem[]>(stream);
  81. }
  82. }
  83. /// <summary>
  84. /// Gets recently added items
  85. /// </summary>
  86. /// <param name="userId">The user id.</param>
  87. /// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
  88. public async Task<DTOBaseItem[]> GetRecentlyAddedItemsAsync(Guid userId, Guid? folderId = null)
  89. {
  90. string url = ApiUrl + "/itemlist?listtype=recentlyaddeditems&userId=" + userId.ToString();
  91. if (folderId.HasValue)
  92. {
  93. url += "&id=" + folderId.ToString();
  94. }
  95. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  96. {
  97. return DeserializeFromStream<DTOBaseItem[]>(stream);
  98. }
  99. }
  100. /// <summary>
  101. /// Gets recently added items that are unplayed.
  102. /// </summary>
  103. /// <param name="userId">The user id.</param>
  104. /// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
  105. public async Task<DTOBaseItem[]> GetRecentlyAddedUnplayedItemsAsync(Guid userId, Guid? folderId = null)
  106. {
  107. string url = ApiUrl + "/itemlist?listtype=recentlyaddedunplayeditems&userId=" + userId.ToString();
  108. if (folderId.HasValue)
  109. {
  110. url += "&id=" + folderId.ToString();
  111. }
  112. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  113. {
  114. return DeserializeFromStream<DTOBaseItem[]>(stream);
  115. }
  116. }
  117. /// <summary>
  118. /// Gets all Years
  119. /// </summary>
  120. public async Task<IBNItem[]> GetAllYearsAsync(Guid userId)
  121. {
  122. string url = ApiUrl + "/years?userId=" + userId.ToString();
  123. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  124. {
  125. return DeserializeFromStream<IBNItem[]>(stream);
  126. }
  127. }
  128. /// <summary>
  129. /// Gets all items that contain a given Year
  130. /// </summary>
  131. public async Task<DTOBaseItem[]> GetItemsWithYearAsync(string name, Guid userId)
  132. {
  133. string url = ApiUrl + "/itemlist?listtype=itemswithyear&userId=" + userId.ToString() + "&name=" + name;
  134. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  135. {
  136. return DeserializeFromStream<DTOBaseItem[]>(stream);
  137. }
  138. }
  139. /// <summary>
  140. /// Gets all items that contain a given Genre
  141. /// </summary>
  142. public async Task<DTOBaseItem[]> GetItemsWithGenreAsync(string name, Guid userId)
  143. {
  144. string url = ApiUrl + "/itemlist?listtype=itemswithgenre&userId=" + userId.ToString() + "&name=" + name;
  145. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  146. {
  147. return DeserializeFromStream<DTOBaseItem[]>(stream);
  148. }
  149. }
  150. /// <summary>
  151. /// Gets all items that contain a given Person
  152. /// </summary>
  153. public async Task<DTOBaseItem[]> GetItemsWithPersonAsync(string name, Guid userId)
  154. {
  155. string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId.ToString() + "&name=" + name;
  156. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  157. {
  158. return DeserializeFromStream<DTOBaseItem[]>(stream);
  159. }
  160. }
  161. /// <summary>
  162. /// Gets all items that contain a given Person
  163. /// </summary>
  164. public async Task<DTOBaseItem[]> GetItemsWithPersonAsync(string name, string personType, Guid userId)
  165. {
  166. string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId.ToString() + "&name=" + name;
  167. url += "&persontype=" + personType;
  168. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  169. {
  170. return DeserializeFromStream<DTOBaseItem[]>(stream);
  171. }
  172. }
  173. /// <summary>
  174. /// Gets all studious
  175. /// </summary>
  176. public async Task<IBNItem[]> GetAllStudiosAsync(Guid userId)
  177. {
  178. string url = ApiUrl + "/studios?userId=" + userId.ToString();
  179. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  180. {
  181. return DeserializeFromStream<IBNItem[]>(stream);
  182. }
  183. }
  184. /// <summary>
  185. /// Gets all items that contain a given Studio
  186. /// </summary>
  187. public async Task<DTOBaseItem[]> GetItemsWithStudioAsync(string name, Guid userId)
  188. {
  189. string url = ApiUrl + "/itemlist?listtype=itemswithstudio&userId=" + userId.ToString() + "&name=" + name;
  190. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  191. {
  192. return DeserializeFromStream<DTOBaseItem[]>(stream);
  193. }
  194. }
  195. /// <summary>
  196. /// Gets a studio
  197. /// </summary>
  198. public async Task<IBNItem> GetStudioAsync(Guid userId, string name)
  199. {
  200. string url = ApiUrl + "/studio?userId=" + userId.ToString() + "&name=" + name;
  201. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  202. {
  203. return DeserializeFromStream<IBNItem>(stream);
  204. }
  205. }
  206. /// <summary>
  207. /// Gets a genre
  208. /// </summary>
  209. public async Task<IBNItem> GetGenreAsync(Guid userId, string name)
  210. {
  211. string url = ApiUrl + "/genre?userId=" + userId.ToString() + "&name=" + name;
  212. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  213. {
  214. return DeserializeFromStream<IBNItem>(stream);
  215. }
  216. }
  217. /// <summary>
  218. /// Gets a person
  219. /// </summary>
  220. public async Task<IBNItem> GetPersonAsync(Guid userId, string name)
  221. {
  222. string url = ApiUrl + "/person?userId=" + userId.ToString() + "&name=" + name;
  223. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  224. {
  225. return DeserializeFromStream<IBNItem>(stream);
  226. }
  227. }
  228. /// <summary>
  229. /// Gets a year
  230. /// </summary>
  231. public async Task<IBNItem> GetYearAsync(Guid userId, int year)
  232. {
  233. string url = ApiUrl + "/year?userId=" + userId.ToString() + "&year=" + year;
  234. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  235. {
  236. return DeserializeFromStream<IBNItem>(stream);
  237. }
  238. }
  239. /// <summary>
  240. /// Gets a list of plugins installed on the server
  241. /// </summary>
  242. public async Task<PluginInfo[]> GetInstalledPluginsAsync()
  243. {
  244. string url = ApiUrl + "/plugins";
  245. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  246. {
  247. return DeserializeFromStream<PluginInfo[]>(stream);
  248. }
  249. }
  250. /// <summary>
  251. /// Gets a list of plugins installed on the server
  252. /// </summary>
  253. public Task<Stream> GetPluginAssemblyAsync(PluginInfo plugin)
  254. {
  255. string url = ApiUrl + "/pluginassembly?assemblyfilename=" + plugin.AssemblyFileName;
  256. return GetStreamAsync(url);
  257. }
  258. /// <summary>
  259. /// Gets the current server configuration
  260. /// </summary>
  261. public async Task<ServerConfiguration> GetServerConfigurationAsync()
  262. {
  263. string url = ApiUrl + "/ServerConfiguration";
  264. // At the moment this can't be retrieved in protobuf format
  265. SerializationFormats format = DataSerializer.CanDeSerializeJsv ? SerializationFormats.Jsv : SerializationFormats.Json;
  266. using (Stream stream = await GetSerializedStreamAsync(url, format).ConfigureAwait(false))
  267. {
  268. return DataSerializer.DeserializeFromStream<ServerConfiguration>(stream, format);
  269. }
  270. }
  271. /// <summary>
  272. /// Gets weather information for the default location as set in configuration
  273. /// </summary>
  274. public async Task<object> GetPluginConfigurationAsync(PluginInfo plugin, Type configurationType)
  275. {
  276. string url = ApiUrl + "/PluginConfiguration?assemblyfilename=" + plugin.AssemblyFileName;
  277. // At the moment this can't be retrieved in protobuf format
  278. SerializationFormats format = DataSerializer.CanDeSerializeJsv ? SerializationFormats.Jsv : SerializationFormats.Json;
  279. using (Stream stream = await GetSerializedStreamAsync(url, format).ConfigureAwait(false))
  280. {
  281. return DataSerializer.DeserializeFromStream(stream, format, configurationType);
  282. }
  283. }
  284. /// <summary>
  285. /// Gets weather information for the default location as set in configuration
  286. /// </summary>
  287. public async Task<DTOUser> GetDefaultUserAsync()
  288. {
  289. string url = ApiUrl + "/defaultuser";
  290. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  291. {
  292. return DeserializeFromStream<DTOUser>(stream);
  293. }
  294. }
  295. /// <summary>
  296. /// Gets weather information for the default location as set in configuration
  297. /// </summary>
  298. public async Task<WeatherInfo> GetWeatherInfoAsync()
  299. {
  300. string url = ApiUrl + "/weather";
  301. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  302. {
  303. return DeserializeFromStream<WeatherInfo>(stream);
  304. }
  305. }
  306. /// <summary>
  307. /// Gets weather information for a specific zip code
  308. /// </summary>
  309. public async Task<WeatherInfo> GetWeatherInfoAsync(string zipCode)
  310. {
  311. string url = ApiUrl + "/weather?zipcode=" + zipCode;
  312. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  313. {
  314. return DeserializeFromStream<WeatherInfo>(stream);
  315. }
  316. }
  317. /// <summary>
  318. /// Authenticates a user and returns the result
  319. /// </summary>
  320. public async Task<AuthenticationResult> AuthenticateUserAsync(Guid userId, string password)
  321. {
  322. string url = ApiUrl + "/UserAuthentication?dataformat=" + SerializationFormat.ToString();
  323. // Create the post body
  324. string postContent = string.Format("userid={0}", userId);
  325. if (!string.IsNullOrEmpty(password))
  326. {
  327. postContent += "&password=" + password;
  328. }
  329. HttpContent content = new StringContent(postContent, Encoding.UTF8, "application/x-www-form-urlencoded");
  330. HttpResponseMessage msg = await HttpClient.PostAsync(url, content).ConfigureAwait(false);
  331. using (Stream stream = await msg.Content.ReadAsStreamAsync().ConfigureAwait(false))
  332. {
  333. return DeserializeFromStream<AuthenticationResult>(stream);
  334. }
  335. }
  336. /// <summary>
  337. /// This is a helper around getting a stream from the server that contains serialized data
  338. /// </summary>
  339. private Task<Stream> GetSerializedStreamAsync(string url)
  340. {
  341. return GetSerializedStreamAsync(url, SerializationFormat);
  342. }
  343. /// <summary>
  344. /// This is a helper around getting a stream from the server that contains serialized data
  345. /// </summary>
  346. private Task<Stream> GetSerializedStreamAsync(string url, SerializationFormats serializationFormat)
  347. {
  348. if (url.IndexOf('?') == -1)
  349. {
  350. url += "?dataformat=" + serializationFormat.ToString();
  351. }
  352. else
  353. {
  354. url += "&dataformat=" + serializationFormat.ToString();
  355. }
  356. return GetStreamAsync(url);
  357. }
  358. /// <summary>
  359. /// This is just a helper around HttpClient
  360. /// </summary>
  361. private Task<Stream> GetStreamAsync(string url)
  362. {
  363. return HttpClient.GetStreamAsync(url);
  364. }
  365. public override void Dispose()
  366. {
  367. HttpClient.Dispose();
  368. }
  369. }
  370. }