ApiClient.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 the recently added items
  68. /// </summary>
  69. /// <param name="userId">The user id.</param>
  70. /// <returns></returns>
  71. public async Task<DTOBaseItem[]> GetRecentlyAddedItemsAsync(Guid userId)
  72. {
  73. string url = ApiUrl + "/itemlist?listtype=recentlyaddeditems&userId=" + userId.ToString();
  74. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  75. {
  76. return DeserializeFromStream<DTOBaseItem[]>(stream);
  77. }
  78. }
  79. /// <summary>
  80. /// Gets recently added items within a specific folder
  81. /// </summary>
  82. /// <param name="userId">The user id.</param>
  83. public async Task<DTOBaseItem[]> GetRecentlyAddedItemsAsync(Guid userId, Guid folderId)
  84. {
  85. string url = ApiUrl + "/itemlist?listtype=recentlyaddeditems&userId=" + userId.ToString();
  86. url += "&id=" + folderId.ToString();
  87. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  88. {
  89. return DeserializeFromStream<DTOBaseItem[]>(stream);
  90. }
  91. }
  92. /// <summary>
  93. /// Gets all Years
  94. /// </summary>
  95. public async Task<IBNItem[]> GetAllYearsAsync(Guid userId)
  96. {
  97. string url = ApiUrl + "/years?userId=" + userId.ToString();
  98. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  99. {
  100. return DeserializeFromStream<IBNItem[]>(stream);
  101. }
  102. }
  103. /// <summary>
  104. /// Gets all items that contain a given Year
  105. /// </summary>
  106. public async Task<DTOBaseItem[]> GetItemsWithYearAsync(string name, Guid userId)
  107. {
  108. string url = ApiUrl + "/itemlist?listtype=itemswithyear&userId=" + userId.ToString() + "&name=" + name;
  109. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  110. {
  111. return DeserializeFromStream<DTOBaseItem[]>(stream);
  112. }
  113. }
  114. /// <summary>
  115. /// Gets all items that contain a given Genre
  116. /// </summary>
  117. public async Task<DTOBaseItem[]> GetItemsWithGenreAsync(string name, Guid userId)
  118. {
  119. string url = ApiUrl + "/itemlist?listtype=itemswithgenre&userId=" + userId.ToString() + "&name=" + name;
  120. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  121. {
  122. return DeserializeFromStream<DTOBaseItem[]>(stream);
  123. }
  124. }
  125. /// <summary>
  126. /// Gets all items that contain a given Person
  127. /// </summary>
  128. public async Task<DTOBaseItem[]> GetItemsWithPersonAsync(string name, Guid userId)
  129. {
  130. string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId.ToString() + "&name=" + name;
  131. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  132. {
  133. return DeserializeFromStream<DTOBaseItem[]>(stream);
  134. }
  135. }
  136. /// <summary>
  137. /// Gets all items that contain a given Person
  138. /// </summary>
  139. public async Task<DTOBaseItem[]> GetItemsWithPersonAsync(string name, string personType, Guid userId)
  140. {
  141. string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId.ToString() + "&name=" + name;
  142. url += "&persontype=" + personType;
  143. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  144. {
  145. return DeserializeFromStream<DTOBaseItem[]>(stream);
  146. }
  147. }
  148. /// <summary>
  149. /// Gets all studious
  150. /// </summary>
  151. public async Task<IBNItem[]> GetAllStudiosAsync(Guid userId)
  152. {
  153. string url = ApiUrl + "/studios?userId=" + userId.ToString();
  154. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  155. {
  156. return DeserializeFromStream<IBNItem[]>(stream);
  157. }
  158. }
  159. /// <summary>
  160. /// Gets all items that contain a given Studio
  161. /// </summary>
  162. public async Task<DTOBaseItem[]> GetItemsWithStudioAsync(string name, Guid userId)
  163. {
  164. string url = ApiUrl + "/itemlist?listtype=itemswithstudio&userId=" + userId.ToString() + "&name=" + name;
  165. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  166. {
  167. return DeserializeFromStream<DTOBaseItem[]>(stream);
  168. }
  169. }
  170. /// <summary>
  171. /// Gets a studio
  172. /// </summary>
  173. public async Task<IBNItem> GetStudioAsync(Guid userId, string name)
  174. {
  175. string url = ApiUrl + "/studio?userId=" + userId.ToString() + "&name=" + name;
  176. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  177. {
  178. return DeserializeFromStream<IBNItem>(stream);
  179. }
  180. }
  181. /// <summary>
  182. /// Gets a genre
  183. /// </summary>
  184. public async Task<IBNItem> GetGenreAsync(Guid userId, string name)
  185. {
  186. string url = ApiUrl + "/genre?userId=" + userId.ToString() + "&name=" + name;
  187. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  188. {
  189. return DeserializeFromStream<IBNItem>(stream);
  190. }
  191. }
  192. /// <summary>
  193. /// Gets a person
  194. /// </summary>
  195. public async Task<IBNItem> GetPersonAsync(Guid userId, string name)
  196. {
  197. string url = ApiUrl + "/person?userId=" + userId.ToString() + "&name=" + name;
  198. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  199. {
  200. return DeserializeFromStream<IBNItem>(stream);
  201. }
  202. }
  203. /// <summary>
  204. /// Gets a year
  205. /// </summary>
  206. public async Task<IBNItem> GetYearAsync(Guid userId, int year)
  207. {
  208. string url = ApiUrl + "/year?userId=" + userId.ToString() + "&year=" + year;
  209. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  210. {
  211. return DeserializeFromStream<IBNItem>(stream);
  212. }
  213. }
  214. /// <summary>
  215. /// Gets a list of plugins installed on the server
  216. /// </summary>
  217. public async Task<PluginInfo[]> GetInstalledPluginsAsync()
  218. {
  219. string url = ApiUrl + "/plugins";
  220. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  221. {
  222. return DeserializeFromStream<PluginInfo[]>(stream);
  223. }
  224. }
  225. /// <summary>
  226. /// Gets a list of plugins installed on the server
  227. /// </summary>
  228. public Task<Stream> GetPluginAssemblyAsync(PluginInfo plugin)
  229. {
  230. string url = ApiUrl + "/pluginassembly?assemblyfilename=" + plugin.AssemblyFileName;
  231. return GetStreamAsync(url);
  232. }
  233. /// <summary>
  234. /// Gets the current server configuration
  235. /// </summary>
  236. public async Task<ServerConfiguration> GetServerConfigurationAsync()
  237. {
  238. string url = ApiUrl + "/ServerConfiguration";
  239. // At the moment this can't be retrieved in protobuf format
  240. SerializationFormats format = DataSerializer.CanDeSerializeJsv ? SerializationFormats.Jsv : SerializationFormats.Json;
  241. using (Stream stream = await GetSerializedStreamAsync(url, format).ConfigureAwait(false))
  242. {
  243. return DataSerializer.DeserializeFromStream<ServerConfiguration>(stream, format);
  244. }
  245. }
  246. /// <summary>
  247. /// Gets weather information for the default location as set in configuration
  248. /// </summary>
  249. public async Task<object> GetPluginConfigurationAsync(PluginInfo plugin, Type configurationType)
  250. {
  251. string url = ApiUrl + "/PluginConfiguration?assemblyfilename=" + plugin.AssemblyFileName;
  252. // At the moment this can't be retrieved in protobuf format
  253. SerializationFormats format = DataSerializer.CanDeSerializeJsv ? SerializationFormats.Jsv : SerializationFormats.Json;
  254. using (Stream stream = await GetSerializedStreamAsync(url, format).ConfigureAwait(false))
  255. {
  256. return DataSerializer.DeserializeFromStream(stream, format, configurationType);
  257. }
  258. }
  259. /// <summary>
  260. /// Gets weather information for the default location as set in configuration
  261. /// </summary>
  262. public async Task<DTOUser> GetDefaultUserAsync()
  263. {
  264. string url = ApiUrl + "/defaultuser";
  265. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  266. {
  267. return DeserializeFromStream<DTOUser>(stream);
  268. }
  269. }
  270. /// <summary>
  271. /// Gets weather information for the default location as set in configuration
  272. /// </summary>
  273. public async Task<WeatherInfo> GetWeatherInfoAsync()
  274. {
  275. string url = ApiUrl + "/weather";
  276. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  277. {
  278. return DeserializeFromStream<WeatherInfo>(stream);
  279. }
  280. }
  281. /// <summary>
  282. /// Gets weather information for a specific zip code
  283. /// </summary>
  284. public async Task<WeatherInfo> GetWeatherInfoAsync(string zipCode)
  285. {
  286. string url = ApiUrl + "/weather?zipcode=" + zipCode;
  287. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  288. {
  289. return DeserializeFromStream<WeatherInfo>(stream);
  290. }
  291. }
  292. /// <summary>
  293. /// Authenticates a user and returns the result
  294. /// </summary>
  295. public async Task<AuthenticationResult> AuthenticateUserAsync(Guid userId, string password)
  296. {
  297. string url = ApiUrl + "/UserAuthentication?dataformat=" + SerializationFormat.ToString();
  298. // Create the post body
  299. string postContent = string.Format("userid={0}", userId);
  300. if (!string.IsNullOrEmpty(password))
  301. {
  302. postContent += "&password=" + password;
  303. }
  304. HttpContent content = new StringContent(postContent, Encoding.UTF8, "application/x-www-form-urlencoded");
  305. HttpResponseMessage msg = await HttpClient.PostAsync(url, content).ConfigureAwait(false);
  306. using (Stream stream = await msg.Content.ReadAsStreamAsync().ConfigureAwait(false))
  307. {
  308. return DeserializeFromStream<AuthenticationResult>(stream);
  309. }
  310. }
  311. /// <summary>
  312. /// This is a helper around getting a stream from the server that contains serialized data
  313. /// </summary>
  314. private Task<Stream> GetSerializedStreamAsync(string url)
  315. {
  316. return GetSerializedStreamAsync(url, SerializationFormat);
  317. }
  318. /// <summary>
  319. /// This is a helper around getting a stream from the server that contains serialized data
  320. /// </summary>
  321. private Task<Stream> GetSerializedStreamAsync(string url, SerializationFormats serializationFormat)
  322. {
  323. if (url.IndexOf('?') == -1)
  324. {
  325. url += "?dataformat=" + serializationFormat.ToString();
  326. }
  327. else
  328. {
  329. url += "&dataformat=" + serializationFormat.ToString();
  330. }
  331. return GetStreamAsync(url);
  332. }
  333. /// <summary>
  334. /// This is just a helper around HttpClient
  335. /// </summary>
  336. private Task<Stream> GetStreamAsync(string url)
  337. {
  338. return HttpClient.GetStreamAsync(url);
  339. }
  340. public override void Dispose()
  341. {
  342. HttpClient.Dispose();
  343. }
  344. }
  345. }