Skip to content
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

Add support for Basic Authentication #18

Merged
merged 2 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
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
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,15 +5,13 @@

public class RqliteOrmClient : RqliteClient, IRqliteOrmClient
{
public RqliteOrmClient(HttpClient client) : base(client) { }

public RqliteOrmClient(string uri, HttpClient? client = null) : base(uri, client) {}

/// <inheritdoc />
public async Task<List<T>> Query<T>(string query) where T : new()
{
var response = await Query(query);
if (response.Results!.Count > 1)

Check warning on line 14 in RqliteDotnet/RqliteOrmClient.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 14 in RqliteDotnet/RqliteOrmClient.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
throw new DataException("Query returned more than 1 result. At the moment only 1 result supported");
var res = response.Results[0];

Expand All @@ -27,8 +25,8 @@

foreach (var prop in typeof(T).GetProperties())
{
var index = res.Columns.FindIndex(c => c.ToLower() == prop.Name.ToLower());

Check warning on line 28 in RqliteDotnet/RqliteOrmClient.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 28 in RqliteDotnet/RqliteOrmClient.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
var val = GetValue(res.Types[index], res.Values[i][index]);

Check warning on line 29 in RqliteDotnet/RqliteOrmClient.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 29 in RqliteDotnet/RqliteOrmClient.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

prop.SetValue(dto, val);
}
Expand Down Expand Up @@ -58,8 +56,8 @@

foreach (var prop in typeof(U).GetProperties())
{
var index = res.Columns.FindIndex(c => c.ToLower() == prop.Name.ToLower());

Check warning on line 59 in RqliteDotnet/RqliteOrmClient.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 59 in RqliteDotnet/RqliteOrmClient.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
var val = GetValue(res.Types[index], res.Values[i][index]);

Check warning on line 60 in RqliteDotnet/RqliteOrmClient.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 60 in RqliteDotnet/RqliteOrmClient.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

prop.SetValue(dto, val);
}
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);
}
}
Loading