Skip to content

Commit

Permalink
Add support for Basic Authentication (#18)
Browse files Browse the repository at this point in the history
Add support for Basic Authentication when your rqlite node is configured to require it. Not used by default
  • Loading branch information
arthrp authored Dec 8, 2024
1 parent b22b2fc commit dad99d0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
13 changes: 12 additions & 1 deletion RqliteDotnet/RqliteClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@ public class RqliteClient : IRqliteClient
{
private readonly HttpClient _httpClient;

public RqliteClient(string uri, HttpClient? client = null)
/// <summary>
/// Returns new RqliteClient
/// </summary>
/// <param name="uri">URI where rqlite instance is running</param>
/// <param name="client">Http client to use instead of default one</param>
/// <param name="basicAuthInfo">Basic auth string (username:password) that is base64 encoded and sent in Authorization header. If empty the header is not sent</param>
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)
Expand Down
2 changes: 0 additions & 2 deletions RqliteDotnet/RqliteOrmClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}

/// <inheritdoc />
Expand Down
5 changes: 3 additions & 2 deletions RqliteDotnetExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit dad99d0

Please sign in to comment.