refactor get method to use http response

This commit is contained in:
Luke Pulverenti 2013-04-20 15:28:58 -04:00
parent 887c280a1b
commit b88f990b82

View File

@ -104,6 +104,8 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
var message = new HttpRequestMessage(HttpMethod.Get, url);
if (resourcePool != null) if (resourcePool != null)
{ {
await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
@ -115,11 +117,14 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
var msg = await GetHttpClient(GetHostFromUrl(url)).GetAsync(url, cancellationToken).ConfigureAwait(false); using (var response = await GetHttpClient(GetHostFromUrl(url)).SendAsync(message, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false))
{
EnsureSuccessStatusCode(response);
EnsureSuccessStatusCode(msg); cancellationToken.ThrowIfCancellationRequested();
return await msg.Content.ReadAsStreamAsync().ConfigureAwait(false); return await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
}
} }
catch (OperationCanceledException ex) catch (OperationCanceledException ex)
{ {
@ -131,6 +136,12 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
throw new HttpException(ex.Message, ex); throw new HttpException(ex.Message, ex);
} }
catch (Exception ex)
{
_logger.ErrorException("Error getting response from " + url, ex);
throw;
}
finally finally
{ {
if (resourcePool != null) if (resourcePool != null)
@ -209,27 +220,12 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
/// <exception cref="System.ArgumentNullException">progress</exception> /// <exception cref="System.ArgumentNullException">progress</exception>
/// <exception cref="HttpException"></exception> /// <exception cref="HttpException"></exception>
/// <exception cref="MediaBrowser.Model.Net.HttpException"></exception> /// <exception cref="MediaBrowser.Model.Net.HttpException"></exception>
public Task<string> GetTempFile(HttpRequestOptions options) public async Task<string> GetTempFile(HttpRequestOptions options)
{
var tempFile = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + ".tmp");
return GetTempFile(options, tempFile);
}
protected static readonly CultureInfo UsCulture = new CultureInfo("en-US");
/// <summary>
/// Gets the temp file.
/// </summary>
/// <param name="options">The options.</param>
/// <param name="tempFile">The temp file.</param>
/// <returns>Task{System.String}.</returns>
/// <exception cref="System.ArgumentNullException">progress</exception>
/// <exception cref="HttpException"></exception>
private async Task<string> GetTempFile(HttpRequestOptions options, string tempFile)
{ {
ValidateParams(options.Url, options.CancellationToken); ValidateParams(options.Url, options.CancellationToken);
var tempFile = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + ".tmp");
if (options.Progress == null) if (options.Progress == null)
{ {
throw new ArgumentNullException("progress"); throw new ArgumentNullException("progress");
@ -310,6 +306,8 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
return tempFile; return tempFile;
} }
protected static readonly CultureInfo UsCulture = new CultureInfo("en-US");
/// <summary> /// <summary>
/// Handles the temp file exception. /// Handles the temp file exception.
/// </summary> /// </summary>