ApiClient.cs 16 KB

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