ApiClient.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Http;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Model.Configuration;
  7. using MediaBrowser.Model.DTO;
  8. using MediaBrowser.Model.Entities;
  9. using MediaBrowser.Model.Weather;
  10. using System.Text;
  11. namespace MediaBrowser.ApiInteraction
  12. {
  13. public class ApiClient : IDisposable
  14. {
  15. public ApiClient(HttpClientHandler handler)
  16. {
  17. handler.AutomaticDecompression = DecompressionMethods.Deflate;
  18. HttpClient = new HttpClient(handler);
  19. DataSerializer.Configure();
  20. }
  21. /// <summary>
  22. /// Gets or sets the server host name (myserver or 192.168.x.x)
  23. /// </summary>
  24. public string ServerHostName { get; set; }
  25. /// <summary>
  26. /// Gets or sets the port number used by the API
  27. /// </summary>
  28. public int ServerApiPort { get; set; }
  29. /// <summary>
  30. /// Gets the current api url based on hostname and port.
  31. /// </summary>
  32. protected string ApiUrl
  33. {
  34. get
  35. {
  36. return string.Format("http://{0}:{1}/mediabrowser/api", ServerHostName, ServerApiPort);
  37. }
  38. }
  39. /// <summary>
  40. /// Gets the data format to request from the server
  41. /// </summary>
  42. private SerializationFormats SerializationFormat
  43. {
  44. get
  45. {
  46. return ApiInteraction.SerializationFormats.Protobuf;
  47. }
  48. }
  49. public HttpClient HttpClient { get; private set; }
  50. /// <summary>
  51. /// Gets an image url that can be used to download an image from the api
  52. /// </summary>
  53. /// <param name="itemId">The Id of the item</param>
  54. /// <param name="imageType">The type of image requested</param>
  55. /// <param name="imageIndex">The image index, if there are multiple. Currently only applies to backdrops. Supply null or 0 for first backdrop.</param>
  56. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  57. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  58. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  59. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  60. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  61. public string GetImageUrl(Guid itemId, ImageType imageType, int? imageIndex = null, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  62. {
  63. string url = ApiUrl + "/image";
  64. url += "?id=" + itemId.ToString();
  65. url += "&type=" + imageType.ToString();
  66. if (imageIndex.HasValue)
  67. {
  68. url += "&index=" + imageIndex;
  69. }
  70. if (width.HasValue)
  71. {
  72. url += "&width=" + width;
  73. }
  74. if (height.HasValue)
  75. {
  76. url += "&height=" + height;
  77. }
  78. if (maxWidth.HasValue)
  79. {
  80. url += "&maxWidth=" + maxWidth;
  81. }
  82. if (maxHeight.HasValue)
  83. {
  84. url += "&maxHeight=" + maxHeight;
  85. }
  86. if (quality.HasValue)
  87. {
  88. url += "&quality=" + quality;
  89. }
  90. return url;
  91. }
  92. /// <summary>
  93. /// Gets an image url that can be used to download an image from the api
  94. /// </summary>
  95. /// <param name="userId">The Id of the user</param>
  96. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  97. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  98. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  99. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  100. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  101. public string GetUserImageUrl(Guid userId, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  102. {
  103. string url = ApiUrl + "/image";
  104. url += "?userId=" + userId.ToString();
  105. if (width.HasValue)
  106. {
  107. url += "&width=" + width;
  108. }
  109. if (height.HasValue)
  110. {
  111. url += "&height=" + height;
  112. }
  113. if (maxWidth.HasValue)
  114. {
  115. url += "&maxWidth=" + maxWidth;
  116. }
  117. if (maxHeight.HasValue)
  118. {
  119. url += "&maxHeight=" + maxHeight;
  120. }
  121. if (quality.HasValue)
  122. {
  123. url += "&quality=" + quality;
  124. }
  125. return url;
  126. }
  127. /// <summary>
  128. /// Gets an image url that can be used to download an image from the api
  129. /// </summary>
  130. /// <param name="name">The name of the person</param>
  131. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  132. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  133. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  134. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  135. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  136. public string GetPersonImageUrl(string name, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  137. {
  138. string url = ApiUrl + "/image";
  139. url += "?personname=" + name;
  140. if (width.HasValue)
  141. {
  142. url += "&width=" + width;
  143. }
  144. if (height.HasValue)
  145. {
  146. url += "&height=" + height;
  147. }
  148. if (maxWidth.HasValue)
  149. {
  150. url += "&maxWidth=" + maxWidth;
  151. }
  152. if (maxHeight.HasValue)
  153. {
  154. url += "&maxHeight=" + maxHeight;
  155. }
  156. if (quality.HasValue)
  157. {
  158. url += "&quality=" + quality;
  159. }
  160. return url;
  161. }
  162. /// <summary>
  163. /// Gets an image url that can be used to download an image from the api
  164. /// </summary>
  165. /// <param name="year">The year</param>
  166. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  167. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  168. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  169. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  170. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  171. public string GetYearImageUrl(int year, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  172. {
  173. string url = ApiUrl + "/image";
  174. url += "?year=" + year;
  175. if (width.HasValue)
  176. {
  177. url += "&width=" + width;
  178. }
  179. if (height.HasValue)
  180. {
  181. url += "&height=" + height;
  182. }
  183. if (maxWidth.HasValue)
  184. {
  185. url += "&maxWidth=" + maxWidth;
  186. }
  187. if (maxHeight.HasValue)
  188. {
  189. url += "&maxHeight=" + maxHeight;
  190. }
  191. if (quality.HasValue)
  192. {
  193. url += "&quality=" + quality;
  194. }
  195. return url;
  196. }
  197. /// <summary>
  198. /// Gets an image url that can be used to download an image from the api
  199. /// </summary>
  200. /// <param name="name">The name of the genre</param>
  201. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  202. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  203. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  204. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  205. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  206. public string GetGenreImageUrl(string name, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  207. {
  208. string url = ApiUrl + "/image";
  209. url += "?genre=" + name;
  210. if (width.HasValue)
  211. {
  212. url += "&width=" + width;
  213. }
  214. if (height.HasValue)
  215. {
  216. url += "&height=" + height;
  217. }
  218. if (maxWidth.HasValue)
  219. {
  220. url += "&maxWidth=" + maxWidth;
  221. }
  222. if (maxHeight.HasValue)
  223. {
  224. url += "&maxHeight=" + maxHeight;
  225. }
  226. if (quality.HasValue)
  227. {
  228. url += "&quality=" + quality;
  229. }
  230. return url;
  231. }
  232. /// <summary>
  233. /// Gets an image url that can be used to download an image from the api
  234. /// </summary>
  235. /// <param name="name">The name of the studio</param>
  236. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  237. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  238. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  239. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  240. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  241. public string GetStudioImageUrl(string name, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  242. {
  243. string url = ApiUrl + "/image";
  244. url += "?studio=" + name;
  245. if (width.HasValue)
  246. {
  247. url += "&width=" + width;
  248. }
  249. if (height.HasValue)
  250. {
  251. url += "&height=" + height;
  252. }
  253. if (maxWidth.HasValue)
  254. {
  255. url += "&maxWidth=" + maxWidth;
  256. }
  257. if (maxHeight.HasValue)
  258. {
  259. url += "&maxHeight=" + maxHeight;
  260. }
  261. if (quality.HasValue)
  262. {
  263. url += "&quality=" + quality;
  264. }
  265. return url;
  266. }
  267. /// <summary>
  268. /// This is a helper to get a list of backdrop url's from a given ApiBaseItemWrapper. If the actual item does not have any backdrops it will return backdrops from the first parent that does.
  269. /// </summary>
  270. /// <param name="item">A given item.</param>
  271. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  272. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  273. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  274. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  275. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  276. public string[] GetBackdropImageUrls(DTOBaseItem item, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  277. {
  278. Guid? backdropItemId = null;
  279. int backdropCount = 0;
  280. if (item.BackdropCount == 0)
  281. {
  282. backdropItemId = item.ParentBackdropItemId;
  283. backdropCount = item.ParentBackdropCount ?? 0;
  284. }
  285. else
  286. {
  287. backdropItemId = item.Id;
  288. backdropCount = item.BackdropCount;
  289. }
  290. if (backdropItemId == null)
  291. {
  292. return new string[] { };
  293. }
  294. string[] files = new string[backdropCount];
  295. for (int i = 0; i < backdropCount; i++)
  296. {
  297. files[i] = GetImageUrl(backdropItemId.Value, ImageType.Backdrop, i, width, height, maxWidth, maxHeight, quality);
  298. }
  299. return files;
  300. }
  301. /// <summary>
  302. /// This is a helper to get the logo image url from a given ApiBaseItemWrapper. If the actual item does not have a logo, it will return the logo from the first parent that does, or null.
  303. /// </summary>
  304. /// <param name="item">A given item.</param>
  305. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  306. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  307. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  308. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  309. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  310. public string GetLogoImageUrl(DTOBaseItem item, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null)
  311. {
  312. Guid? logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId;
  313. if (logoItemId.HasValue)
  314. {
  315. return GetImageUrl(logoItemId.Value, ImageType.Logo, null, width, height, maxWidth, maxHeight, quality);
  316. }
  317. return null;
  318. }
  319. /// <summary>
  320. /// Gets an image stream based on a url
  321. /// </summary>
  322. public Task<Stream> GetImageStreamAsync(string url)
  323. {
  324. return GetStreamAsync(url);
  325. }
  326. /// <summary>
  327. /// Gets a BaseItem
  328. /// </summary>
  329. public async Task<DTOBaseItem> GetItemAsync(Guid id, Guid userId)
  330. {
  331. string url = ApiUrl + "/item?userId=" + userId.ToString();
  332. if (id != Guid.Empty)
  333. {
  334. url += "&id=" + id.ToString();
  335. }
  336. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  337. {
  338. return DeserializeFromStream<DTOBaseItem>(stream);
  339. }
  340. }
  341. /// <summary>
  342. /// Gets all Users
  343. /// </summary>
  344. public async Task<DTOUser[]> GetAllUsersAsync()
  345. {
  346. string url = ApiUrl + "/users";
  347. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  348. {
  349. return DeserializeFromStream<DTOUser[]>(stream);
  350. }
  351. }
  352. /// <summary>
  353. /// Gets all Genres
  354. /// </summary>
  355. public async Task<IBNItem[]> GetAllGenresAsync(Guid userId)
  356. {
  357. string url = ApiUrl + "/genres?userId=" + userId.ToString();
  358. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  359. {
  360. return DeserializeFromStream<IBNItem[]>(stream);
  361. }
  362. }
  363. /// <summary>
  364. /// Gets all Years
  365. /// </summary>
  366. public async Task<IBNItem[]> GetAllYearsAsync(Guid userId)
  367. {
  368. string url = ApiUrl + "/years?userId=" + userId.ToString();
  369. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  370. {
  371. return DeserializeFromStream<IBNItem[]>(stream);
  372. }
  373. }
  374. /// <summary>
  375. /// Gets all items that contain a given Year
  376. /// </summary>
  377. public async Task<DTOBaseItem[]> GetItemsWithYearAsync(string name, Guid userId)
  378. {
  379. string url = ApiUrl + "/itemlist?listtype=itemswithyear&userId=" + userId.ToString() + "&name=" + name;
  380. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  381. {
  382. return DeserializeFromStream<DTOBaseItem[]>(stream);
  383. }
  384. }
  385. /// <summary>
  386. /// Gets all items that contain a given Genre
  387. /// </summary>
  388. public async Task<DTOBaseItem[]> GetItemsWithGenreAsync(string name, Guid userId)
  389. {
  390. string url = ApiUrl + "/itemlist?listtype=itemswithgenre&userId=" + userId.ToString() + "&name=" + name;
  391. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  392. {
  393. return DeserializeFromStream<DTOBaseItem[]>(stream);
  394. }
  395. }
  396. /// <summary>
  397. /// Gets all items that contain a given Person
  398. /// </summary>
  399. public async Task<DTOBaseItem[]> GetItemsWithPersonAsync(string name, Guid userId)
  400. {
  401. string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId.ToString() + "&name=" + name;
  402. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  403. {
  404. return DeserializeFromStream<DTOBaseItem[]>(stream);
  405. }
  406. }
  407. /// <summary>
  408. /// Gets all items that contain a given Person
  409. /// </summary>
  410. public async Task<DTOBaseItem[]> GetItemsWithPersonAsync(string name, string personType, Guid userId)
  411. {
  412. string url = ApiUrl + "/itemlist?listtype=itemswithperson&userId=" + userId.ToString() + "&name=" + name;
  413. url += "&persontype=" + personType;
  414. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  415. {
  416. return DeserializeFromStream<DTOBaseItem[]>(stream);
  417. }
  418. }
  419. /// <summary>
  420. /// Gets all studious
  421. /// </summary>
  422. public async Task<IBNItem[]> GetAllStudiosAsync(Guid userId)
  423. {
  424. string url = ApiUrl + "/studios?userId=" + userId.ToString();
  425. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  426. {
  427. return DeserializeFromStream<IBNItem[]>(stream);
  428. }
  429. }
  430. /// <summary>
  431. /// Gets all items that contain a given Studio
  432. /// </summary>
  433. public async Task<DTOBaseItem[]> GetItemsWithStudioAsync(string name, Guid userId)
  434. {
  435. string url = ApiUrl + "/itemlist?listtype=itemswithstudio&userId=" + userId.ToString() + "&name=" + name;
  436. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  437. {
  438. return DeserializeFromStream<DTOBaseItem[]>(stream);
  439. }
  440. }
  441. /// <summary>
  442. /// Gets a studio
  443. /// </summary>
  444. public async Task<IBNItem> GetStudioAsync(Guid userId, string name)
  445. {
  446. string url = ApiUrl + "/studio?userId=" + userId.ToString() + "&name=" + name;
  447. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  448. {
  449. return DeserializeFromStream<IBNItem>(stream);
  450. }
  451. }
  452. /// <summary>
  453. /// Gets a genre
  454. /// </summary>
  455. public async Task<IBNItem> GetGenreAsync(Guid userId, string name)
  456. {
  457. string url = ApiUrl + "/genre?userId=" + userId.ToString() + "&name=" + name;
  458. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  459. {
  460. return DeserializeFromStream<IBNItem>(stream);
  461. }
  462. }
  463. /// <summary>
  464. /// Gets a person
  465. /// </summary>
  466. public async Task<IBNItem> GetPersonAsync(Guid userId, string name)
  467. {
  468. string url = ApiUrl + "/person?userId=" + userId.ToString() + "&name=" + name;
  469. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  470. {
  471. return DeserializeFromStream<IBNItem>(stream);
  472. }
  473. }
  474. /// <summary>
  475. /// Gets a year
  476. /// </summary>
  477. public async Task<IBNItem> GetYearAsync(Guid userId, int year)
  478. {
  479. string url = ApiUrl + "/year?userId=" + userId.ToString() + "&year=" + year;
  480. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  481. {
  482. return DeserializeFromStream<IBNItem>(stream);
  483. }
  484. }
  485. /// <summary>
  486. /// Gets a list of plugins installed on the server
  487. /// </summary>
  488. public async Task<PluginInfo[]> GetInstalledPluginsAsync()
  489. {
  490. string url = ApiUrl + "/plugins";
  491. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  492. {
  493. return DeserializeFromStream<PluginInfo[]>(stream);
  494. }
  495. }
  496. /// <summary>
  497. /// Gets a list of plugins installed on the server
  498. /// </summary>
  499. public Task<Stream> GetPluginAssemblyAsync(PluginInfo plugin)
  500. {
  501. string url = ApiUrl + "/pluginassembly?assemblyfilename=" + plugin.AssemblyFileName;
  502. return GetStreamAsync(url);
  503. }
  504. /// <summary>
  505. /// Gets the current server configuration
  506. /// </summary>
  507. public async Task<ServerConfiguration> GetServerConfigurationAsync()
  508. {
  509. string url = ApiUrl + "/ServerConfiguration";
  510. // At the moment this can't be retrieved in protobuf format
  511. SerializationFormats format = DataSerializer.CanDeSerializeJsv ? SerializationFormats.Jsv : SerializationFormats.Json;
  512. using (Stream stream = await GetSerializedStreamAsync(url, format).ConfigureAwait(false))
  513. {
  514. return DataSerializer.DeserializeFromStream<ServerConfiguration>(stream, format);
  515. }
  516. }
  517. /// <summary>
  518. /// Gets weather information for the default location as set in configuration
  519. /// </summary>
  520. public async Task<object> GetPluginConfigurationAsync(PluginInfo plugin, Type configurationType)
  521. {
  522. string url = ApiUrl + "/PluginConfiguration?assemblyfilename=" + plugin.AssemblyFileName;
  523. // At the moment this can't be retrieved in protobuf format
  524. SerializationFormats format = DataSerializer.CanDeSerializeJsv ? SerializationFormats.Jsv : SerializationFormats.Json;
  525. using (Stream stream = await GetSerializedStreamAsync(url, format).ConfigureAwait(false))
  526. {
  527. return DataSerializer.DeserializeFromStream(stream, format, configurationType);
  528. }
  529. }
  530. /// <summary>
  531. /// Gets weather information for the default location as set in configuration
  532. /// </summary>
  533. public async Task<DTOUser> GetDefaultUserAsync()
  534. {
  535. string url = ApiUrl + "/defaultuser";
  536. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  537. {
  538. return DeserializeFromStream<DTOUser>(stream);
  539. }
  540. }
  541. /// <summary>
  542. /// Gets weather information for the default location as set in configuration
  543. /// </summary>
  544. public async Task<WeatherInfo> GetWeatherInfoAsync()
  545. {
  546. string url = ApiUrl + "/weather";
  547. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  548. {
  549. return DeserializeFromStream<WeatherInfo>(stream);
  550. }
  551. }
  552. /// <summary>
  553. /// Gets weather information for a specific zip code
  554. /// </summary>
  555. public async Task<WeatherInfo> GetWeatherInfoAsync(string zipCode)
  556. {
  557. string url = ApiUrl + "/weather?zipcode=" + zipCode;
  558. using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
  559. {
  560. return DeserializeFromStream<WeatherInfo>(stream);
  561. }
  562. }
  563. /// <summary>
  564. /// Authenticates a user and returns the result
  565. /// </summary>
  566. public async Task<AuthenticationResult> AuthenticateUserAsync(Guid userId, string password)
  567. {
  568. string url = ApiUrl + "/UserAuthentication?dataformat=" + SerializationFormat.ToString();
  569. // Create the post body
  570. string postContent = string.Format("userid={0}&password={1}", userId, password);
  571. HttpContent content = new StringContent(postContent, Encoding.UTF8, "application/x-www-form-urlencoded");
  572. HttpResponseMessage msg = await HttpClient.PostAsync(url, content).ConfigureAwait(false);
  573. using (Stream stream = await msg.Content.ReadAsStreamAsync().ConfigureAwait(false))
  574. {
  575. return DeserializeFromStream<AuthenticationResult>(stream);
  576. }
  577. }
  578. /// <summary>
  579. /// This is a helper around getting a stream from the server that contains serialized data
  580. /// </summary>
  581. private Task<Stream> GetSerializedStreamAsync(string url)
  582. {
  583. return GetSerializedStreamAsync(url, SerializationFormat);
  584. }
  585. /// <summary>
  586. /// This is a helper around getting a stream from the server that contains serialized data
  587. /// </summary>
  588. private Task<Stream> GetSerializedStreamAsync(string url, SerializationFormats serializationFormat)
  589. {
  590. if (url.IndexOf('?') == -1)
  591. {
  592. url += "?dataformat=" + serializationFormat.ToString();
  593. }
  594. else
  595. {
  596. url += "&dataformat=" + serializationFormat.ToString();
  597. }
  598. return GetStreamAsync(url);
  599. }
  600. private T DeserializeFromStream<T>(Stream stream)
  601. {
  602. return DataSerializer.DeserializeFromStream<T>(stream, SerializationFormat);
  603. }
  604. /// <summary>
  605. /// This is just a helper around HttpClient
  606. /// </summary>
  607. private Task<Stream> GetStreamAsync(string url)
  608. {
  609. return HttpClient.GetStreamAsync(url);
  610. }
  611. public void Dispose()
  612. {
  613. HttpClient.Dispose();
  614. }
  615. }
  616. }