Skip to content

Commit

Permalink
Finish custom HttpClient constructor
Browse files Browse the repository at this point in the history
This commit adds a constructor that accepts an HttpClient with
user-supplied parameters. Fixes hashicorp#42.
  • Loading branch information
highlyunavailable committed Mar 10, 2016
1 parent 2f68f5b commit ddde081
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
16 changes: 10 additions & 6 deletions Consul.Test/ClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,17 @@ public async Task Client_SetWriteOptions()
[Fact]
public async Task Client_CustomHttpClient()
{
var hc = new HttpClient();
hc.Timeout = TimeSpan.FromDays(10);
using (var client = new ConsulClient(new ConsulClientConfiguration(), hc))
using (var hc = new HttpClient())
{
await client.KV.Put(new KVPair("kv/customhttpclient"));
Assert.Equal(TimeSpan.FromDays(10), client.HttpClient.Timeout);
Assert.True(client.HttpClient.DefaultRequestHeaders.Accept.Contains(new MediaTypeWithQualityHeaderValue("application/json")));
hc.Timeout = TimeSpan.FromDays(10);
hc.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using (var client = new ConsulClient(new ConsulClientConfiguration(), hc))
{
await client.KV.Put(new KVPair("customhttpclient") { Value = System.Text.Encoding.UTF8.GetBytes("hello world") });
Assert.Equal(TimeSpan.FromDays(10), client.HttpClient.Timeout);
Assert.True(client.HttpClient.DefaultRequestHeaders.Accept.Contains(new MediaTypeWithQualityHeaderValue("application/json")));
}
Assert.Equal("hello world", await (await hc.GetAsync("http://localhost:8500/v1/kv/customhttpclient?raw")).Content.ReadAsStringAsync());
}
}

Expand Down
22 changes: 5 additions & 17 deletions Consul/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ public class WriteResult<T> : WriteResult
public partial class ConsulClient : IDisposable
{
private object _lock = new object();
private bool skipClientDispose;
internal HttpClient HttpClient { get; set; }
internal ConsulClientConfiguration Config { get; set; }

Expand All @@ -409,37 +410,24 @@ public ConsulClient(ConsulClientConfiguration config)
Config = config;
HttpClient = new HttpClient(config.Handler);
HttpClient.Timeout = TimeSpan.FromMinutes(15);
HttpClient.BaseAddress = config.Address;
HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpClient.DefaultRequestHeaders.Add("Keep-Alive", "true");
}

/// <summary>
/// Initializes a new Consul client with the configuration specified and a custom HttpClient, which is useful for setting proxies/custom timeouts.
/// Do not reuse HttpClients with different ConsulClients, since the HttpClient will be disposed of by the ConsulClient.
/// The base address of the HTTP client will be set to the Address of the ConsulClientConfiguration.
/// If the HttpClient's timeout value is less than 15 minutes, it will be increased to 15 minutes to allow for blocking queries.
/// If the HttpClient does not accept the media type "application/json" it will be added.
/// If the HttpClient does not set Keep-Alive to true, it will be set to true.
/// The HttpClient must accept the "application/json" content type and the Timeout property should be set to at least 15 minutes to allow for blocking queries.
/// </summary>
/// <param name="config">A Consul client configuration</param>
/// <param name="client">A custom HttpClient</param>
public ConsulClient(ConsulClientConfiguration config, HttpClient client)
{
Config = config;
HttpClient = client;
HttpClient.BaseAddress = config.Address;
if (HttpClient.Timeout < TimeSpan.FromMinutes(15))
{
HttpClient.Timeout = TimeSpan.FromMinutes(15);
}
skipClientDispose = true;
if (!HttpClient.DefaultRequestHeaders.Accept.Contains(new MediaTypeWithQualityHeaderValue("application/json")))
{
HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
if (!HttpClient.DefaultRequestHeaders.Contains("Keep-Alive"))
{
HttpClient.DefaultRequestHeaders.Add("Keep-Alive", "true");
throw new ArgumentException("HttpClient must accept MIME type application/json", "client");
}
}

Expand All @@ -452,7 +440,7 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (HttpClient != null)
if (HttpClient != null && !skipClientDispose)
{
HttpClient.Dispose();
}
Expand Down

0 comments on commit ddde081

Please sign in to comment.