From 03104c487a3595730cf64ec87b038b1cfead96c4 Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 9 Dec 2024 00:08:01 +0200 Subject: [PATCH 1/2] Add support for Basic Authentication --- RqliteDotnet/RqliteClient.cs | 13 ++++++++++++- RqliteDotnet/RqliteOrmClient.cs | 2 -- RqliteDotnetExample/Program.cs | 5 +++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/RqliteDotnet/RqliteClient.cs b/RqliteDotnet/RqliteClient.cs index d69745a..594a0ee 100644 --- a/RqliteDotnet/RqliteClient.cs +++ b/RqliteDotnet/RqliteClient.cs @@ -8,9 +8,20 @@ public class RqliteClient : IRqliteClient { private readonly HttpClient _httpClient; - public RqliteClient(string uri, HttpClient? client = null) + /// + /// Returns new RqliteClient + /// + /// URI where rqlite instance is running + /// Http client to use instead of default one + /// Basic auth string (username:password) that is base64 encoded and sent in Authorization header. If empty the header is not set + public RqliteClient(string uri, HttpClient? client = null, string basicAuthInfo = "") { _httpClient = client ?? new HttpClient(){ BaseAddress = new Uri(uri) }; + if (!string.IsNullOrEmpty(basicAuthInfo)) + { + var encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(basicAuthInfo)); + _httpClient.DefaultRequestHeaders.Add("Authorization", $"Basic {encoded}"); + } } public RqliteClient(HttpClient client) diff --git a/RqliteDotnet/RqliteOrmClient.cs b/RqliteDotnet/RqliteOrmClient.cs index e792417..bd1337c 100644 --- a/RqliteDotnet/RqliteOrmClient.cs +++ b/RqliteDotnet/RqliteOrmClient.cs @@ -5,8 +5,6 @@ namespace RqliteDotnet; public class RqliteOrmClient : RqliteClient, IRqliteOrmClient { - public RqliteOrmClient(HttpClient client) : base(client) { } - public RqliteOrmClient(string uri, HttpClient? client = null) : base(uri, client) {} /// diff --git a/RqliteDotnetExample/Program.cs b/RqliteDotnetExample/Program.cs index 8fabb49..92813ca 100644 --- a/RqliteDotnetExample/Program.cs +++ b/RqliteDotnetExample/Program.cs @@ -5,9 +5,10 @@ namespace RqliteDotnetExample; public static class RqliteDotnetExample { - public static void Main(string[] args) + public static async Task Main(string[] args) { var x = new RqliteClient("http://localhost:4001"); - Console.WriteLine(x.Ping().Result); + var ping = await x.Ping(); + Console.WriteLine(ping); } } \ No newline at end of file From 7cfe144608f65d7e9e18277649ccc24452693cc4 Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 9 Dec 2024 00:18:09 +0200 Subject: [PATCH 2/2] Typo --- RqliteDotnet/RqliteClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RqliteDotnet/RqliteClient.cs b/RqliteDotnet/RqliteClient.cs index 594a0ee..7621886 100644 --- a/RqliteDotnet/RqliteClient.cs +++ b/RqliteDotnet/RqliteClient.cs @@ -13,7 +13,7 @@ public class RqliteClient : IRqliteClient /// /// URI where rqlite instance is running /// Http client to use instead of default one - /// Basic auth string (username:password) that is base64 encoded and sent in Authorization header. If empty the header is not set + /// Basic auth string (username:password) that is base64 encoded and sent in Authorization header. If empty the header is not sent public RqliteClient(string uri, HttpClient? client = null, string basicAuthInfo = "") { _httpClient = client ?? new HttpClient(){ BaseAddress = new Uri(uri) };