ApiClient.cs 27 KB

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