From 32f7ecf4d0876da9265599266de5e170e83d2d2b Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Tue, 31 Jul 2012 21:53:40 -0400 Subject: [PATCH] Added HttpClientHandler as a constructor param to ApiClient, and added automatic decompression. --- MediaBrowser.ApiInteraction/ApiClient.cs | 23 ++++++++++++----------- MediaBrowser.ApiInteraction/BaseClient.cs | 9 ++++++++- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/MediaBrowser.ApiInteraction/ApiClient.cs b/MediaBrowser.ApiInteraction/ApiClient.cs index 06d7b7ed42..5174f598df 100644 --- a/MediaBrowser.ApiInteraction/ApiClient.cs +++ b/MediaBrowser.ApiInteraction/ApiClient.cs @@ -19,6 +19,11 @@ namespace MediaBrowser.ApiInteraction { } + public ApiClient(HttpClientHandler handler) + : base(handler) + { + } + /// /// Gets an image url that can be used to download an image from the api /// @@ -70,10 +75,12 @@ namespace MediaBrowser.ApiInteraction /// public async Task GetImageStreamAsync(string url) { - Stream stream = await HttpClient.GetStreamAsync(url); + return await HttpClient.GetStreamAsync(url); + /*byte[] bytes = await HttpClient.GetByteArrayAsync(url); - // For now this assumes the response stream is compressed. We can always improve this later. - return new GZipStream(stream, CompressionMode.Decompress, false); + MemoryStream stream = new MemoryStream(bytes); + + return stream;*/ } /// @@ -90,10 +97,7 @@ namespace MediaBrowser.ApiInteraction using (Stream stream = await HttpClient.GetStreamAsync(url)) { - using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false)) - { - return JsonSerializer.DeserializeFromStream>(gzipStream); - } + return JsonSerializer.DeserializeFromStream>(stream); } } @@ -106,10 +110,7 @@ namespace MediaBrowser.ApiInteraction using (Stream stream = await HttpClient.GetStreamAsync(url)) { - using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false)) - { - return JsonSerializer.DeserializeFromStream>(gzipStream); - } + return JsonSerializer.DeserializeFromStream>(stream); } } diff --git a/MediaBrowser.ApiInteraction/BaseClient.cs b/MediaBrowser.ApiInteraction/BaseClient.cs index ab76b9e486..bd25b1653b 100644 --- a/MediaBrowser.ApiInteraction/BaseClient.cs +++ b/MediaBrowser.ApiInteraction/BaseClient.cs @@ -13,8 +13,15 @@ namespace MediaBrowser.ApiInteraction protected HttpClient HttpClient { get; private set; } public BaseClient() + : this(new HttpClientHandler()) { - HttpClient = new HttpClient(); + } + + public BaseClient(HttpClientHandler clientHandler) + { + clientHandler.AutomaticDecompression = System.Net.DecompressionMethods.GZip; + + HttpClient = new HttpClient(clientHandler); } public void Dispose()