Skip to content

Commit ad7319c

Browse files
committed
Adding global BaseAddress config option
1 parent 5e29cda commit ad7319c

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

src/Rqlite.Client/RqliteClientFactory.cs

+10-15
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// Copyright (c) bfren - licensed under https://mit.bfren.dev/2023
33

44
using System;
5-
using System.Collections.Generic;
65
using System.Net.Http;
76
using Microsoft.Extensions.Logging;
87
using Microsoft.Extensions.Options;
98
using Rqlite.Client.Exceptions;
9+
using Wrap;
1010

1111
namespace Rqlite.Client;
1212

@@ -58,29 +58,24 @@ public IRqliteClient CreateClient() =>
5858

5959
/// <inheritdoc/>
6060
public IRqliteClient CreateClient(string httpClientName) =>
61-
Options.Clients.GetValueOrDefault(httpClientName) switch
62-
{
63-
RqliteOptions.Client client =>
64-
new RqliteClient(
65-
httpClient: HttpClientFactory.CreateClient(httpClientName),
66-
includeTimings: client.IncludeTimings ?? Options.IncludeTimings,
67-
logger: Logger
68-
),
69-
70-
_ =>
71-
throw new UnknownClientException($"Client '{httpClientName}' cannot be found in Rqlite settings.")
72-
};
61+
Options.Clients.GetValueOrNone(httpClientName).Match(
62+
none: () => throw new UnknownClientException($"Client '{httpClientName}' cannot be found in Rqlite settings."),
63+
some: x => new RqliteClient(
64+
httpClient: HttpClientFactory.CreateClient(httpClientName),
65+
includeTimings: x.IncludeTimings ?? Options.IncludeTimings,
66+
logger: Logger
67+
)
68+
);
7369

7470
/// <inheritdoc/>
7571
public IRqliteClient CreateClientWithDefaults()
7672
{
7773
// get default options
7874
var options = new RqliteOptions();
79-
var clientOptions = new RqliteOptions.Client();
8075

8176
// create and configure the HttpClient
8277
var httpClient = HttpClientFactory.CreateClient();
83-
httpClient.BaseAddress = new(clientOptions.BaseAddress);
78+
httpClient.BaseAddress = new(options.BaseAddress);
8479
httpClient.Timeout = TimeSpan.FromSeconds(options.TimeoutInSeconds);
8580

8681
// return default RqliteClient

src/Rqlite.Client/RqliteOptions.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public sealed record class RqliteOptions
1515
/// </summary>
1616
public string? DefaultClientName { get; init; }
1717

18+
/// <summary>
19+
/// The default base address for all clients (without trailing slash).
20+
/// </summary>
21+
public string BaseAddress { get; init; } = "http://localhost:4001";
22+
1823
/// <summary>
1924
/// If set to true, timings will be included with each request.
2025
/// </summary>
@@ -36,9 +41,9 @@ public sealed record class RqliteOptions
3641
public sealed record class Client
3742
{
3843
/// <summary>
39-
/// The base address of the Rqlite database instance (without trailing slash).
44+
/// Set to override the base address of the Rqlite database instance (without trailing slash).
4045
/// </summary>
41-
public string BaseAddress { get; init; } = "http://localhost:4001";
46+
public string? BaseAddress { get; init; }
4247

4348
/// <summary>
4449
/// Set to override the global setting to include timings with each request.

src/Rqlite.Client/ServiceCollectionExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static IServiceCollection AddRqlite(this IServiceCollection @this)
3030
{
3131
_ = @this.AddHttpClient(name, opt =>
3232
{
33-
opt.BaseAddress = new(connection.BaseAddress);
33+
opt.BaseAddress = new(connection.BaseAddress ?? rqliteOptions.BaseAddress);
3434
opt.Timeout = TimeSpan.FromSeconds(connection.TimeoutInSeconds ?? rqliteOptions.TimeoutInSeconds);
3535
});
3636
}

0 commit comments

Comments
 (0)