From 627b8370a89cbf9826898c2edfc46767dfb5272a Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Sun, 3 Mar 2013 00:25:42 -0500 Subject: [PATCH] made IHttpClient a little more flexible, allow Semaphore to be optional --- .../HttpClientManager/HttpClientManager.cs | 103 ++++++++++++++---- MediaBrowser.Common/Net/IHttpClient.cs | 35 ++++++ 2 files changed, 119 insertions(+), 19 deletions(-) diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs index f542165e4c..e01718f45d 100644 --- a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs +++ b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs @@ -100,11 +100,14 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager /// public async Task Get(string url, SemaphoreSlim resourcePool, CancellationToken cancellationToken) { - ValidateParams(url, resourcePool, cancellationToken); + ValidateParams(url, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); - await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); + if (resourcePool != null) + { + await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); + } _logger.Info("HttpClientManager.Get url: {0}", url); @@ -130,7 +133,10 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager } finally { - resourcePool.Release(); + if (resourcePool != null) + { + resourcePool.Release(); + } } } @@ -146,7 +152,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager /// public async Task Post(string url, Dictionary postData, SemaphoreSlim resourcePool, CancellationToken cancellationToken) { - ValidateParams(url, resourcePool, cancellationToken); + ValidateParams(url, cancellationToken); if (postData == null) { @@ -159,7 +165,10 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager var postContent = string.Join("&", strings.ToArray()); var content = new StringContent(postContent, Encoding.UTF8, "application/x-www-form-urlencoded"); - await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); + if (resourcePool != null) + { + await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); + } _logger.Info("HttpClientManager.Post url: {0}", url); @@ -185,7 +194,10 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager } finally { - resourcePool.Release(); + if (resourcePool != null) + { + resourcePool.Release(); + } } } @@ -202,7 +214,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager /// public async Task GetTempFile(string url, SemaphoreSlim resourcePool, CancellationToken cancellationToken, IProgress progress, string userAgent = null) { - ValidateParams(url, resourcePool, cancellationToken); + ValidateParams(url, cancellationToken); if (progress == null) { @@ -220,7 +232,10 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager message.Headers.Add("User-Agent", userAgent); } - await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); + if (resourcePool != null) + { + await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); + } _logger.Info("HttpClientManager.GetTempFile url: {0}, temp file: {1}", url, tempFile); @@ -304,7 +319,10 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager } finally { - resourcePool.Release(); + if (resourcePool != null) + { + resourcePool.Release(); + } } } @@ -318,13 +336,16 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager /// public async Task GetMemoryStream(string url, SemaphoreSlim resourcePool, CancellationToken cancellationToken) { - ValidateParams(url, resourcePool, cancellationToken); + ValidateParams(url, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); var message = new HttpRequestMessage(HttpMethod.Get, url); - await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); + if (resourcePool != null) + { + await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); + } var ms = new MemoryStream(); @@ -376,7 +397,10 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager } finally { - resourcePool.Release(); + if (resourcePool != null) + { + resourcePool.Release(); + } } } @@ -384,21 +408,15 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager /// Validates the params. /// /// The URL. - /// The resource pool. /// The cancellation token. /// url - private void ValidateParams(string url, SemaphoreSlim resourcePool, CancellationToken cancellationToken) + private void ValidateParams(string url, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(url)) { throw new ArgumentNullException("url"); } - if (resourcePool == null) - { - throw new ArgumentNullException("resourcePool"); - } - if (cancellationToken == null) { throw new ArgumentNullException("cancellationToken"); @@ -478,5 +496,52 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager throw new HttpException(response.ReasonPhrase) { StatusCode = response.StatusCode }; } } + + /// + /// Gets the specified URL. + /// + /// The URL. + /// The cancellation token. + /// Task{Stream}. + public Task Get(string url, CancellationToken cancellationToken) + { + return Get(url, null, cancellationToken); + } + + /// + /// Posts the specified URL. + /// + /// The URL. + /// The post data. + /// The cancellation token. + /// Task{Stream}. + public Task Post(string url, Dictionary postData, CancellationToken cancellationToken) + { + return Post(url, postData, null, cancellationToken); + } + + /// + /// Gets the temp file. + /// + /// The URL. + /// The cancellation token. + /// The progress. + /// The user agent. + /// Task{System.String}. + public Task GetTempFile(string url, CancellationToken cancellationToken, IProgress progress, string userAgent = null) + { + return GetTempFile(url, null, cancellationToken, progress, userAgent); + } + + /// + /// Gets the memory stream. + /// + /// The URL. + /// The cancellation token. + /// Task{MemoryStream}. + public Task GetMemoryStream(string url, CancellationToken cancellationToken) + { + return GetMemoryStream(url, null, cancellationToken); + } } } diff --git a/MediaBrowser.Common/Net/IHttpClient.cs b/MediaBrowser.Common/Net/IHttpClient.cs index d02f08572d..cec3ccff21 100644 --- a/MediaBrowser.Common/Net/IHttpClient.cs +++ b/MediaBrowser.Common/Net/IHttpClient.cs @@ -21,6 +21,14 @@ namespace MediaBrowser.Common.Net /// Task Get(string url, SemaphoreSlim resourcePool, CancellationToken cancellationToken); + /// + /// Gets the specified URL. + /// + /// The URL. + /// The cancellation token. + /// Task{Stream}. + Task Get(string url, CancellationToken cancellationToken); + /// /// Performs a POST request /// @@ -33,6 +41,15 @@ namespace MediaBrowser.Common.Net /// Task Post(string url, Dictionary postData, SemaphoreSlim resourcePool, CancellationToken cancellationToken); + /// + /// Posts the specified URL. + /// + /// The URL. + /// The post data. + /// The cancellation token. + /// Task{Stream}. + Task Post(string url, Dictionary postData, CancellationToken cancellationToken); + /// /// Downloads the contents of a given url into a temporary location /// @@ -46,6 +63,16 @@ namespace MediaBrowser.Common.Net /// Task GetTempFile(string url, SemaphoreSlim resourcePool, CancellationToken cancellationToken, IProgress progress, string userAgent = null); + /// + /// Gets the temp file. + /// + /// The URL. + /// The cancellation token. + /// The progress. + /// The user agent. + /// Task{System.String}. + Task GetTempFile(string url, CancellationToken cancellationToken, IProgress progress, string userAgent = null); + /// /// Downloads the contents of a given url into a MemoryStream /// @@ -55,5 +82,13 @@ namespace MediaBrowser.Common.Net /// Task{MemoryStream}. /// Task GetMemoryStream(string url, SemaphoreSlim resourcePool, CancellationToken cancellationToken); + + /// + /// Gets the memory stream. + /// + /// The URL. + /// The cancellation token. + /// Task{MemoryStream}. + Task GetMemoryStream(string url, CancellationToken cancellationToken); } } \ No newline at end of file