Skip to content

Add http client constructor #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions Source/EasyNetQ.Management.Client/ManagementClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net;
using System.Net.Http.Headers;
using System.Text;
using EasyNetQ.Management.Client.Internals;
using EasyNetQ.Management.Client.Model;
using EasyNetQ.Management.Client.Serialization;
Expand All @@ -9,6 +10,7 @@

#if NET6_0
using HttpHandler = System.Net.Http.SocketsHttpHandler;

#else
using HttpHandler = System.Net.Http.HttpClientHandler;
#endif
Expand Down Expand Up @@ -44,6 +46,8 @@ public class ManagementClient : IManagementClient

private readonly HttpClient httpClient;
private readonly Action<HttpRequestMessage>? configureHttpRequestMessage;
private readonly bool disposeHttpClient;
private readonly AuthenticationHeaderValue basicAuthHeader;

static ManagementClient()
{
Expand Down Expand Up @@ -97,9 +101,25 @@ public ManagementClient(

this.configureHttpRequestMessage = configureHttpRequestMessage;

var httpHandler = new HttpHandler { Credentials = new NetworkCredential(username, password) };
var httpHandler = new HttpHandler();
configureHttpHandler?.Invoke(httpHandler);
basicAuthHeader = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}")));
httpClient = new HttpClient(httpHandler) { Timeout = timeout ?? DefaultTimeout, BaseAddress = endpoint };
disposeHttpClient = true;
}

public ManagementClient(HttpClient httpClient, string username, string password)
{
if (httpClient.BaseAddress == null)
throw new ArgumentNullException(nameof(httpClient.BaseAddress), "Endpoint should be specified");

if (!httpClient.BaseAddress.IsAbsoluteUri)
throw new ArgumentOutOfRangeException(nameof(httpClient.BaseAddress), httpClient.BaseAddress, "Endpoint should be absolute");

this.httpClient = httpClient;
basicAuthHeader = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}")));
configureHttpRequestMessage = null;
disposeHttpClient = false;
}

public Uri Endpoint => httpClient.BaseAddress!;
Expand Down Expand Up @@ -542,7 +562,8 @@ public async Task<bool> IsAliveAsync(Vhost vhost, CancellationToken cancellation

public void Dispose()
{
httpClient.Dispose();
if (disposeHttpClient)
httpClient.Dispose();
}

public Task<IReadOnlyList<Consumer>> GetConsumersAsync(CancellationToken cancellationToken = default)
Expand All @@ -558,7 +579,7 @@ private async Task<T> GetAsync<T>(
CancellationToken cancellationToken = default
)
{
using var request = CreateRequestForPath(HttpMethod.Get, path, queryParameters);
using var request = CreateRequest(HttpMethod.Get, path, queryParameters);
using var response = await httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);

return await DeserializeResponseAsync<T>(c => c == HttpStatusCode.OK, response).ConfigureAwait(false);
Expand All @@ -570,7 +591,7 @@ private async Task<TResult> PostAsync<TItem, TResult>(
CancellationToken cancellationToken = default
)
{
using var request = CreateRequestForPath(HttpMethod.Post, path);
using var request = CreateRequest(HttpMethod.Post, path);

InsertRequestBody(request, item);

Expand All @@ -583,7 +604,7 @@ private async Task<TResult> PostAsync<TItem, TResult>(

private async Task DeleteAsync(RelativePath path, CancellationToken cancellationToken = default)
{
using var request = CreateRequestForPath(HttpMethod.Delete, path);
using var request = CreateRequest(HttpMethod.Delete, path);
using var response = await httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);

await DeserializeResponseAsync(c => c == HttpStatusCode.NoContent, response).ConfigureAwait(false);
Expand All @@ -595,7 +616,7 @@ private async Task PutAsync<T>(
CancellationToken cancellationToken = default
) where T : class
{
using var request = CreateRequestForPath(HttpMethod.Put, path);
using var request = CreateRequest(HttpMethod.Put, path);

if (item != default) InsertRequestBody(request, item);

Expand Down Expand Up @@ -633,15 +654,16 @@ private static void InsertRequestBody<T>(HttpRequestMessage request, T item)
request.Content = content;
}

private HttpRequestMessage CreateRequestForPath(
private HttpRequestMessage CreateRequest(
HttpMethod httpMethod,
in RelativePath path,
IReadOnlyDictionary<string, string>? queryParameters = null
)
{
var httpRequestMessage = new HttpRequestMessage(httpMethod, QueryStringHelpers.AddQueryString(path.Build(), queryParameters));
configureHttpRequestMessage?.Invoke(httpRequestMessage);
return httpRequestMessage;
var request = new HttpRequestMessage(httpMethod, QueryStringHelpers.AddQueryString(path.Build(), queryParameters));
request.Headers.Authorization = basicAuthHeader;
configureHttpRequestMessage?.Invoke(request);
return request;
}

private static IReadOnlyDictionary<string, string>? MergeQueryParameters(params IReadOnlyDictionary<string, string>?[]? multipleQueryParameters)
Expand Down