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

pull/702/head
LukePulverenti Luke Pulverenti luke pulverenti 12 years ago
parent 8edc1ce710
commit 32f7ecf4d0

@ -19,6 +19,11 @@ namespace MediaBrowser.ApiInteraction
{ {
} }
public ApiClient(HttpClientHandler handler)
: base(handler)
{
}
/// <summary> /// <summary>
/// Gets an image url that can be used to download an image from the api /// Gets an image url that can be used to download an image from the api
/// </summary> /// </summary>
@ -70,10 +75,12 @@ namespace MediaBrowser.ApiInteraction
/// </summary> /// </summary>
public async Task<Stream> GetImageStreamAsync(string url) 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. MemoryStream stream = new MemoryStream(bytes);
return new GZipStream(stream, CompressionMode.Decompress, false);
return stream;*/
} }
/// <summary> /// <summary>
@ -90,10 +97,7 @@ namespace MediaBrowser.ApiInteraction
using (Stream stream = await HttpClient.GetStreamAsync(url)) using (Stream stream = await HttpClient.GetStreamAsync(url))
{ {
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false)) return JsonSerializer.DeserializeFromStream<ApiBaseItemWrapper<ApiBaseItem>>(stream);
{
return JsonSerializer.DeserializeFromStream<ApiBaseItemWrapper<ApiBaseItem>>(gzipStream);
}
} }
} }
@ -106,10 +110,7 @@ namespace MediaBrowser.ApiInteraction
using (Stream stream = await HttpClient.GetStreamAsync(url)) using (Stream stream = await HttpClient.GetStreamAsync(url))
{ {
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress, false)) return JsonSerializer.DeserializeFromStream<IEnumerable<User>>(stream);
{
return JsonSerializer.DeserializeFromStream<IEnumerable<User>>(gzipStream);
}
} }
} }

@ -13,8 +13,15 @@ namespace MediaBrowser.ApiInteraction
protected HttpClient HttpClient { get; private set; } protected HttpClient HttpClient { get; private set; }
public BaseClient() 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() public void Dispose()

Loading…
Cancel
Save