소스 검색

Added HttpClientHandler as a constructor param to ApiClient, and added automatic decompression.

LukePulverenti Luke Pulverenti luke pulverenti 13 년 전
부모
커밋
32f7ecf4d0
2개의 변경된 파일20개의 추가작업 그리고 12개의 파일을 삭제
  1. 12 11
      MediaBrowser.ApiInteraction/ApiClient.cs
  2. 8 1
      MediaBrowser.ApiInteraction/BaseClient.cs

+ 12 - 11
MediaBrowser.ApiInteraction/ApiClient.cs

@@ -19,6 +19,11 @@ namespace MediaBrowser.ApiInteraction
         {
         }
 
+        public ApiClient(HttpClientHandler handler)
+            : base(handler)
+        {
+        }
+
         /// <summary>
         /// Gets an image url that can be used to download an image from the api
         /// </summary>
@@ -70,10 +75,12 @@ namespace MediaBrowser.ApiInteraction
         /// </summary>
         public async Task<Stream> 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;*/
         }
 
         /// <summary>
@@ -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<ApiBaseItemWrapper<ApiBaseItem>>(gzipStream);
-                }
+                return JsonSerializer.DeserializeFromStream<ApiBaseItemWrapper<ApiBaseItem>>(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<IEnumerable<User>>(gzipStream);
-                }
+                return JsonSerializer.DeserializeFromStream<IEnumerable<User>>(stream);
             }
         }
 

+ 8 - 1
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()