ApiClient.cs 15 KB

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