BaseHttpApiClient.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. using MediaBrowser.Model.Authentication;
  2. using MediaBrowser.Model.Configuration;
  3. using MediaBrowser.Model.DTO;
  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. using MediaBrowser.Model.Entities;
  12. namespace MediaBrowser.ApiInteraction
  13. {
  14. /// <summary>
  15. /// Provides api methods centered around an HttpClient
  16. /// </summary>
  17. public abstract class BaseHttpApiClient : BaseApiClient
  18. {
  19. public BaseHttpApiClient(HttpClientHandler handler)
  20. : base()
  21. {
  22. handler.AutomaticDecompression = DecompressionMethods.Deflate;
  23. HttpClient = new HttpClient(handler);
  24. }
  25. private HttpClient HttpClient { get; set; }
  26. /// <summary>
  27. /// Gets an image stream based on a url
  28. /// </summary>
  29. public Task<Stream> GetImageStreamAsync(string url)
  30. {
  31. return GetStreamAsync(url);
  32. }
  33. /// <summary>
  34. /// Gets a BaseItem
  35. /// </summary>
  36. public async Task<DTOBaseItem> GetItemAsync(Guid id, Guid userId)
  37. {
  38. string url = ApiUrl + "/item?userId=" + userId.ToString();
  39. if (id != Guid.Empty)
  40. {
  41. url += "&id=" + id.ToString();
  42. }
  43. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  44. {
  45. return DeserializeFromStream<DTOBaseItem>(stream);
  46. }
  47. }
  48. /// <summary>
  49. /// Gets all Users
  50. /// </summary>
  51. public async Task<DTOUser[]> GetAllUsersAsync()
  52. {
  53. string url = ApiUrl + "/users";
  54. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  55. {
  56. return DeserializeFromStream<DTOUser[]>(stream);
  57. }
  58. }
  59. /// <summary>
  60. /// Gets all Genres
  61. /// </summary>
  62. public async Task<IBNItem[]> GetAllGenresAsync(Guid userId)
  63. {
  64. string url = ApiUrl + "/genres?userId=" + userId.ToString();
  65. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  66. {
  67. return DeserializeFromStream<IBNItem[]>(stream);
  68. }
  69. }
  70. /// <summary>
  71. /// Gets in-progress items
  72. /// </summary>
  73. /// <param name="userId">The user id.</param>
  74. /// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
  75. public async Task<DTOBaseItem[]> GetInProgressItemsItemsAsync(Guid userId, Guid? folderId = null)
  76. {
  77. string url = ApiUrl + "/itemlist?listtype=inprogressitems&userId=" + userId.ToString();
  78. if (folderId.HasValue)
  79. {
  80. url += "&id=" + folderId.ToString();
  81. }
  82. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  83. {
  84. return DeserializeFromStream<DTOBaseItem[]>(stream);
  85. }
  86. }
  87. /// <summary>
  88. /// Gets recently added items
  89. /// </summary>
  90. /// <param name="userId">The user id.</param>
  91. /// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
  92. public async Task<DTOBaseItem[]> GetRecentlyAddedItemsAsync(Guid userId, Guid? folderId = null)
  93. {
  94. string url = ApiUrl + "/itemlist?listtype=recentlyaddeditems&userId=" + userId.ToString();
  95. if (folderId.HasValue)
  96. {
  97. url += "&id=" + folderId.ToString();
  98. }
  99. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  100. {
  101. return DeserializeFromStream<DTOBaseItem[]>(stream);
  102. }
  103. }
  104. /// <summary>
  105. /// Gets favorite items
  106. /// </summary>
  107. /// <param name="userId">The user id.</param>
  108. /// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
  109. public async Task<DTOBaseItem[]> GetFavoriteItemsAsync(Guid userId, Guid? folderId = null)
  110. {
  111. string url = ApiUrl + "/itemlist?listtype=favorites&userId=" + userId.ToString();
  112. if (folderId.HasValue)
  113. {
  114. url += "&id=" + folderId.ToString();
  115. }
  116. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  117. {
  118. return DeserializeFromStream<DTOBaseItem[]>(stream);
  119. }
  120. }
  121. /// <summary>
  122. /// Gets recently added items that are unplayed.
  123. /// </summary>
  124. /// <param name="userId">The user id.</param>
  125. /// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
  126. public async Task<DTOBaseItem[]> GetRecentlyAddedUnplayedItemsAsync(Guid userId, Guid? folderId = null)
  127. {
  128. string url = ApiUrl + "/itemlist?listtype=recentlyaddedunplayeditems&userId=" + userId.ToString();
  129. if (folderId.HasValue)
  130. {
  131. url += "&id=" + folderId.ToString();
  132. }
  133. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  134. {
  135. return DeserializeFromStream<DTOBaseItem[]>(stream);
  136. }
  137. }
  138. /// <summary>
  139. /// Gets all Years
  140. /// </summary>
  141. public async Task<IBNItem[]> GetAllYearsAsync(Guid userId)
  142. {
  143. string url = ApiUrl + "/years?userId=" + userId.ToString();
  144. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  145. {
  146. return DeserializeFromStream<IBNItem[]>(stream);
  147. }
  148. }
  149. /// <summary>
  150. /// Gets all items that contain a given Year
  151. /// </summary>
  152. /// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
  153. public async Task<DTOBaseItem[]> GetItemsWithYearAsync(string name, Guid userId, Guid? folderId = null)
  154. {
  155. string url = ApiUrl + "/itemlist?listtype=itemswithyear&userId=" + userId.ToString() + "&name=" + name;
  156. if (folderId.HasValue)
  157. {
  158. url += "&id=" + folderId.ToString();
  159. }
  160. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  161. {
  162. return DeserializeFromStream<DTOBaseItem[]>(stream);
  163. }
  164. }
  165. /// <summary>
  166. /// Gets all items that contain a given Genre
  167. /// </summary>
  168. /// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
  169. public async Task<DTOBaseItem[]> GetItemsWithGenreAsync(string name, Guid userId, Guid? folderId = null)
  170. {
  171. string url = ApiUrl + "/itemlist?listtype=itemswithgenre&userId=" + userId.ToString() + "&name=" + name;
  172. if (folderId.HasValue)
  173. {
  174. url += "&id=" + folderId.ToString();
  175. }
  176. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  177. {
  178. return DeserializeFromStream<DTOBaseItem[]>(stream);
  179. }
  180. }
  181. /// <summary>
  182. /// Gets all items that contain a given Person
  183. /// </summary>
  184. /// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
  185. public async Task<DTOBaseItem[]> GetItemsWithPersonAsync(string name, Guid userId, Guid? folderId = null)
  186. {
  187. string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId.ToString() + "&name=" + name;
  188. if (folderId.HasValue)
  189. {
  190. url += "&id=" + folderId.ToString();
  191. }
  192. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  193. {
  194. return DeserializeFromStream<DTOBaseItem[]>(stream);
  195. }
  196. }
  197. /// <summary>
  198. /// Gets all items that contain a given Person
  199. /// </summary>
  200. /// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
  201. public async Task<DTOBaseItem[]> GetItemsWithPersonAsync(string name, string personType, Guid userId, Guid? folderId = null)
  202. {
  203. string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId.ToString() + "&name=" + name;
  204. url += "&persontype=" + personType;
  205. if (folderId.HasValue)
  206. {
  207. url += "&id=" + folderId.ToString();
  208. }
  209. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  210. {
  211. return DeserializeFromStream<DTOBaseItem[]>(stream);
  212. }
  213. }
  214. /// <summary>
  215. /// Gets all studious
  216. /// </summary>
  217. public async Task<IBNItem[]> GetAllStudiosAsync(Guid userId)
  218. {
  219. string url = ApiUrl + "/studios?userId=" + userId.ToString();
  220. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  221. {
  222. return DeserializeFromStream<IBNItem[]>(stream);
  223. }
  224. }
  225. /// <summary>
  226. /// Gets all items that contain a given Studio
  227. /// </summary>
  228. /// <param name="folderId">(Optional) Specify a folder Id to localize the search to a specific folder.</param>
  229. public async Task<DTOBaseItem[]> GetItemsWithStudioAsync(string name, Guid userId, Guid? folderId = null)
  230. {
  231. string url = ApiUrl + "/itemlist?listtype=itemswithstudio&userId=" + userId.ToString() + "&name=" + name;
  232. if (folderId.HasValue)
  233. {
  234. url += "&id=" + folderId.ToString();
  235. }
  236. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  237. {
  238. return DeserializeFromStream<DTOBaseItem[]>(stream);
  239. }
  240. }
  241. /// <summary>
  242. /// Gets a studio
  243. /// </summary>
  244. public async Task<IBNItem> GetStudioAsync(Guid userId, string name)
  245. {
  246. string url = ApiUrl + "/studio?userId=" + userId.ToString() + "&name=" + name;
  247. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  248. {
  249. return DeserializeFromStream<IBNItem>(stream);
  250. }
  251. }
  252. /// <summary>
  253. /// Gets a genre
  254. /// </summary>
  255. public async Task<IBNItem> GetGenreAsync(Guid userId, string name)
  256. {
  257. string url = ApiUrl + "/genre?userId=" + userId.ToString() + "&name=" + name;
  258. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  259. {
  260. return DeserializeFromStream<IBNItem>(stream);
  261. }
  262. }
  263. /// <summary>
  264. /// Gets a person
  265. /// </summary>
  266. public async Task<IBNItem> GetPersonAsync(Guid userId, string name)
  267. {
  268. string url = ApiUrl + "/person?userId=" + userId.ToString() + "&name=" + name;
  269. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  270. {
  271. return DeserializeFromStream<IBNItem>(stream);
  272. }
  273. }
  274. /// <summary>
  275. /// Gets a year
  276. /// </summary>
  277. public async Task<IBNItem> GetYearAsync(Guid userId, int year)
  278. {
  279. string url = ApiUrl + "/year?userId=" + userId.ToString() + "&year=" + year;
  280. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  281. {
  282. return DeserializeFromStream<IBNItem>(stream);
  283. }
  284. }
  285. /// <summary>
  286. /// Gets a list of plugins installed on the server
  287. /// </summary>
  288. public async Task<PluginInfo[]> GetInstalledPluginsAsync()
  289. {
  290. string url = ApiUrl + "/plugins";
  291. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  292. {
  293. return DeserializeFromStream<PluginInfo[]>(stream);
  294. }
  295. }
  296. /// <summary>
  297. /// Gets a list of plugins installed on the server
  298. /// </summary>
  299. public Task<Stream> GetPluginAssemblyAsync(PluginInfo plugin)
  300. {
  301. string url = ApiUrl + "/pluginassembly?assemblyfilename=" + plugin.AssemblyFileName;
  302. return GetStreamAsync(url);
  303. }
  304. /// <summary>
  305. /// Gets the current server configuration
  306. /// </summary>
  307. public async Task<ServerConfiguration> GetServerConfigurationAsync()
  308. {
  309. string url = ApiUrl + "/ServerConfiguration";
  310. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  311. {
  312. return DeserializeFromStream<ServerConfiguration>(stream);
  313. }
  314. }
  315. /// <summary>
  316. /// Gets weather information for the default location as set in configuration
  317. /// </summary>
  318. public async Task<object> GetPluginConfigurationAsync(PluginInfo plugin, Type configurationType)
  319. {
  320. string url = ApiUrl + "/PluginConfiguration?assemblyfilename=" + plugin.AssemblyFileName;
  321. // At the moment this can't be retrieved in protobuf format
  322. SerializationFormats format = DataSerializer.CanDeSerializeJsv ? SerializationFormats.Jsv : SerializationFormats.Json;
  323. using (Stream stream = await GetSerializedStreamAsync(url, format).ConfigureAwait(false))
  324. {
  325. return DataSerializer.DeserializeFromStream(stream, format, configurationType);
  326. }
  327. }
  328. /// <summary>
  329. /// Gets the default user
  330. /// </summary>
  331. public async Task<DTOUser> GetDefaultUserAsync()
  332. {
  333. string url = ApiUrl + "/user";
  334. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  335. {
  336. return DeserializeFromStream<DTOUser>(stream);
  337. }
  338. }
  339. /// <summary>
  340. /// Gets a user by id
  341. /// </summary>
  342. public async Task<DTOUser> GetUserAsync(Guid id)
  343. {
  344. string url = ApiUrl + "/user?id=" + id.ToString();
  345. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  346. {
  347. return DeserializeFromStream<DTOUser>(stream);
  348. }
  349. }
  350. /// <summary>
  351. /// Gets weather information for the default location as set in configuration
  352. /// </summary>
  353. public async Task<WeatherInfo> GetWeatherInfoAsync()
  354. {
  355. string url = ApiUrl + "/weather";
  356. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  357. {
  358. return DeserializeFromStream<WeatherInfo>(stream);
  359. }
  360. }
  361. /// <summary>
  362. /// Gets weather information for a specific zip code
  363. /// </summary>
  364. public async Task<WeatherInfo> GetWeatherInfoAsync(string zipCode)
  365. {
  366. string url = ApiUrl + "/weather?zipcode=" + zipCode;
  367. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  368. {
  369. return DeserializeFromStream<WeatherInfo>(stream);
  370. }
  371. }
  372. /// <summary>
  373. /// Updates a user's favorite status for an item and returns the updated UserItemData object.
  374. /// </summary>
  375. public async Task<DTOUserItemData> UpdateFavoriteStatusAsync(Guid itemId, Guid userId, bool isFavorite)
  376. {
  377. string url = ApiUrl + "/favoritestatus?id=" + itemId;
  378. url += "&userid=" + userId;
  379. url += "&isfavorite=" + (isFavorite ? "1" : "0");
  380. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  381. {
  382. return DeserializeFromStream<DTOUserItemData>(stream);
  383. }
  384. }
  385. /// <summary>
  386. /// Clears a user's rating for an item
  387. /// </summary>
  388. public async Task<DTOUserItemData> ClearUserItemRatingAsync(Guid itemId, Guid userId)
  389. {
  390. string url = ApiUrl + "/UserItemRating?id=" + itemId;
  391. url += "&userid=" + userId;
  392. url += "&clear=1";
  393. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  394. {
  395. return DeserializeFromStream<DTOUserItemData>(stream);
  396. }
  397. }
  398. /// <summary>
  399. /// Updates a user's rating for an item, based on a numeric scale
  400. /// </summary>
  401. public async Task<DTOUserItemData> UpdateUserItemRatingAsync(Guid itemId, Guid userId, float value)
  402. {
  403. string url = ApiUrl + "/UserItemRating?id=" + itemId;
  404. url += "&userid=" + userId;
  405. url += "&value=" + value;
  406. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  407. {
  408. return DeserializeFromStream<DTOUserItemData>(stream);
  409. }
  410. }
  411. /// <summary>
  412. /// Updates a user's rating for an item, based on likes or dislikes
  413. /// </summary>
  414. public async Task<DTOUserItemData> UpdateUserItemRatingAsync(Guid itemId, Guid userId, bool likes)
  415. {
  416. string url = ApiUrl + "/UserItemRating?id=" + itemId;
  417. url += "&userid=" + userId;
  418. url += "&likes=" + (likes ? "1" : "0");
  419. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  420. {
  421. return DeserializeFromStream<DTOUserItemData>(stream);
  422. }
  423. }
  424. /// <summary>
  425. /// Authenticates a user and returns the result
  426. /// </summary>
  427. public async Task<AuthenticationResult> AuthenticateUserAsync(Guid userId, string password)
  428. {
  429. string url = ApiUrl + "/UserAuthentication?dataformat=" + SerializationFormat.ToString();
  430. // Create the post body
  431. string postContent = string.Format("userid={0}", userId);
  432. if (!string.IsNullOrEmpty(password))
  433. {
  434. postContent += "&password=" + password;
  435. }
  436. HttpContent content = new StringContent(postContent, Encoding.UTF8, "application/x-www-form-urlencoded");
  437. HttpResponseMessage msg = await HttpClient.PostAsync(url, content).ConfigureAwait(false);
  438. using (Stream stream = await msg.Content.ReadAsStreamAsync().ConfigureAwait(false))
  439. {
  440. return DeserializeFromStream<AuthenticationResult>(stream);
  441. }
  442. }
  443. /// <summary>
  444. /// This is a helper around getting a stream from the server that contains serialized data
  445. /// </summary>
  446. private Task<Stream> GetSerializedStreamAsync(string url)
  447. {
  448. return GetSerializedStreamAsync(url, SerializationFormat);
  449. }
  450. /// <summary>
  451. /// This is a helper around getting a stream from the server that contains serialized data
  452. /// </summary>
  453. private Task<Stream> GetSerializedStreamAsync(string url, SerializationFormats serializationFormat)
  454. {
  455. if (url.IndexOf('?') == -1)
  456. {
  457. url += "?dataformat=" + serializationFormat.ToString();
  458. }
  459. else
  460. {
  461. url += "&dataformat=" + serializationFormat.ToString();
  462. }
  463. return GetStreamAsync(url);
  464. }
  465. /// <summary>
  466. /// This is just a helper around HttpClient
  467. /// </summary>
  468. private Task<Stream> GetStreamAsync(string url)
  469. {
  470. return HttpClient.GetStreamAsync(url);
  471. }
  472. public override void Dispose()
  473. {
  474. HttpClient.Dispose();
  475. }
  476. }
  477. }