Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #13 from StephenClearyFaithlife/reuse-httpclient
Browse files Browse the repository at this point in the history
Add WebServiceRequestSettings.GetHttpClient.
  • Loading branch information
StephenCleary authored Aug 8, 2018
2 parents 99143c4 + 0f07c6c commit 67c905d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 29 deletions.
2 changes: 2 additions & 0 deletions VersionHistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ is updated in preparation for publishing an updated NuGet package.

Prefix the description of the change with `[major]`, `[minor]` or `[patch]` in accordance with [SemVer](http://semver.org).

* [minor] Add `WebServiceRequestSettings.GetHttpClient` to allow `HttpClient` reuse.

## Released

### 0.5.0
Expand Down
65 changes: 38 additions & 27 deletions src/Faithlife.WebRequests/WebServiceRequestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public string Method
public string IfNoneMatch { get; set; }

/// <summary>
/// Gets or sets the timeout.
/// Gets or sets the timeout. If <see cref="WebServiceRequestSettings.GetHttpClient"/> is set, then this property is ignored.
/// </summary>
/// <value>The timeout.</value>
public TimeSpan? Timeout { get; set; }
Expand Down Expand Up @@ -125,7 +125,7 @@ public string Method
public ByteRange Range { get; set; }

/// <summary>
/// True if HTTP redirects should not be followed automatically.
/// True if HTTP redirects should not be followed automatically. If <see cref="WebServiceRequestSettings.GetHttpClient"/> is set, then this property is ignored.
/// </summary>
public bool DisableAutoRedirect { get; set; }

Expand Down Expand Up @@ -188,12 +188,34 @@ protected virtual void OnWebRequestCreated(HttpRequestMessage request)
{
}

private HttpRequestMessage CreateWebRequest(out HttpClient client)
private HttpClientHandler CreateHttpClientHandler(WebServiceRequestSettings settings)
{
WebServiceRequestSettings settings = Settings ?? new WebServiceRequestSettings();
var request = new HttpRequestMessage(new HttpMethod(Method ?? "GET"), RequestUri);
var handler = new HttpClientHandler();
client = new HttpClient(handler);
if (settings.CookieManager != null)
handler.CookieContainer = settings.CookieManager.CookieContainer;

handler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

if (DisableAutoRedirect)
handler.AllowAutoRedirect = false;

return handler;
}

private HttpClient CreateHttpClient(WebServiceRequestSettings settings)
{
if (settings.GetHttpClient != null)
return settings.GetHttpClient();

var client = new HttpClient(CreateHttpClientHandler(settings));
var timeout = Timeout ?? settings.DefaultTimeout;
client.Timeout = timeout ?? System.Threading.Timeout.InfiniteTimeSpan;
return client;
}

private HttpRequestMessage CreateHttpRequestMessage(WebServiceRequestSettings settings)
{
var request = new HttpRequestMessage(new HttpMethod(Method ?? "GET"), RequestUri);

if (settings.DefaultHeaders != null)
request.Headers.AddWebHeaders(settings.DefaultHeaders);
Expand All @@ -204,7 +226,7 @@ private HttpRequestMessage CreateWebRequest(out HttpClient client)
if (!string.IsNullOrEmpty(Accept))
request.Headers.Accept.ParseAdd(Accept);

string userAgent = UserAgent ?? settings.UserAgent;
var userAgent = UserAgent ?? settings.UserAgent;
if (!string.IsNullOrEmpty(userAgent))
request.Headers.UserAgent.ParseAdd(userAgent);

Expand All @@ -220,19 +242,10 @@ private HttpRequestMessage CreateWebRequest(out HttpClient client)
if (settings.Host != null)
request.Headers.Host = settings.Host;

TimeSpan? timeout = Timeout ?? settings.DefaultTimeout;
if (timeout.HasValue)
client.Timeout = timeout.Value;

if (settings.CookieManager != null)
handler.CookieContainer = settings.CookieManager.CookieContainer;

string authorizationHeader = settings.AuthorizationHeader ?? (settings.AuthorizationHeaderCreator != null ? settings.AuthorizationHeaderCreator(new WebServiceRequestInfo(request)) : null);
var authorizationHeader = settings.AuthorizationHeader ?? settings.AuthorizationHeaderCreator?.Invoke(new WebServiceRequestInfo(request));
if (authorizationHeader != null)
request.Headers.Authorization = AuthenticationHeaderValue.Parse(authorizationHeader);

handler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

if (IfMatch != null)
request.Headers.IfMatch.ParseAdd(IfMatch);

Expand All @@ -242,22 +255,20 @@ private HttpRequestMessage CreateWebRequest(out HttpClient client)
if (IfNoneMatch != null)
request.Headers.IfNoneMatch.ParseAdd(IfNoneMatch);

if (DisableAutoRedirect)
handler.AllowAutoRedirect = false;

if (Range != null)
{
if (!Range.HasEnd)
request.Headers.Range = new RangeHeaderValue(Range.From, null);
else
request.Headers.Range = new RangeHeaderValue(Range.From, Range.To);
}
request.Headers.Range = new RangeHeaderValue(Range.From, Range.HasEnd ? Range.To : default(long?));

OnWebRequestCreated(request);

return request;
}

private HttpRequestMessage CreateWebRequest(out HttpClient client)
{
var settings = Settings ?? new WebServiceRequestSettings();
client = CreateHttpClient(settings);
return CreateHttpRequestMessage(settings);
}

private HttpContent GetRequestContent(HttpRequestMessage webRequest)
{
HttpContent requestContent = Content;
Expand Down
12 changes: 10 additions & 2 deletions src/Faithlife.WebRequests/WebServiceRequestSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public WebServiceRequestSettings()
public string UserAgent { get; set; }

/// <summary>
/// Gets or sets the cookie manager.
/// Gets or sets the cookie manager. If <see cref="GetHttpClient"/> is set, then this property is ignored.
/// </summary>
/// <value>The cookie manager.</value>
public CookieManager CookieManager { get; set; }
Expand Down Expand Up @@ -51,7 +51,7 @@ public WebServiceRequestSettings()
public WebHeaderCollection DefaultHeaders { get; set; }

/// <summary>
/// Gets or sets the default timeout.
/// Gets or sets the default timeout. If <see cref="GetHttpClient"/> is set, then this property is ignored.
/// </summary>
/// <value>The default timeout.</value>
public TimeSpan? DefaultTimeout { get; set; }
Expand Down Expand Up @@ -87,6 +87,14 @@ public WebServiceRequestSettings()
/// </summary>
public Func<HttpRequestMessage, IDisposable> StartTrace { get; set; }

/// <summary>
/// A delegate that, if set, is called to retrieve an <see cref="HttpClient"/>. If this property is set, then the consumer is responsible for the entire lifetime of the <see cref="HttpClient"/>, including disposal.
/// </summary>
/// <remarks>
/// If this property is set, then <see cref="CookieManager"/>, <see cref="WebServiceRequestBase.DisableAutoRedirect"/>, <see cref="DefaultTimeout"/>, and <see cref="WebServiceRequestBase.Timeout"/> are ignored.
/// </remarks>
public Func<HttpClient> GetHttpClient { get; set; }

/// <summary>
/// Clones this instance.
/// </summary>
Expand Down

0 comments on commit 67c905d

Please sign in to comment.