ApiClient.cs 14 KB

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