ApiClient.cs 28 KB

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