Skip to content

Commit

Permalink
Add HttpClientTransport(HttpMessageHandler) overload (#15724)
Browse files Browse the repository at this point in the history
Fixes: #15703

Considering most of the interesting settings are on HTTP client handler having an extra overload would help with more succinct transport configuration.
  • Loading branch information
pakrym authored Oct 6, 2020
1 parent aba1ac7 commit f6ccb0c
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 16 deletions.
2 changes: 2 additions & 0 deletions sdk/core/Azure.Core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 1.6.0-beta.1 (Unreleased)

### Added
- The `HttpClientTransport(HttpMessageHandler)` constructor overload.

## 1.5.1 (2020-10-01)

Expand Down
1 change: 1 addition & 0 deletions sdk/core/Azure.Core/api/Azure.Core.net461.cs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ public partial class HttpClientTransport : Azure.Core.Pipeline.HttpPipelineTrans
public static readonly Azure.Core.Pipeline.HttpClientTransport Shared;
public HttpClientTransport() { }
public HttpClientTransport(System.Net.Http.HttpClient client) { }
public HttpClientTransport(System.Net.Http.HttpMessageHandler messageHandler) { }
public sealed override Azure.Core.Request CreateRequest() { throw null; }
public override void Process(Azure.Core.HttpMessage message) { }
public sealed override System.Threading.Tasks.ValueTask ProcessAsync(Azure.Core.HttpMessage message) { throw null; }
Expand Down
1 change: 1 addition & 0 deletions sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ public partial class HttpClientTransport : Azure.Core.Pipeline.HttpPipelineTrans
public static readonly Azure.Core.Pipeline.HttpClientTransport Shared;
public HttpClientTransport() { }
public HttpClientTransport(System.Net.Http.HttpClient client) { }
public HttpClientTransport(System.Net.Http.HttpMessageHandler messageHandler) { }
public sealed override Azure.Core.Request CreateRequest() { throw null; }
public override void Process(Azure.Core.HttpMessage message) { }
public sealed override System.Threading.Tasks.ValueTask ProcessAsync(Azure.Core.HttpMessage message) { throw null; }
Expand Down
11 changes: 5 additions & 6 deletions sdk/core/Azure.Core/samples/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ SecretClientOptions options = new SecretClientOptions
## Configuring a proxy

```C# Snippet:HttpClientProxyConfiguration
using HttpClient client = new HttpClient(
new HttpClientHandler()
{
Proxy = new WebProxy(new Uri("http://example.com"))
});
using HttpClientHandler handler = new HttpClientHandler()
{
Proxy = new WebProxy(new Uri("http://example.com"))
};

SecretClientOptions options = new SecretClientOptions
{
Transport = new HttpClientTransport(client)
Transport = new HttpClientTransport(handler)
};
```
9 changes: 9 additions & 0 deletions sdk/core/Azure.Core/src/Pipeline/HttpClientTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public HttpClientTransport() : this(CreateDefaultClient())
{
}

/// <summary>
/// Creates a new instance of <see cref="HttpClientTransport"/> using the provided client instance.
/// </summary>
/// <param name="messageHandler">The instance of <see cref="HttpMessageHandler"/> to use.</param>
public HttpClientTransport(HttpMessageHandler messageHandler)
{
_client = new HttpClient(messageHandler) ?? throw new ArgumentNullException(nameof(messageHandler));
}

/// <summary>
/// Creates a new instance of <see cref="HttpClientTransport"/> using the provided client instance.
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions sdk/core/Azure.Core/tests/HttpClientTransportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task DoesntDisposeContentIfStreamGotReplaced()
});
});

var transport = new HttpClientTransport(new HttpClient(mockHandler));
var transport = new HttpClientTransport(mockHandler);
Request request = transport.CreateRequest();
request.Method = RequestMethod.Get;
request.Uri.Reset(new Uri("https://example.com:340"));
Expand All @@ -58,7 +58,7 @@ public async Task CanGetAndSetUri()
});

var expectedUri = new Uri("http://example.com:340");
var transport = new HttpClientTransport(new HttpClient(mockHandler));
var transport = new HttpClientTransport(mockHandler);
Request request = transport.CreateRequest();
request.Method = RequestMethod.Get;
request.Uri.Reset(expectedUri);
Expand Down Expand Up @@ -95,7 +95,7 @@ public async Task SettingContentHeaderDoesNotSetContent(string headerName, strin
HttpContent httpMessageContent = null;
var mockHandler = new MockHttpClientHandler(httpRequestMessage => { httpMessageContent = httpRequestMessage.Content; });

var transport = new HttpClientTransport(new HttpClient(mockHandler));
var transport = new HttpClientTransport(mockHandler);
Request request = transport.CreateRequest();
request.Method = RequestMethod.Get;
request.Uri.Reset(new Uri("https://example.com:340"));
Expand Down Expand Up @@ -127,7 +127,7 @@ public async Task SettingContentStreamPreservesHeaders(string headerName, string
return Task.FromResult(responseMessage);
});

var transport = new HttpClientTransport(new HttpClient(mockHandler));
var transport = new HttpClientTransport(mockHandler);
Request request = transport.CreateRequest();
request.Method = RequestMethod.Get;
request.Uri.Reset(new Uri("https://example.com:340"));
Expand Down
11 changes: 5 additions & 6 deletions sdk/core/Azure.Core/tests/samples/ConfigurationSamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,14 @@ public void HttpClientProxyConfiguration()
{
#region Snippet:HttpClientProxyConfiguration

using HttpClient client = new HttpClient(
new HttpClientHandler()
{
Proxy = new WebProxy(new Uri("http://example.com"))
});
using HttpClientHandler handler = new HttpClientHandler()
{
Proxy = new WebProxy(new Uri("http://example.com"))
};

SecretClientOptions options = new SecretClientOptions
{
Transport = new HttpClientTransport(client)
Transport = new HttpClientTransport(handler)
};

#endregion
Expand Down

0 comments on commit f6ccb0c

Please sign in to comment.