diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2013-05-04 00:07:27 -0400 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2013-05-04 00:07:27 -0400 |
| commit | 87598ca6d064aa7d3f20ef7ee564aab31f2cb53e (patch) | |
| tree | e23a2808afb4225c432b59cea24306fc95a8d723 | |
| parent | 163a1bdbcb4db04d97925aed496746ce5ef52580 (diff) | |
add customizable accept header to http client interface
| -rw-r--r-- | MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs | 106 | ||||
| -rw-r--r-- | MediaBrowser.Common/Net/HttpRequestOptions.cs | 6 | ||||
| -rw-r--r-- | MediaBrowser.Common/Net/IHttpClient.cs | 9 |
3 files changed, 83 insertions, 38 deletions
diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs index dd02bdc09..87922f60a 100644 --- a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs +++ b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs @@ -107,14 +107,13 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager /// <summary> /// Performs a GET request and returns the resulting stream /// </summary> - /// <param name="url">The URL.</param> - /// <param name="resourcePool">The resource pool.</param> - /// <param name="cancellationToken">The cancellation token.</param> + /// <param name="options">The options.</param> /// <returns>Task{Stream}.</returns> + /// <exception cref="HttpException"></exception> /// <exception cref="MediaBrowser.Model.Net.HttpException"></exception> - public async Task<Stream> Get(string url, SemaphoreSlim resourcePool, CancellationToken cancellationToken) + public async Task<Stream> Get(HttpRequestOptions options) { - ValidateParams(url, cancellationToken); + ValidateParams(options.Url, options.CancellationToken); //var urlHash = url.GetMD5().ToString(); //var infoPath = _cacheRepository.GetResourcePath(urlHash + ".js"); @@ -136,9 +135,9 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager // return GetCachedResponse(responsePath); //} - cancellationToken.ThrowIfCancellationRequested(); + options.CancellationToken.ThrowIfCancellationRequested(); - var message = new HttpRequestMessage(HttpMethod.Get, url); + var message = GetHttpRequestMessage(options); //if (cachedInfo != null) //{ @@ -152,22 +151,22 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager // } //} - if (resourcePool != null) + if (options.ResourcePool != null) { - await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); + await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false); } - _logger.Info("HttpClientManager.Get url: {0}", url); + _logger.Info("HttpClientManager.Get url: {0}", options.Url); try { - cancellationToken.ThrowIfCancellationRequested(); + options.CancellationToken.ThrowIfCancellationRequested(); - var response = await GetHttpClient(GetHostFromUrl(url)).SendAsync(message, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); + var response = await GetHttpClient(GetHostFromUrl(options.Url)).SendAsync(message, HttpCompletionOption.ResponseHeadersRead, options.CancellationToken).ConfigureAwait(false); EnsureSuccessStatusCode(response); - cancellationToken.ThrowIfCancellationRequested(); + options.CancellationToken.ThrowIfCancellationRequested(); //cachedInfo = UpdateInfoCache(cachedInfo, url, infoPath, response); @@ -187,30 +186,59 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager } catch (OperationCanceledException ex) { - throw GetCancellationException(url, cancellationToken, ex); + throw GetCancellationException(options.Url, options.CancellationToken, ex); } catch (HttpRequestException ex) { - _logger.ErrorException("Error getting response from " + url, ex); + _logger.ErrorException("Error getting response from " + options.Url, ex); throw new HttpException(ex.Message, ex); } catch (Exception ex) { - _logger.ErrorException("Error getting response from " + url, ex); + _logger.ErrorException("Error getting response from " + options.Url, ex); throw; } finally { - if (resourcePool != null) + if (options.ResourcePool != null) { - resourcePool.Release(); + options.ResourcePool.Release(); } } } /// <summary> + /// Performs a GET request and returns the resulting stream + /// </summary> + /// <param name="url">The URL.</param> + /// <param name="resourcePool">The resource pool.</param> + /// <param name="cancellationToken">The cancellation token.</param> + /// <returns>Task{Stream}.</returns> + public Task<Stream> Get(string url, SemaphoreSlim resourcePool, CancellationToken cancellationToken) + { + return Get(new HttpRequestOptions + { + Url = url, + ResourcePool = resourcePool, + CancellationToken = cancellationToken, + }); + } + + /// <summary> + /// Gets the specified URL. + /// </summary> + /// <param name="url">The URL.</param> + /// <param name="cancellationToken">The cancellation token.</param> + /// <returns>Task{Stream}.</returns> + public Task<Stream> Get(string url, CancellationToken cancellationToken) + { + return Get(url, null, cancellationToken); + } + + + /// <summary> /// Gets the cached response. /// </summary> /// <param name="responsePath">The response path.</param> @@ -393,13 +421,6 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager options.CancellationToken.ThrowIfCancellationRequested(); - var message = new HttpRequestMessage(HttpMethod.Get, options.Url); - - if (!string.IsNullOrEmpty(options.UserAgent)) - { - message.Headers.Add("User-Agent", options.UserAgent); - } - if (options.ResourcePool != null) { await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false); @@ -413,7 +434,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager { options.CancellationToken.ThrowIfCancellationRequested(); - using (var response = await GetHttpClient(GetHostFromUrl(options.Url)).SendAsync(message, HttpCompletionOption.ResponseHeadersRead, options.CancellationToken).ConfigureAwait(false)) + using (var response = await GetHttpClient(GetHostFromUrl(options.Url)).SendAsync(GetHttpRequestMessage(options), HttpCompletionOption.ResponseHeadersRead, options.CancellationToken).ConfigureAwait(false)) { EnsureSuccessStatusCode(response); @@ -464,6 +485,28 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager } /// <summary> + /// Gets the message. + /// </summary> + /// <param name="options">The options.</param> + /// <returns>HttpResponseMessage.</returns> + private HttpRequestMessage GetHttpRequestMessage(HttpRequestOptions options) + { + var message = new HttpRequestMessage(HttpMethod.Get, options.Url); + + if (!string.IsNullOrEmpty(options.UserAgent)) + { + message.Headers.Add("User-Agent", options.UserAgent); + } + + if (!string.IsNullOrEmpty(options.AcceptHeader)) + { + message.Headers.Add("Accept", options.AcceptHeader); + } + + return message; + } + + /// <summary> /// Gets the length of the content. /// </summary> /// <param name="response">The response.</param> @@ -617,17 +660,6 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager } /// <summary> - /// Gets the specified URL. - /// </summary> - /// <param name="url">The URL.</param> - /// <param name="cancellationToken">The cancellation token.</param> - /// <returns>Task{Stream}.</returns> - public Task<Stream> Get(string url, CancellationToken cancellationToken) - { - return Get(url, null, cancellationToken); - } - - /// <summary> /// Posts the specified URL. /// </summary> /// <param name="url">The URL.</param> diff --git a/MediaBrowser.Common/Net/HttpRequestOptions.cs b/MediaBrowser.Common/Net/HttpRequestOptions.cs index 98d02a9f9..77cb8b3df 100644 --- a/MediaBrowser.Common/Net/HttpRequestOptions.cs +++ b/MediaBrowser.Common/Net/HttpRequestOptions.cs @@ -15,6 +15,12 @@ namespace MediaBrowser.Common.Net public string Url { get; set; } /// <summary> + /// Gets or sets the accept header. + /// </summary> + /// <value>The accept header.</value> + public string AcceptHeader { get; set; } + + /// <summary> /// Gets or sets the cancellation token. /// </summary> /// <value>The cancellation token.</value> diff --git a/MediaBrowser.Common/Net/IHttpClient.cs b/MediaBrowser.Common/Net/IHttpClient.cs index 87f9b5d71..2998d1af9 100644 --- a/MediaBrowser.Common/Net/IHttpClient.cs +++ b/MediaBrowser.Common/Net/IHttpClient.cs @@ -28,7 +28,14 @@ namespace MediaBrowser.Common.Net /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task{Stream}.</returns> Task<Stream> Get(string url, CancellationToken cancellationToken); - + + /// <summary> + /// Gets the specified options. + /// </summary> + /// <param name="options">The options.</param> + /// <returns>Task{Stream}.</returns> + Task<Stream> Get(HttpRequestOptions options); + /// <summary> /// Performs a POST request /// </summary> |
