BaseHttpApiClient.cs 16 KB

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